What I want the program to do is to only choose one of the three if/else statement.
If the user inputs A, I want the program to pick the if/else statement that belongs to A, and then stop. I don't want it to show me the results for the B if/else statement, or the C if/else statement.
if (p = 'A' || 'a' && h<=50)
{
cout<< "Your total for this month is: $"<< base_A << endl<<endl;
}
else
{
cout<< "Your total for this month is: $" << (2*(h-50))+base_A << endl<<endl;
}
if (p = 'B' || 'b' && h<=100)
{
cout<< "Your total for this month is: $"<< base_B << endl<<endl;
}
else
{
cout<< "Your total for this month is: $"<< (1.5*(h-100))+base_B << endl<<endl;
}
if (p = 'C' || 'c' && h<=150)
{
cout<< "Your total for this month is: $" << base_C << endl<<endl;
}
else
{
cout<< "Your total for this month is: $" << (1*(h-150))+base_C << endl<<endl;
}
You have some problems with your if statement conditions; you need to use == for equality comparison as = does assignment.
Also, conditions like p = 'A' || 'a' && h<=50 don't work the way you think. That has three parts: p = 'A' (which as stated above is assignment, and should be ==) 'a' (which is always true as the ASCII code for 'a' is non-zero) h<=50
You need to explicitly write out the part involving 'a' with p == in front.
// convert to uppercase whatever
// is in p so it doesn't matter
// what the user enters :)
p = toupper(p);
switch (p)
{
case'A':
if (h <= 50)
cout << "Your total for this month is: $" << base_A << endl << endl;
else
cout << "Your total for this month is: $" << (2 * (h - 50)) + base_A << endl << endl;
break;
case'B':
if (h <= 100)
cout << "Your total for this month is: $" << base_B << endl << endl;
else
cout << "Your total for this month is: $" << (2 * (h - 50)) + base_B << endl << endl;
break;
case'C':
if (h <= 150)
cout << "Your total for this month is: $" << base_C << endl << endl;
else
cout << "Your total for this month is: $" << (2 * (h - 50)) + base_C << endl << endl;
break;
}
Zhuge -- That doesn't fix my problem of the program using the if/else statement associated with the letter. It executes all the if/else statements, I only want it to execute the one that goes with the letter.
Softrix -- My teacher wants us to use if/else statements. We're not supposed to know what switch or break mean. Also, the program must recognize upper case 'A', and lower case 'a' as the same letter.
Your best course of action is to try to implement Zhuge's suggestions and then post your modified code again. Then we can help you make further adjustments to your code.
if (p == 'A' || p == 'a') {
if (h <= 50)
cout<< "Your total for this month is: $"<< base_A << endl<<endl;
else
cout<< "Your total for this month is: $" << (2*(h-50))+base_A << endl<<endl;
}
elseif (...) { ... }
...
if (p = 'A' || 'a' && h<=50)
{
cout<< "Your total for this month is: $"<< base_A << endl<<endl;
}
else
{
cout<< "Your total for this month is: $" << (2*(h-50))+base_A << endl<<endl;
}
if (p = 'B' || 'b' && h<=100)
{
cout<< "Your total for this month is: $"<< base_B << endl<<endl;
}
else
{
cout<< "Your total for this month is: $"<< (1.5*(h-100))+base_B << endl<<endl;
}
if (p = 'C' || 'c' && h<=150)
{
cout<< "Your total for this month is: $" << base_C << endl<<endl;
}
else
{
cout<< "Your total for this month is: $" << (1*(h-150))+base_C << endl<<endl;
}