Hi there, I'm new to the site and new to learning C++.
The program I am writing is for a C++ college class, and I'm just having a little trouble on if I am formatting the if statements I am using correctly. I'll give a breakdown of the assignment. I have to get an input of either A(ddition), S(ubtraction), D(ivide), or M(ultiply) and then have the user enter two integers then perform the expressed operation based on what letter they entered. If the user inputs a letter other than a, s, d, or m, it gives off an error message.
So far my program works if I take out anything but the first nested if statement.
//Ch6AppE08.cpp
//Displays the answer to arithmetic problems
//Created/revised by Greg Schader on 6/22/2009
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
//Define variables
char function = ' ';
int firstNum = 0;
int secondNum = 0;
int answer = 0;
//Enter variables and perform operation
cout << "Enter the letter of the operation you wish to perform: ";
cin >> function;
if (toupper(function) != 'A' && toupper(function) != 'S' && toupper(function) != 'D' && toupper(function) != 'M')
cout << "That letter is not valid." << endl;
else
{
if (toupper(function) == 'A')
cout << "Enter the first number of the operation: ";
cin >> firstNum;
cout << "Enter the second number of the operation: ";
cin >> secondNum;
answer = firstNum + secondNum;
cout << "Answer of operation is: " << answer << endl;
else
if (toupper(function) == 'S')
cout << "Enter the first number of the operation: ";
cin >> firstNum;
cout << "Enter the second number of the operation: ";
cin >> secondNum;
answer = firstNum - secondNum;
cout << "Answer of operation is: " << answer << endl;
else
if (toupper(function) == 'M')
cout << "Enter the first number of the operation: ";
cin >> firstNum;
cout << "Enter the second number of the operation: ";
cin >> secondNum;
answer = firstNum * secondNum;
cout << "Answer of operation is: " << answer << endl;
else
if (toupper(function) == 'D'){
cout << "Enter the first number of the operation: ";
cin >> firstNum;
cout << "Enter the second number of the operation: ";
cin >> secondNum;
answer = firstNum / secondNum;
cout << "Answer of operation is: " << answer << endl;
}
}
return 0;
} //end of main function