- Using else -
But what if you want the program to respond to No answers as well? The you can use the else statement.
if(response = 'y' || response = 'Y)
cout << "\nYou answered Yes." << endl;
else << "\nYou didn't answer Yes!" << endl;
- Using else if -
When you want the program to respond to three or more possible conditions, use one or more else if statements after the if statement and before the else statement.
#include
using namespace std;
int main()
{
char response;
cout << "\nAre you human? (Enter Y for Yes or N for No): ";
cin >> response;
if(response == 'y' || response == 'Y')
cout << "\nYou answered No." << endl;
else if(response == 'n' || response == 'N')
cout << "\nYou answered No! You must not be human!" << endl;
else
cout << "\nPlease press Y for Yes or N for No." << endl;
cin.get(); cin.get();
return 0;
}
ASSIGNMENT #3
Lesson: Relational Operators and If Statements
Due Date: February 19
1.Write a GPA calculator program that asks for the numerical grades (0-4) for six classes and
then prints out the grade average for those classes.
2.Modify the above program so that it prints ?Uh-oh!? to the screen every time a grade
lower than 2 ( C ) is entered. Print -That's okay.- for a 2, -Good.- for 3, and -Great!-
for a 4.
3.Write a second program that asks the user for their pre-tax salary. Print the amount
of taxes the person owes according to the following rates:
10% tax if salary is less than $20,000
15% tax if between $20,000 and $55,000
20% tax if more than $55,000