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!
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: ...
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 constint 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.
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.
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.
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?
@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
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
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.