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:

#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;
}

Any help appreciated.

Kam

Re: CIN Input count question? 80 80

I would input everything info one big string then parse the string to find out how many words where are. If it contains the correct number of words then it can be broken into its individual parts.

Re: CIN Input count question? 80 80

I thought of doing that, but the problem I had was differentiating between the String and INT characters. My tutor told me we should be able to do this program with the little knowledge he imparted to us in the first 3 weeks of the course. How can I convert the string characters into ints to do my calculation?

Thanks,

Kam

Re: CIN Input count question? 80 80

Mybe something like:

string my_string = "1234" ;
long i = strtol( my_string.c_str() ) ; // long = 1234

Hope it helped, bye.

Re: CIN Input count question? 80 80

Thanks for the suggestions guys. I got a tip from someone on another forum to use cin.get(). This is the chunk I have added into my code below the cin.fail section:

else if (cin.get() != '\n')
        {
            cout << "Invalid input CIN.GET" << endl;
            exit(1);
        }

This has worked and works in line with the way I wanted to write the program. Thanks again, I will be using this forum as I'm learning C++ and am 4 weeks into the course!

Kam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.