How do you make a reset function? Tip Calculator example

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
//Tip Calculator
#include <iostream>
#include <math.h>
using namespace std;

float data[2];  //data[0] = billtotal, 1 = %2tip, 2 = # of peeps 
float totalTip;
float totalBill;
float paymentpp;
    
float calcTotalTip(float a, float b)
{
      float r;
      r = a * b * .01;
      return r;
}

float calcTotalBill(float a, float b)
{
      float r;
      r = a + b;
      return r;
}

float calcPaymentpp (float a, int b)
{
      float r;
      r = a/b;
      return r;
}

void printTotal(float t, float b, float p)
{
    cout << "The total amount to tip is $" << t << endl;     
    cout << "The total bill is $" << b << endl;
    cout << "The amount due per person is $" << p << endl;
}

void gatherData(float d[2])
{
    cout << "How much was the bill?\n";
    cin >> d[0];
    if (d[0]<0)
    {
            cout << "Error!  Your bill cannot be less than 0! Please restart the program." << endl;
            system("pause");
            exit(0);
    }
    cout << "What percentage do you want to tip?\n";
    cin >> d[1];
    if (d[1] == 0)
    {
             cout << "Wow you are a cheap ass mother fucker" << endl;
             system("pause");
             exit(0);
    }
    else if (d[1] < 0){
         cout << "You must tip a positive amount!  Please reset the program!" << endl;
         system("pause");
         exit(0);
    }
                 
    cout << "How many people are splitting the check?\n";
    cin >> d[2]; 
    if (d[2] < 1)
    {
             cout << "Error!  At least 1 person needs to pay!" << endl;
             system("pause");
             exit(0);
    }
}

int main()
{
    float data[2];  //data[0] = billtotal, 1 = %2tip, 2 = # of peeps     
    float totalTip;
    float totalBill;
    float paymentpp;
    int choice = 1;
    gatherData(data);
    totalTip = calcTotalTip (data[0], data[1]);
    totalBill = calcTotalBill (data[0], totalTip);
    paymentpp = calcPaymentpp (totalBill, data[2]); 
    printTotal(totalTip, totalBill, paymentpp);
    system("pause");
    return 0;
}


So I made a simple tip calculator program, but now I am struggling to think of a way to add a reset function after the program has executed, so it outputs something like "Press R to reset or Q to quit", and after you press R it restarts the program. Is there a simple way to do this in C++?
in your main function

you can do something like

1
2
3
4
5
6
7
do{
//your code
//set your values to 0 ... create a reset function here to set your d[] arrays to 0

cout << "Repeat (press R) or Quit (press Q)" << endl;
cin >> repeat 
}while( repeat != 'Q' )
Last edited on
please also remove the system pause, and if you are doing the
std::cout << "Repeat press r, or q for quit";
you need to do
cin>>repeat;
tolower(repeat); //in the ctype.h library
while(repeat != 'q')

this will help the program handle upper and lower case letters entered. making it a little more robust of a program.
For some reason I can't get the tolower function to work. Here is my main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    float data[2];  //data[0] = billtotal, 1 = %2tip, 2 = # of peeps     
    float totalTip;
    float totalBill;
    float paymentpp;
    int choice = 1;
    char repeat;
    do{
        gatherData(data);
        totalTip = calcTotalTip (data[0], data[1]);
        totalBill = calcTotalBill (data[0], totalTip);
        paymentpp = calcPaymentpp (totalBill, data[2]); 
        printTotal(totalTip, totalBill, paymentpp);
        reset(data);
        std::cout << "Enter Q to quit or R to calculate another tip" << endl;
        cin >> repeat;
        char tolower(repeat);
        }
        while(repeat != 'q');
    return 0;
}

For some reason, the tolower function doesn't return a lower case letter. I have the #include <ctype.h> and it still wont work.
repeat = tolower(repeat);
Thanks for the help everyone.

Alright, so now I am trying to make a function that ends the program, here is what I have:
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
void endProgram(char x){
        std::cout << "Enter Q to quit or R to calculate another tip" << endl;
        cin >> x;
        x = tolower(x);
        if((x != 'r')&&(x != 'q')){
           cout << "Invalid entry!" << endl;
           endProgram(x);
        }
}
     
int main()
{
    float data[2];  //data[0] = billtotal, 1 = %2tip, 2 = # of peeps     
    float totalTip, totalBill, paymentpp;
    char repeat;
    do{
        getBillTotal(data);
        getPercentTip(data);
        getPeopleAmount(data);
        totalTip = calcTotalTip (data[0], data[1]);
        totalBill = calcTotalBill (data[0], totalTip);
        paymentpp = calcPaymentpp (totalBill, data[2]); 
        printTotal(totalTip, totalBill, paymentpp);
        reset(data);
        endProgram(repeat);
        }
        while(repeat != 'q');
    return 0;
}


Now I am having a problem, for some reason the endProgram function isn't returning any value once it is called. The function works well enough, it will only continue on with the program if a Q or R is typed, however once the endProgram function executes there is no value stored in char repeat.

EDIT: Nevermind, I got it working! Simply change void endProgram to char endProgram, and change the line:

endProgram(repeat); to repeat = endProgram(repeat);
Last edited on
Topic archived. No new replies allowed.