I'm stuck on just my last bit of code with this problem, its a basic cell phone bill program that computes your bill depending on what type of service you have, how many minutes you've used, and what time of day you used them in the case of premium service. The "Regular Service" section is working just fine but I'm having an issue in the if and else section in the "Premium service" section, it's spitting back the wrong answer and it looks like it's using the formulas from the regular service section for some reason. Any and all help would be greatly appreciated and I need to have this figured out ASAP!
When you have an else, you should encase everything you want printed or computed, inside of curly brackets. Such as your code here, and others like it..
Gotcha, I've been confused about this because in my text book there are several examples that don't include the {} in the else's for some reason. But it seems like I'm still having issues with the second section even when I've closed off all the else parts in both :[
cout << "Enter the number of minutes used during the day (6AM - 6PM): ";
cin >> numofminsDay;
cout << endl;
cout << "Enter the number of minutes used during the night (6PM - 6AM): ";
cin >> numofminsDay;
cout << endl;
You're using the same variable for both. You should name each one differently, then use numofminsDay to add both of them together, to get the full minutes used.
When you do not have the braces, only the next instruction is part of the if statement.
1 2 3 4
if(this_is_true)
cout << "Yay, I am true."; //Part of if statement
++var; //This line is outside the statement
do_something(); //This too
To have a multi-line if body, use the curly braces (or use them all the time)
1 2 3 4 5
if(this_is_true){
cout << "Yay, I am true."; //Part of if statement
++var; //Now this line is also part of the if statement
}
do_something(); //This is still outside
This also goes for elseif, else, and other structures such as loops.