Code looks good but wont run because of build errors?

I wrote a program for my class, and the IDE doesn't see anything wrong with my code but when I go to execute it tells me there are build errors and won't run it. Any help is appreciated!

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
//Phone Bill Program
#include <iostream>
using namespace std;

int main()
{
	char customer,				// User's plan type
		rerun;					// If the user wants to do more bills
	string phone;				// User's phone number
	int usage;					// User's phone usage
	const int STAGE_1_R = 0.55,	// For Regular stage 1 units
		STAGE_2_R = 0.88,		// For Regular stage 2 units
		STAGE_1_B = 0.45,		// For Business stage 1 units
		STAGE_2_B = 0.99;		// For Business stage 2 units
	double totalChargeR,		// For Regular total charge
		totalChargeB,			// For Business total charge
		stage1r,				// Total Regular stage 1 cost
		stage2r,				// Total Regular stage 2 cost
		stage1b,				// Total Business stage 1 cost
		stage2b;				// Total Business stage 2 cost

	do
	{
		cout << "Please enter the customer type (R for Regular, B for Business): ";
		cin >> customer;
		cout << "Please Enter the Customer Phone Number: ";
		cin >> phone;
		cout << "Enter the phone usage in minutes: ";
		cin >> usage;

		switch (customer == 'r' || customer == 'R' || customer == 'b' || customer == 'B')
		{
		case 'r':
			double monthlyBaseR = 29.99;
			cout << "\n\t\t\tPower Bill for: " << phone << endl;
			cout << "\t\t\t   = = = = = = = = = = =";

			cout << "\n\nTotal Minutes Used: " << usage;
			cout << "\nMonthly Base Payment: " << monthlyBaseR;

			stage1r = (STAGE_1_R * (usage - 200));
			cout << "\nCost for Stage 1 units: " << stage1r;

			stage2r = (STAGE_2_R * (usage - 450));
			cout << "\nCost for Stage 2 units: " << stage2r;

			totalChargeR = monthlyBaseR + stage1r + stage2r;
			cout << "Total Cost: " << totalChargeR;

			break;

		case 'b':
			double monthlyBaseB = 99.99;
			cout << "\n\t\t\tPower Bill for: " << phone << endl;
			cout << "\t\t\t   = = = = = = = = = = =";

			cout << "\n\nTotal Minutes Used: " << usage;
			cout << "\nMonthly Base Payment: " << monthlyBaseB;

			stage1r = (STAGE_1_B * (usage - 600));
			cout << "\nCost for Stage 1 units: " << stage1b;

			stage2r = (STAGE_2_R * (usage - 700));
			cout << "\nCost for Stage 2 units: " << stage2b;

			totalChargeB = monthlyBaseB + stage1b + stage2b;
			cout << "Total Cost: " << totalChargeB;

			break;

		default:
			cout << "\nYou may only enter R, r or B, b.\n";
		}
		cout << "\nMore bills? Y/N: ";
		cin >> rerun;
	} while (rerun == 'y' || rerun == 'Y');
	return 0;
}
your switch statement produces a boolean, your case statements test characters.

consider a format like:

switch(customer) //a character instead of bool
case 'b': //no break, will do the code for B too
case 'B':
code for bees;
break;
case 'r':
case 'R':
code for arrs
break;
default: ...


Last edited on
Hello morganniie,

In addition to what jonnin has said. What is between the () of a switch needs to be an integer value. Since a "char" is a type of "int" it works.

Just because you can type string phone; and the IDE or compiler will not complain about it when you get to cin >> phone; this is a problem because with out the "<string>" header file the "cin" does not know how to process it.

If yo define or try to use any "std::string" you need to include the "<string>" header file.

ALWAYS initialize all your variables.. Even if some get a value soon after they are defined it helps to know that the variable does have a garbage value. Especially if you send a variable to a function with out a proper value.

Next I found const int STAGE_1_R = 0.55. You define the variable as an "int", but set it equal to a double. You will only set the variable to (0)zero. The decimal portion will be dropped. Did you mean to make this a "double"?

In "case 'b'" you define monthlyBaseB. In order to use this the case statements need to be inside {}s to be able to define a variable. excluding the break statement. The Problem is that when you reach the closing } the variable(s) are destroyed, so if you need it outside of the switch it needs to be defined outside the switch.

In the future when your code does not compile try to fix all the errors and warnings that you can and post the complete error message for what you do not understand.

Andy
You define the variable as an "int", but set it equal to a double. You will only set the variable to (0)zero. The decimal portion will be dropped. Did you mean to make this a "double"?
If OP had used uniform initialization
const int STAGE_1_R { 0.55 }; // error
The result would have been an error message instead of incorrect code.
Last edited on
@mbozzi,

I will have to give that a test. I did know that the {}s do catch things that are improperly defined, but I have not had that problem to see it in action.

Thank you for the information.

Andy
Hello morganniie,

While testing your program I did by accident find a problem I did not see at first.

When I reached:
1
2
cout << "Please Enter the Customer Phone Number: ";
cin >> phone;

And I entered "201 555-1234" The space only allowed the formatted input to store the "201" in "phone" and then the "555" in "usage" leaving the rest in the buffer for what came next. That being the cin >> rerun which would not match a "y" or "Y", so the program would end and of course all your calculations would be off.

I do like your program though. When I entered "r" and types "100" for usage all the calculations came out negative numbers, so I guess that would mean that the company owes me money for not using the phone enough?

You may want to rethink these lines of code:
1
2
3
4
5
stage1r = (STAGE_1_R * (usage - 200));
cout << "\nCost for Stage 1 units: " << stage1r;

stage2r = (STAGE_2_R * (usage - 450));
cout << "\nCost for Stage 2 units: " << stage2r;

Maybe use an if statement: if (usage > 200). Otherwise you could end up with a negative number.

Just a thought for now. I have not worked on it yet.

Andy
@OP Here is a running version of what you had with a few changes:
1. tidied up your variable declarations and initialization.
2. used a complex while loops - they look complicated but really aren't if you read closely. The first one makes sure it is an r or b account - why bother going further if the user doesn't get that right?
3. rerun is placed at the start - while's are bad enough but do-whiles are worse
4. I haven't looked at the billing process/logic

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
#include <iostream>
using namespace std;

int main()
{
    char customer{}, rerun{'y'};
    string phone{};
    int usage{0};

    const int STAGE_1_R = 0.55,
    STAGE_2_R = 0.88,
    STAGE_1_B = 0.45,
    STAGE_2_B = 0.99; // UNUSED

    double totalChargeR{0.0};
    double totalChargeB{0.0};
    double stage1r{0.0};
    double stage2r{0.0};
    double stage1b{0.0};
    double stage2b{0.0};

    double monthlyBaseR{0.0};
    double monthlyBaseB{0.0};

    while (tolower(rerun) == 'y')
    {

        while(
              cout << "Please enter the customer type (R for Regular, B for Business): " and
              cin >> customer and
              ( ( tolower(customer) != 'r' ) and ( tolower(customer) != 'b') )
              )
        {cout << "Rr or Bb ONLY! Go again!\n";}

        cout << "Please Enter the Customer Phone Number: ";
        cin >> phone;

        cout << "Enter the phone usage in minutes: ";
        cin >> usage;

        switch (tolower(customer))
        {
            case 'r':
                monthlyBaseR = 29.99;
                cout << "\n\t\t\tPower Bill for: " << phone << endl;
                cout << "\t\t\t   = = = = = = = = = = =";

                cout << "\n\nTotal Minutes Used: " << usage;
                cout << "\nMonthly Base Payment: " << monthlyBaseR;

                stage1r = (STAGE_1_R * (usage - 200));
                cout << "\nCost for Stage 1 units: " << stage1r;

                stage2r = (STAGE_2_R * (usage - 450));
                cout << "\nCost for Stage 2 units: " << stage2r;

                totalChargeR = monthlyBaseR + stage1r + stage2r;
                cout << "Total Cost: " << totalChargeR << '\n';

                break;

            case 'b':
                monthlyBaseB = 99.99;
                cout << "\n\t\t\tPower Bill for: " << phone << endl;
                cout << "\t\t\t   = = = = = = = = = = =";

                cout << "\n\nTotal Minutes Used: " << usage;
                cout << "\nMonthly Base Payment: " << monthlyBaseB;

                stage1r = (STAGE_1_B * (usage - 600));
                cout << "\nCost for Stage 1 units: " << stage1b;

                stage2r = (STAGE_2_R * (usage - 700));
                cout << "\nCost for Stage 2 units: " << stage2b;

                totalChargeB = monthlyBaseB + stage1b + stage2b;
                cout << "Total Cost: " << totalChargeB << '\n';

                break;

            default:
                cout << "\nYou may only enter R, r or B, b.\n";
        }

        cout << "\nMore bills? Y/N: ";
        cin >> rerun;

    }
    return 0;
}


Please enter the customer type (R for Regular, B for Business): x
Rr or Bb ONLY! Go again!
Please enter the customer type (R for Regular, B for Business): M
Rr or Bb ONLY! Go again!
Please enter the customer type (R for Regular, B for Business): r
Please Enter the Customer Phone Number: 1234-5678
Enter the phone usage in minutes: 2356

			Power Bill for: 1234-5678
			   = = = = = = = = = = =

Total Minutes Used: 2356
Monthly Base Payment: 29.99
Cost for Stage 1 units: 0
Cost for Stage 2 units: 0Total Cost: 29.99

More bills? Y/N: y
Please enter the customer type (R for Regular, B for Business): b
Please Enter the Customer Phone Number: wer9087
Enter the phone usage in minutes: 7890

			Power Bill for: wer9087
			   = = = = = = = = = = =

Total Minutes Used: 7890
Monthly Base Payment: 99.99
Cost for Stage 1 units: 0
Cost for Stage 2 units: 0Total Cost: 99.99

More bills? Y/N: n
Program ended with exit code: 0
Thanks guys for all your input and help! After @againtry posted I tweaked it a little so the math is right and now I think it's ready!
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
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
	char customer{}, rerun{ 'y' };
	string phone{};
	int usage{ 0 };

	const double STAGE_1_R = 0.55,
		STAGE_2_R = 0.88,
		STAGE_1_B = 0.45,
		STAGE_2_B = 0.99;

	double totalChargeR{ 0.0 };
	double totalChargeB{ 0.0 };
	double stage1r{ 0.0 };
	double stage2r{ 0.0 };
	double stage1b{ 0.0 };
	double stage2b{ 0.0 };

	double monthlyBaseR{ 0.0 };
	double monthlyBaseB{ 0.0 };

	while (tolower(rerun) == 'y')
	{

		while (
			cout << "\nPlease enter the customer type (R for Regular, B for Business): " and
			cin >> customer and
			((tolower(customer) != 'r') and (tolower(customer) != 'b'))
			)
		{
			cout << "Please only enter R, r for Regular\n"
				 << "Or B, b for Business.";
		}

		cout << "\nPlease Enter the Customer Phone Number: ";
		cin >> phone;

		cout << "\nEnter the phone usage in minutes: ";
		cin >> usage;
		
		if (usage > 0)
		{
			switch (tolower(customer))
			{
			case 'r':
				monthlyBaseR = 29.99;
				cout << "\n\t\t\tPower Bill for: " << phone << endl;
				cout << "\t\t\t   = = = = = = = = = = =";

				cout << "\n\nTotal Minutes Used: " << usage;
				cout << "\nMonthly Base Payment: " << monthlyBaseR;
				if (usage >= 201 && usage <= 450)
				{
					stage1r = (STAGE_1_R * (usage - 200));
					stage2r = 0;

				}
				else if (usage >= 451)
				{
					stage1r = 137.5;
					stage2r = (STAGE_2_R * (usage - 450));
				}

				cout << "\nCost for Stage 1 units: " << stage1r;
				cout << "\nCost for Stage 2 units: " << stage2r;
				totalChargeR = monthlyBaseR + stage1r + stage2r;
				cout << "\nTotal Cost: " << totalChargeR << '\n';

				break;

			case 'b':
				monthlyBaseB = 99.99;
				cout << "\n\t\t\tPower Bill for: " << phone << endl;
				cout << "\t\t\t   = = = = = = = = = = =";

				cout << "\n\nTotal Minutes Used: " << usage;
				cout << "\nMonthly Base Payment: " << monthlyBaseB;
				if (usage >= 601 && usage <= 700)
				{
					stage1b = (STAGE_1_B * (usage - 600));
					stage2b = 0;
				}
				else if (usage >= 701)
				{
					stage1b = 45;
					stage2b = (STAGE_2_B * (usage - 700));
				}

				totalChargeB = monthlyBaseB + stage1b + stage2b;
				cout << "\nCost for Stage 1 units: " << stage1b;
				cout << "\nCost for Stage 2 units: " << stage2b;
				cout << "\nTotal Cost: " << totalChargeB << '\n';

				break;

			default:
				cout << "\nYou may only enter R, r or B, b.\n";
			}
			cout << "\nMore bills? Y/N: ";
			cin >> rerun;
		}
		else if (usage < 0)
		{
			cout << "Invalid input: Usage needs to be\n"
				<< "greater than zero.\n\n"
				<< "Restart the program? Y/N: ";
			cin >> rerun;
		}
	}
	return 0;
}
L8 You don't need to default initialise a std::string as the default constructor will do this.

The default initialise for int is 0 and for double 0.0 - so for these types there's no need to specify the value, just use {}

Note that the phone number can't contain any white-space chars (space, tab, newline).

If a non-integer number is entered for usage, then the input stream will enter fail state until the state is cleared ie for rerun which will fail etc. What if the user enters a decimal number for usage rather than an int?

The default value for the switch should never be reached as the input is first validated. Perhaps a more ominous error message here?

@OP
You might have noticed the repetition in the two cases. That repetition can be taken advantage of by using function(s) to produce the costings and then the report.
Registered users can post here. Sign in or register to post.