Shorten the program

I'm still quite new to this. Can someone tell me how can i shorten my program. Thanks in advanced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <stdlib.h>  
#include "clearscreen.h"
using namespace std;


int main()
{

    const int apple = 300;

    int  totalMoney  = 0;
    int  timesBought = 0;
    bool mustbeDigit = true;

    cout << "Hi! I'm CoreSiri, what's your name?" << endl;
    string name;
    getline (cin, name);
    cout << "                     " << endl;         // leaves a line
    cout << "How much money do you have?" << endl;

    cout << "$";
    cin  >> totalMoney;

    while (!cin)    // integer validation for totalMoney since it has to be digits
    {
        cout << "Invalid! Please enter a valid amount." << endl;
        cout << "$ ";
        cin.clear();
        cin.ignore();
        cin >> totalMoney;
        clearScreen();
    }

    if (totalMoney < 300)
    {
    cout << "Sorry " << name << ", you don't have enough money. Goodbye!" << endl;
    return 0;    // ends the program immediately
    }


    clearScreen();   // clears the screen
    cout << "Greetings " << name << "! You have $" << totalMoney << ", " << "would you like to buy an apple laptop worth $300?" << endl;
    cout << "1) Yes" << endl;
    cout << "2) No"  << endl;
    int resp;                   // response
    cin  >> resp;

    switch (resp)    // Input validation, resp has to be either 1 or 2 only
    {
        case 1 : goto mainLoop;
        case 2 : goto mainLoop;
        default: cout << "Invalid entry. Please try again." << endl;
                return 0;
    }

mainLoop:
    while (resp != 2)
    {
        totalMoney = totalMoney - apple;
        cout << "Money left: $" << totalMoney << endl;
        timesBought++;           // only to indicate number of times bought (increments)

        cout << "Buy again? (1- Yes / 2- No)" << endl;
        cin  >> resp;

        switch (resp)    // Input validation, resp has to be either 1 or 2 only
    {
        case 1 : goto mainLoop;
        case 2 : goto mainLoop;
        default: cout << "Invalid entry." << endl;
                 cout << "Amount left : $" << totalMoney << endl;
                 cout << "Times bought: "<< timesBought << " times.";
                 return 0;
    }

        if (totalMoney < 300)
        {
            cout << "You don't have enough money. Goodbye, " << name << "!" << endl; break;
        }
    }

    cout << "Amount left : $" << totalMoney << endl;     // displays total money left
    cout << "Times bought: "<< timesBought << " times."; // displays number of times bought
    return 0;
}


Note: I made my own header to clear the console hence the "clearscreen.h".
Topic archived. No new replies allowed.