Hi,
I am doing a part time degree and need some help to finish off some code for my first assignment in C++. I have written a program which checks some inputted int's and strings, validates them and outputs the result. The last thing I need to do to finish my program below is to have my CIN command check to see if there are more than 5 items (3 int's and 2 strings) entered onto the single input line, and if so, output the an error "Invalid Input". I don't know how to use a loop or anything else to check if more than 5 items have been input on my cin line. For example, 2 + 2 = 4 is a valid input, but 2 + 2 = 3 + 1 should read as invalid. Here is my code:
Any help appreciated.Code:#include <iostream> #include <string> using namespace std; int main() { int a = 0; int b = 0; int total = 0; string oper1, oper2; cout << "Please key in your equation." << endl; cin >> a >> oper1 >> b >> oper2 >> total; if (cin.fail()) { cout << "Invalid input" << endl; exit(1); } else finished = true; if (oper1 == "+") { if (oper2 == "=") if (a + b == total) cout << "Correct" << endl; else cout << "Incorrect" << endl; else if (oper2 != "=") cout << "Invalid input" << endl; } else if (oper1 == "-") { if (oper2 == "=") if (a - b == total) cout << "Correct" << endl; else cout << "Incorrect" << endl; else if (oper2 != "=") cout << "Invalid input" << endl; } else if (oper1 != "+" || oper1 != "-") cout << "Invalid input" << endl; cout << endl << "a = " << a << endl; cout << "oper1 = " << oper1 << endl; cout << "b = " << b << endl; cout << "oper2 = " << oper2 << endl; cout << "total = " << total << endl; cout << endl << "The equation you keyed in was: " << a << oper1 << b << oper2 << total << endl; }
Kam



LinkBack URL
About LinkBacks



