What did I do wrong? Either function 3 or 4?

My program runs but does not seem to do functions 3 and 4. What have I done wrong? It must be the syntax, I am guessing, but I keep going over it, and I can't find the problems....Any help would be very appreciated.
Thank you,
Erin

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//This program calculates a customer's monthly bill

#include <iostream>

using namespace std;

char package;                                       //Declaring variables
float hours, paymentA, paymentB;
float packA = 9.95;
float packB = 14.95;
float packC = 19.95;
float savingsOnPackB = paymentA - packB;
float savingsOnPackC = paymentA - packC;

void getPackage();                                  //Declaring prototypes
void getBill();
void savingsWithA();
void savingsWithB();

void getPackage()                                   //Function Definition 1
{
    cout << "Which package have you purchased?" << endl                 //Ask for package
         << "Please enter A, B, or C and press the Enter key." << endl;
    cin  >> package;                                //Read package

    if(package == 'C')                              //If package is equal to C
        {
            cout << "Your payment is $" << packC << "." << endl;    //Then payment is automatically $19.95
        }
    else                                            //Or else, if package was NOT C,
    {
        cout << "How many hours were used during this billing cycle?" << endl   //Ask for hours
             << "Please enter numerical amount and press the Enter key." << endl;
        cin  >> hours;                              //Read hours
    }
}

void getBill()                                      //Function Definition 2
{
    if(package == 'A')                              //If package is equal to A,
    {
        if(hours <= 10)                             //And if hours were greater or equal to 10
        {
            cout << "Your payment is $" << packA << "." << endl;    //Display payment of $9.95
        }
        else                                        //Or else,
        {
            float paymentA = ((hours - 10) * 2) + packA;          //Payment equals that plus $2/addirional hour
            cout << "Your payment is $" << paymentA << endl;         //Display payment
        }
    }
        else                                        //Or else,
        {
            if(package == 'B')                      //If package is B
            {
                if (hours <= 20)                    //And hours are less than or equal to 20
                {
                    cout << "Your payment is $" << packB << "." << endl;    //Display payment of $14.95
                }
                else                                //Or else,
                {
                float paymentB = ((hours - 20) * 1) + packB;           //Payment is that plus additional $1/hhour
                cout << "Your payment is $" << paymentB << endl;         //Display payment
                }
            }
        }
 }

void savingsWithA()                                 //Function Definition 3
{
    if (package == 'A')                             //If package is A
    {
      if(paymentA > packC)                          //If the payment was greater than the cost of package C
        {
        cout << "You would save $" << savingsOnPackC << " if you upgraded to Package C." << endl; //Then display savings
        }
    }
    else                                            //Or else
    {
        if(paymentA > packB)                        //If the payment was greater than the cost of package B
        {
            cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
        }
    }
}

void savingsWithB()                                 //Function Definition 4
{
    if(package == 'B')                              //If package was B
    {
        if(paymentB > packC)                        //If payment was greater than the cost of package C
        {
            cout << "You would save $ " << savingsOnPackC << " if you upgraded to Package C." << endl; //Display savings
        }
    }

}


int main()                                          //Begin main program
{
    getPackage();                                   //Call functions
    getBill();
    savingsWithA();
    savingsWithB();
    return 0;                                       //Quit
}

closed account (zb0S216C)
Can you clarify this: "My program runs but does not seem to do functions 3 and 4." (sic) It's confusing and ambiguous.

Wazzak
Last edited on
Oh, I'm sorry. Sure. My program does what the first two functions were written to do: It accepts the Package when input, and it displays how much the bill is depending on which package was purchased. What it does not do is then calculate and display the savings amount if Packages B or C had been chosen.
The program is supposed to ask which package you have.
Then it should tell you what your bill is.
Then, it should tell you the amount you would have saved with either package B or C.
If no savings would have occurred, then no message regarding savings should be displayed.

Does that help? I feel like I'm really close (I hope!) but I can't seem to figure out the problem....
I saw a line or two that looked wrong, so here is what I have updated, although, functions 3 and 4 still do nothing...

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//This program calculates a customer's monthly bill

#include <iostream>

using namespace std;

char package;                                       //Declaring variables
float hours, paymentA, paymentB;
float packA = 9.95;
float packB = 14.95;
float packC = 19.95;

void getPackage();                                  //Declaring prototypes
void getBill();
void savingsWithA();
void savingsWithB();

void getPackage()                                   //Function Definition 1
{
    cout << "Which package have you purchased?" << endl                 //Ask for package
         << "Please enter A, B, or C and press the Enter key." << endl;
    cin  >> package;                                //Read package

    if(package == 'C')                              //If package is equal to C
        {
            cout << "Your payment is $" << packC << "." << endl;    //Then payment is automatically $19.95
        }
    else                                            //Or else, if package was NOT C,
    {
        cout << "How many hours were used during this billing cycle?" << endl   //Ask for hours
             << "Please enter numerical amount and press the Enter key." << endl;
        cin  >> hours;                              //Read hours
    }
}

void getBill()                                      //Function Definition 2
{
    if(package == 'A')                              //If package is equal to A,
    {
        if(hours <= 10)                             //And if hours were greater or equal to 10
        {
            cout << "Your payment is $" << packA << "." << endl;    //Display payment of $9.95
        }
        else                                        //Or else,
        {
            float paymentA = ((hours - 10) * 2) + packA;          //Payment equals that plus $2/additional hour
            cout << "Your payment is $" << paymentA << endl;         //Display payment
        }
    }
        else                                        //Or else,
        {
            if(package == 'B')                      //If package is B
            {
                if (hours <= 20)                    //And hours are less than or equal to 20
                {
                    cout << "Your payment is $" << packB << "." << endl;    //Display payment of $14.95
                }
                else                                //Or else,
                {
                float paymentB = ((hours - 20) * 1) + packB;           //Payment is that plus additional $1/hour
                cout << "Your payment is $" << paymentB << endl;         //Display payment
                }
            }
        }
 }

void savingsWithA()                                 //Function Definition 3
{
    if (package == 'A')                             //If package is A
    {
      if(paymentA > packB)                        //If the payment was greater than the cost of package B
        {
            float savingsOnPackB = paymentA - packB;
            cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
        }
      else
      {
          if(paymentA > packC)                          //If the payment was greater than the cost of package C
            {
            float savingsOnPackC = paymentA - packC;
            cout << "You would save $" << savingsOnPackC << " if you upgraded to Package C." << endl; //Then display savings
            }
      }

    }
}

void savingsWithB()                                 //Function Definition 4
{
    if(package == 'B')                              //If package was B
    {
        if(paymentB > packC)                        //If payment was greater than the cost of package C
        {
            float savingsOnPackC = paymentB - packC;
            cout << "You would save $ " << savingsOnPackC << " if you upgraded to Package C." << endl; //Display savings
        }
    }

}


int main()                                          //Begin main program
{
    getPackage();                                   //Call functions
    getBill();
    savingsWithA();
    savingsWithB();
    return 0;                                       //Quit
}
closed account (zb0S216C)
Sorry for the later reply, Erin.

The best thing to do is walk through it.

ErinCorona wrote:
1
2
3
4
5
if(paymentA > packB)                        //If the payment was greater than the cost of package B
        {
            float savingsOnPackB = paymentA - packB;
            cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
        }
(sic)

In function savingsWithA(), on line 71, if paymentA is greater than packB, the following block is executed, otherwise, the else is executed. Since it's not printing, control must've been passed to else.

ErinCorona wrote:
1
2
3
else
{
    if(paymentA > packC) 
(sic)

So, the proposition in else is tested. If the evaluation yields false, the following block isn't executed. Again, since it never prints, the proposition must've been false. Since there's no associated else, you're telling the computer that you're refusing to handle the scenario when the proposition yields false.

Since savingsWithB() is more or less the same as savingsWithA(), the latter can be applied to either function. Your main problem is your program's logic.

Note: I'll update this post with relevant information regarding your code.

Wazzak
It's okay, no worries, and thanks so much for even responding. This one has taken me the better part of a week, I'm embarrassed to admit. But....I think I got it! I'm so excited. Here's what I ended up with. Once I moved the variable declarations within each function to the very top of each function, it worked. :) I still have no idea how to put everything into an output file to send to my teacher, but I'm really excited that I got my program to work right. :)


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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//This program calculates a customer's monthly bill

#include <iostream>

using namespace std;

char package;                                       //Declaring variables
float hours;
float packA = 9.95;
float packB = 14.95;
float packC = 19.95;
float paymentA = ((hours - 10) * 2) + packA;
float paymentB = ((hours - 20) * 1) + packB;

void getPackage();                                  //Declaring prototypes
void getBill();
void savingsWithA();
void savingsWithB();

void getPackage()                                   //Function Definition 1
{
    cout << "Which package have you purchased?" << endl                 //Ask for package
         << "Please enter A, B, or C and press the Enter key." << endl;
    cin  >> package;                                //Read package

    if(package == 'C')                              //If package is equal to C
        {
            cout << "Your payment is $" << packC << "." << endl;    //Then payment is automatically $19.95
        }
    else                                            //Or else, if package was NOT C,
    {
        cout << "How many hours were used during this billing cycle?" << endl   //Ask for hours
             << "Please enter numerical amount and press the Enter key." << endl;
        cin  >> hours;                              //Read hours
    }
}

void getBill()                                      //Function Definition 2
{
    if(package == 'A')                              //If package is equal to A,
    {
        if(hours <= 10)                             //And if hours were greater or equal to 10
        {
            cout << "Your payment is $" << packA << "." << endl;    //Display payment of $9.95
        }
        else                                        //Or else,
        {
            float paymentA = ((hours - 10) * 2) + packA;          //Payment equals that plus $2/additional hour
            cout << "Your payment is $" << paymentA << endl;         //Display payment
        }
    }
        else                                        //Or else,
        {
            if(package == 'B')                      //If package is B
            {
                if (hours <= 20)                    //And hours are less than or equal to 20
                {
                    cout << "Your payment is $" << packB << "." << endl;    //Display payment of $14.95
                }
                else                                //Or else,
                {
                float paymentB = ((hours - 20) * 1) + packB;           //Payment is that plus additional $1/hour
                cout << "Your payment is $" << paymentB << endl;         //Display payment
                }
            }
        }
 }

void savingsWithA()                                 //Function Definition 3
{
    float paymentA = ((hours - 10) * 2) + packA;
    float savingsOnPackB = paymentA - packA;
    float savingsOnPackC = paymentA - packC;
    if (package == 'A')                             //If package is A
    {
        if(paymentA > packC)                          //If the payment was greater than the cost of package C
        {
            cout << "You would save $" << savingsOnPackC << " if you upgraded to Package C." << endl; //Then display savings
        }

        else
        {
            if(paymentA > packA)                        //If the payment was greater than the cost of package A
            {
                cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
            }
        }
    }

}


void savingsWithB()                                 //Function Definition 4
{
    float paymentB = ((hours - 20) * 1) + packB;
    float savingsOnPackC = paymentB - packC;
    if(package == 'B')                              //If package was B
    {
        if(paymentB > packC)                        //If payment was greater than the cost of package C
        {
                cout << "You would save $ " << savingsOnPackC << " if you upgraded to Package C." << endl; //Display savings
        }
    }

}


int main()                                          //Begin main program
{
    getPackage();                                   //Call functions
    getBill();
    savingsWithA();
    savingsWithB();
    return 0;                                       //Quit
}

I couldn't just leave it alone...no. I had to try and make it better. I'm trying to test it when the package gets input at the beginning. I'm trying to make sure it is either A, B, or C, or else I would like to display a message asking for A, B, or C. I thought that was the correct use of a while loop, but I've done it wrong, because it just loops forever. I will keep at it, but if there's anything you see right away, please give a girl a hint.
And thanks for all of your help. I have emailed my teacher questions (last week) but still gotten no reply, so this site is a godsend. :) I'm learning a lot.


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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//This program calculates a customer's monthly bill

#include <iostream>

using namespace std;

char package;                                       //Declaring variables
float hours;
float packA = 9.95;
float packB = 14.95;
float packC = 19.95;
float paymentA = ((hours - 10) * 2) + packA;
float paymentB = ((hours - 20) * 1) + packB;

void getPackage();                                  //Declaring prototypes
void isPackageC();
void getBill();
void savingsWithA();
void savingsWithB();

void getPackage()                                   //Function Definition 1
{
    cout << "Which package have you purchased?" << endl                 //Ask for package
         << "Please enter A, B, or C and press the Enter key." << endl;
    cin  >> package;                                //Read package
}

 void isPackageC()                                 //Function Definition 2
 {
        if(package == 'C')                              //If package is equal to C
        {
            cout << "Your payment is $" << packC << "." << endl;    //Then payment is automatically $19.95
        }
        else                                            //Or else, if package was NOT C,
        {
            cout << "How many hours were used during this billing cycle?" << endl   //Ask for hours
                 << "Please enter numerical amount and press the Enter key." << endl;
            cin  >> hours;                              //Read hours
        }
 }

void getBill()                                      //Function Definition 3
{
    if(package == 'A')                              //If package is equal to A,
    {
        if(hours <= 10)                             //And if hours were greater or equal to 10
        {
            cout << "Your payment is $" << packA << "." << endl;    //Display payment of $9.95
        }
        else                                        //Or else,
        {
            float paymentA = ((hours - 10) * 2) + packA;          //Payment equals that plus $2/additional hour
            cout << "Your payment is $" << paymentA << endl;         //Display payment
        }
    }
    else                                        //Or else,
    {
        if(package == 'B')                      //If package is B
        {
            if (hours <= 20)                    //And hours are less than or equal to 20
            {
                cout << "Your payment is $" << packB << "." << endl;    //Display payment of $14.95
            }
            else                                //Or else,
            {
                float paymentB = ((hours - 20) * 1) + packB;           //Payment is that plus additional $1/hour
                cout << "Your payment is $" << paymentB << endl;         //Display payment
            }
        }
    }
}

void savingsWithA()                                 //Function Definition 4
{
    float paymentA = ((hours - 10) * 2) + packA;
    float savingsOnPackB = paymentA - packA;
    float savingsOnPackC = paymentA - packC;
    if (package == 'A')                             //If package is A
    {
        if(paymentA > packC)                          //If the payment was greater than the cost of package C
        {
            cout << "You would save $" << savingsOnPackC << " if you upgraded to Package C." << endl; //Then display savings
        }

        else
        {
            if(paymentA > packA)                        //If the payment was greater than the cost of package A
            {
                cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
            }
        }
    }

}


void savingsWithB()                                 //Function Definition 5
{
    float paymentB = ((hours - 20) * 1) + packB;
    float savingsOnPackC = paymentB - packC;
    if(package == 'B')                              //If package was B
    {
        if(paymentB > packC)                        //If payment was greater than the cost of package C
        {
            cout << "You would save $ " << savingsOnPackC << " if you upgraded to Package C." << endl; //Display savings
        }
    }

}


int main()                                          //Begin main program
{
    getPackage();                                   //Call functions
    while(package != 'A'|| 'B'|| 'C')          //While package entered is NOT A, B, or C
    {
        cout << "Please enter either A, B, or C." << endl;          //Prompt for A, B, or C
    }
    isPackageC();                                    //Otherwise, go on with the program
    getBill();
    savingsWithA();
    savingsWithB();
    return 0;                                         //Quit
}

I've corrected a few small problems in your code. I moved the while loop into the getPackage() function and set package input, to upper case, so both upper and lower cases have to be checked. Also, in savingsWithA() function, float savingsOnPackB = paymentA - packA;, was changed to float savingsOnPackB = paymentA - packB; Hope this helps you..
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Mailing Package.cpp : main project file.
//This program calculates a customer's monthly bill

#include <iostream>

using namespace std;

char package;                                       //Declaring variables
float hours;
double packA = 9.95;
double packB = 14.95;
double packC = 19.95;
double paymentA;
double paymentB;

/*
char getPackage();             //Declaring prototypes
void isPackageC();              //No need to declare, since functions NOT located below main()             
void getBill();
void savingsWithA();
void savingsWithB();
*/

char getPackage()                                   //Function Definition 1
{
	package = ' ';    
	while(package != 'A' && package != 'B' && package != 'C')  //While package entered is NOT A, B, or C
	{
		cout << "Which package have you purchased?" << endl   //Ask for package
			<< "Please enter A, B, or C and press the Enter key." << endl;
		cin  >> package;     //Read package
		package = toupper(package);
	}
	return package;
}

void isPackageC()                                 //Function Definition 2
{
	if(package == 'C')                              //If package is equal to C
	{
		cout << "Your payment is $" << packC << "." << endl;    //Then payment is automatically $19.95
	}
	else                                            //Or else, if package was NOT C,
	{
		cout << "How many hours were used during this billing cycle?" << endl   //Ask for hours
			<< "Please enter numerical amount and press the Enter key." << endl;
		cin  >> hours;                              //Read hours
	}
}

void getBill()                                      //Function Definition 3
{
	if(package == 'A')                              //If package is equal to A,
	{
		if(hours <= 10)                             //And if hours were greater or equal to 10
		{
			cout << "Your payment is $" << packA << "." << endl;    //Display payment of $9.95
		}
		else                                        //Or else,
		{
			paymentA = ((hours - 10) * 2) + packA;          //Payment equals that plus $2/additional hour
			cout << "Your payment is $" << paymentA << endl;         //Display payment
		}
	}
	else                                        //Or else,
	{
		if(package == 'B')                      //If package is B
		{
			if (hours <= 20)                    //And hours are less than or equal to 20
			{
				cout << "Your payment is $" << packB << "." << endl;    //Display payment of $14.95
			}
			else                                //Or else,
			{
				paymentB = ((hours - 20) * 1) + packB;           //Payment is that plus additional $1/hour
				cout << "Your payment is $" << paymentB << endl;         //Display payment
			}
		}
	}
}

void savingsWithA()                                 //Function Definition 4
{
	//paymentA = ((hours - 10) * 2) + packA;
	double savingsOnPackB = paymentA - packB;
	double savingsOnPackC = paymentA - packC;
	if (package == 'A')                             //If package is A
	{
		if(paymentA > packC)                          //If the payment was greater than the cost of package C
		{
			cout << "You would save $" << savingsOnPackC << " if you upgraded to Package C." << endl; //Then display savings
		}

		else
		{
			if(paymentA > packB)                        //If the payment was greater than the cost of package A
			{
				cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
			}
		}
	}

}


void savingsWithB()                                 //Function Definition 5
{
	//paymentB = ((hours - 20) * 1) + packB;
	double savingsOnPackC = paymentB - packC;
	if(package == 'B')                              //If package was B
	{
		if(paymentB > packC)                        //If payment was greater than the cost of package C
		{
			cout << "You would save $ " << savingsOnPackC << " if you upgraded to Package C." << endl; //Display savings
		}
	}

}


int main()                                          //Begin main program
{
	package = getPackage();  //Call functions
	isPackageC();  //Otherwise, go on with the program
	getBill();
	savingsWithA();
	savingsWithB();
	return 0;                                         //Quit
}
Thank you so much. In the car on the way to work, I realized my while loop had to move, and I was going to tackle the uppercase problem next. You read my mind. Thanks very much. A few questions if you don't mind, so I can better understand:
1) Why change the datatypes to double from float? Wouldn't floats have worked, or would they have given me a problem somewhere?
2) When typing functions, I see that you changed the getPackage from a 'void' to a 'char' so how do I know what those need to be? Is there a question I need to ask for every function to know which type it falls under?
3) In the getPackage function, your return statement says "return package." What exactly does that mean/do?
4) In the main program, the first function says "package = getPackage();" Why couldn't if have just read getPackage();? What does that first part do?

Okay, I'm sorry for all the questions, and if you don't get around to answering them, I'll figure them out.
Thanks a lot for your help, again.
:)
Okay, I'll see how well I can explain the answers to your questions.

1) I was getting a warning from my compiler. warning C4305: 'initializing' : truncation from 'double' to 'float' So, I changed them. No more warnings.
2) A void function means it doesn't get sent, or return, anything. A char is any one character. So, I was just requesting, from the function, a letter. As you see, though, not every function needs to return a variable, or receive one. So they can be void.
3) The char named package is sent to the getPackage function. The return package then gave the calling variable a value.
4) Yes, it could be written that way, and it does work. But I find it easier to understand what is happening in the program. By having "package = getPackage();", I know what variable is being used, and which will be changed.

I'm probably not to good at explaining my reasonings for doing things, so if you want, or need, better understanding of what I did, please ask. I promise to answer to the best of my ability
Your reasons are good enough for me. Thanks. And I'm actually online now studying up on how to put all this in an output file. I understand how to create one, but is there a way to keep appending it, for each time I run the program? Or could you point me to a tutorial webpage, perhaps , if you don't want to just tell me the answer?
This is the page I'm on now, but so far, nothing about appending an output file...
http://www.cplusplus.com/doc/tutorial/files/

Thanks so much! You really have been so helpful. (STILL no answer from my actual teacher. :( )
Never mind, I figured it out! Yay me! Thanks so much!
You're welcome. And I'm sure you are like me. "I love it when a plan comes together!". Best of luck on the rest of your projects, as well.
Topic archived. No new replies allowed.