Reputation: 73
I apologize in advance for my noob question. My first ever experience with programming has been with my current course. On to the question,
Why is my code skipping the second while loop. When I choose p or P for my input, I'm still getting asked for the inputs pertaining to the first while loop and not the second. E.g minutes instead of day minutes/night minutes.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char service;
int number;
int minutes;
int dayMinutes;
int nightMinutes;
double bill;
double const REG_FEE = 10.00;
double const PREM_FEE = 25.00;
double const REG_MIN = 0.20;
double const PREM_DAY = 0.10;
double const PREM_NIGHT = 0.05;
cout << "Please enter your account number: ";
cin >> number;
cout << "Please enter your service type (regular or premium): ";
cin >> service;
while (service == 'r' || 'R')
{
cout << "How many minutes have been used for this service?: ";
cin >> minutes;
if (minutes < 50)
{
bill = REG_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
else
{
bill = ((minutes - 50) * REG_MIN) + REG_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
return 0;
}
while (service == 'p' || 'P')
{
cout << "How many minutes were used during the day?: ";
cin >> dayMinutes;
cout << "How many minutes were used during the night?: ";
cin >> nightMinutes;
if (dayMinutes > 75)
{
bill = ((dayMinutes - 75) * PREM_DAY) + PREM_FEE;
}
if (nightMinutes > 100)
{
bill = ((nightMinutes - 100) * PREM_NIGHT) + PREM_FEE;
bill = bill + PREM_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << dayMinutes + nightMinutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
else
{
cout << "You have entered an invalid service code.";
}
return 0;
}
return 0;
}
Upvotes: 1
Views: 86
Reputation: 76
Since every while only is executed once, it would be better to use
if (service == 'r' || service == 'R')
in stead of first while, and
if (service == 'p' || service == 'P')
in stead of second while.
The idea of while loop is to execute a block of sentences zero or more times.
Upvotes: 0
Reputation: 4395
No need to use while
loop as code will run only once and value of service
is not changing inside the loop.
As earlier all have already explained to use service == 'r' || service == 'R'
I would like to suggest you to use if
elseif
instead of while
if(service == 'r' || service == 'R')
{
// your code
}
else if(service == 'p' || service == 'P')
{
//you code
}
remove return 0;
inside these two conditions.
Use loop when you want to run a condition multiple times.
In case if you want to run it multiple time, then keep using while
loop and just remove return 0;
inside the loop.
Upvotes: 0
Reputation: 1051
Because your first while
's condition is always true
whatever input is.
while (service == 'r' || 'R')
is equilavent to:
while ( (service == 'r') || 'R') )
e.g, input 'p'
while ( ('p' == 'r') || 'R') )
while ( (false || 'R') )
while ( (false || true) ) // 'R' 's equivalent decimal is greater
// than 0, so is always 'true'
while (true)
possible solution:
while ( service == 'r' || service == 'R' )
Upvotes: 0
Reputation: 1902
Your condition is logically incorrect. You need to change it to:
while (service == 'r' || service == 'R')
and same for the other loop:
while (service == 'p' || service == 'P')
Operator ==
has higher precedence than operator ||
.
What you have written is this:
while (service == 'r' || 'R')
So, in your case, the expression service == 'r'
is evaluated first.
Assuming the value of service
is 'p'
, the expression evaluates to false
.
Next, the expression false || 'R'
is evaluated which is always true
, because the character R
evaluates to true
.
So the condition is always true
and you always enter the first loop.
And the second loop has the same problem.
Upvotes: 0
Reputation: 308
As @Vaughn Cato said, you have a return 0;
in your code that is not part of a conditional statement. I am assuming that you might need to insert the return statement into one of your if/else statements.
My best choice would be instead to use a break;
statement instead of a return 0;
to only exit the first loop.
Upvotes: 2