CIN Input count question?

This is a discussion on CIN Input count question? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am doing a part time degree and need some help to finish off some code for my first ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Post CIN Input count question?

    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:

    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

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Miami, FL
    Posts
    6
    It might be easier to use cin.getline(); and then parse the line after you read it. Using getline you would know it would read the entire line (up to any '\n' i think). It would read it in as an array of chars instead of a string but you could use something like this.

    Code:
    char input[201];
    cin.getline(input, 200);
    string str = new string(input);
    then the string str would contain all the input and you could use all the features of c++ strings to extract the information. Like find and substr. I'm not sure if this is what you're looking. But it's a pretty easy way read in a whole line, that's why i thought it might help.

    http://www.cplusplus.com/ref/iostrea...m/getline.html
    http://www.cppreference.com/cppstring/index.html

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    This same question (exactly) was asked recently. Just attempt to read in a single character using cin.get() after you've read in the initial equation. If it returns a newline ('\n') then the input is valid. Otherwise, there are extra characters in the expression and the input is invalid.

    Using getline could work, but it isn't unnecessary for this simple enhancement. Although you could use getline instead of get and check for an empty string after you've read in the initial equation.

    UMTopSpinC7, if you are using strings, you just use the string version of getline, not the C-style version. Also, there's no reason to use new in your code example.
    Last edited by Daved; 10-24-2006 at 09:43 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Thumbs up

    "This same question (exactly) was asked recently."

    Must have been someone else from my class on this forum!

    Daved, you are a legend. I have been trying various things for the past few days, and I new there was a way, but with my limited knowledge didn't know what. There had to be a better, more efficient way to do this rather than using a string input for the equation line and breaking that down and doing int conversions etc.

    Just to clarify if I understand this correctly, cin.get() reads the next character from the stdin, in my case, for eg. if 2 + 5 = 7 + 4 was input. In my program, there are only enough variables to read up to 7, so cin.get() would then contain + from the buffer, not a \n.

    Here is the code I included to my program below the if (cin.fail()) statement to make it work.

    Code:
    else if (cin.get() != '\n')
    		{
    			cout << "Invalid input" << endl;
    			exit(1);
    		}
    Thanks for all your help guys, I will definetly be using this forum more as my course progresses!

    Kam

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Technically, cin.get() would grab the space ' ' from after the 7. The get() function is unformatted, so it does not skip whitespace like operator>>. If the user input "2 + 5 = 7 " (note the space at the end) then your code would print "Invalid input". I would assume that is ok.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Yep, that is absolutely OK. Glad I understand how that works now. Many thanks again.

    Kam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. simple cin question
    By verbity in forum C++ Programming
    Replies: 25
    Last Post: 05-09-2007, 03:02 PM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  4. cin question....
    By Alien_Freak in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 10:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21