Checking Input Buffer...

This is a discussion on Checking Input Buffer... within the C++ Programming forums, part of the General Programming Boards category; ok so i got a question... again lol big suprise ok so heres the code i got so far Code: ...

  1. #1
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52

    Checking Input Buffer...

    ok so i got a question... again lol big suprise

    ok so heres the code i got so far

    Code:
    //Written By : Andrew Bomstad
    //"Day6"
    
    #include<iostream>
    #include<ctype.h>
    
    #define cls system("cls");
    #define pause system("pause");
    
    using namespace std;
    
    int main()
    {
        
        int input;
        cout << "enter a digit" << endl;
        cin>>input;
        cout<<cin;
        pause;
        if(cin){
                cls;
                cout << "You Entered The Number: " << input<<endl;
                pause;
                cls;
                main();
                }
             else{
                   cls;
                   cout << "Error! Must Enter A Number" << endl;
                   pause;
                   main();}
             
        pause;
        main();
    
    }
    ok... the next part i have NO idea how to go about....
    i want a way to send a error message out, if the user inputs more than one input... like say if they type "3hgh" or somthing i want an error message

    so im thinkin... do i need to check the keyboard buffer to see how much input was enterd and then do somthing bout that? i dont know really im confused with it all lol

    anyhelp would be great guys

    o yeah... im also looking for a way to get out of the Infinite loop you'll get if you hit a "Char" in my program. lol i tried clearing the buffer before sending it back to main.. but it just keeps erroring

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    for your second question you need to both clear it and then ignore any input left on stream like

    std::cin.clear(); //clears failed stream error
    std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n'); //ignore any characters left in stream

    for your first question, I am not sure what you are saying, you currently have an error message, you just need to add the above to it. If you are wanting to ouptput what they typed as part of the error, instead of ignore you could do a cin > string_variable, but I would still do the ignore after just to make sure it's fully cleared.

    hmmm...I just looked at your code again... you really shouldn't be calling main() from within main... you should instead try and create a loop to keep your program running.
    Last edited by Darryl; 04-12-2005 at 05:03 PM.

  3. #3
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    ok wait no..... now i see my problem...if you type "6tk" it'll still just pick up the 6 and ignore the other two... i want it to error if it picks up any "garbage" input like that

  4. #4
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Here's a simple loop you can use:

    Code:
    while(!cin)
    {
         cin.clear();
         cin.ignore();
         cout<<"ERROR! You must enter a number! ";
         cin>>input;
    }
    
    cout<<"You entered the number "<<input<<end;
    Also, since you are using 'define', there will be a direct code substitution, so I don't think the ; is needed after 'cls' or 'pause' in your code.

    And as already mentioned, don't recall main. Use a loop.
    Last edited by homeyg; 04-12-2005 at 05:31 PM.

  5. #5
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    yeah the while loop cleans it up quite a bit,
    Code:
    //Written By : Andrew Bomstad
    //"Day6"
    
    #include<iostream>
    #include<ctype.h>
    
    #define cls system("cls");
    #define pause system("pause");
    
    using namespace std;
    
    int main()
    {
        cls
        int input;
        cout << "enter a digit : ";
        cin>>input;
        while(!cin)
        {
         cls
         cin.clear();
         cin.ignore();
         cout<<"Please enter a number: ";
         cin>>input;
         }
    
         cout<<"You entered the number "<<input<<endl;
         pause
         main();
    
    }
    but it still allows the user to hit "6gh" and sill still pick up the int value, i want it to void the user's input if they hit Chars at All

  6. #6
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I'm not sure that's possible, and if it is, I don't know how to do it. I don't think you can look ahead in the input buffer.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    ok, then the next step is to not input int, but input strings insteads, this way you won't get the failing stream on char inputs and you'll be able to test the whole input before passing it on.

    Code:
    //Written By : Andrew Bomstad and Darryl :-)
    //"Day6"
    
    #include <iostream>
    #include <cctype> // prefer cctype over ctype.h
    #include <string> 
    
    #define cls system("cls");
    #define pause system("pause"); // ok for newbie but ditch it with experience
    
    using namespace std;
    
    int main()
    {
    	while (true)
    	{
    		cls
    			string input; // c++ strings are your friend.
    		bool valid_input;
    		do
    		{
    			cout << "enter a digit : ";
    			cin >> input;
    			// don't need the cin check now
    			for (int i = 0; i <input.size();++i)
    			{
    				valid_input = true;
    				if (!isdigit(input[i]))
    				{
    					cout<<"Error: Non-numeric data entered\n";
    					valid_input = false;
    					break;
    				}
    			}
    		}while (!valid_input);
    
    		cout<<"You entered the number "<<input<<endl;
    		pause
    	}
    }

  8. #8
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    Hey Thanks Alot Darryl, Thats Exactly What I was after, i was just getting to using isdigit() but i couldnt get that stomped out, but again thanks for the help!

  9. #9
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    hey i got one more question, i just need to figure out whats going on, in the "isdigit(); statement, like i said ive been trying to figure it out latley and obviously you know whats going on

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by AndyBomstad
    hey i got one more question, i just need to figure out whats going on, in the "isdigit(); statement, like i said ive been trying to figure it out latley and obviously you know whats going on
    isdigit() takes a char as its parameter and determines if that is a digit and return true if it is and false if it's not. I imagine it just does a simple test if (char > '0' && char < '9' )

    Since we were working with strings I had to loop through the string checking each char 1 at a time. In a string using the index operator[] will return a single char for that position.

    string test = "12345A";
    cout << test[2] ; //outputs '3'
    cout << isdigits( test[5]); // outputs false, but cout will convert to 0;

    I was thinking of using atoi() but it still would have converted a string like "65dfs" to just 65

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Checking if buffer is completely empty...
    By cookie in forum C Programming
    Replies: 8
    Last Post: 07-13-2008, 04:45 AM
  3. Input Buffer
    By Padawan in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2004, 07:12 PM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM

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