How to ignore extra data inputted by user

I can't believe how long I've spent on this... Problem asks for user to input a 6 digit account # and only a 6 digit account #. I assume my teacher is going to attempt to put in more than 6 digits.

I've tried to prevent that by telling the user to only input one digit at a time, and verifying/forcing it to be a digit. But the user can still input more than one digit at a time. And if the user ends up inserting more than 7 digits, then the program automatically has an input stored for the next cin.

Any suggestions on how I can force the user to only input one character at a time, or a total of 6 characters? Or just a way to erase whitespace garbage that the user inputs?

My brain is pretty fried on this one. Also, we havent learned strings yet, so I don't think I am allowed to include the cstring library.

Please help.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
          do {

                char acct[7];
                cout<<"Enter your 6 Digit Account #: "<<endl;
                //Originally I had this as (i=1;i<7;i++) and I would always 
                //get a 'P' appearing in char acct. Why is that??????
                for (int i=0;i<6;i++) {
                    //Prompts user for which digit it wants.
                    cout<<"Enter Digit "<<i+1<<":"<<endl;
                    cin>>acct[i];
                        if(acct[i]<'0'||acct[i]>'9'){
                            cout<<"Error"<<endl;
                            i--;
                        }
                    }
                cout<<"Account #: "<<acct<<endl;




            cout<<endl<<endl;
            cout<<"Would you like to run again? N for No."<<endl;
            cout<<"Press any other key to run again."<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem"<<endl;break;

read in the whole line as a string, then just do checking to make sure it is correct.

or flush the buffer

or unbuffered input
How do I "flush the buffer" ??
How do I "flush the buffer" ??

You should discard characters instead after you get all the digits.
#include <limits> and add this after reading all the input :
cin.ignore( numeric_limits<streamsize>::max(), '\n' );


If you do not want to prompt for every digit, u need to use cin.get() and assign the returned value to acct[i] like ( acct[i] = cin.get() ) then check if it
is a digit.

something like :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <limits>
using namespace std;

bool isDigit( char c )
{
    return( c >= '0' && c <= '9' );
}

int main()
{
    char acct[7];
    int i = 0;

    bool success = true;

    // get 6 characters from the input buffer
    // or until a non-digit is encountered
    while( i < 6 && (acct[i] = cin.get()) ) {
        if( !isDigit(acct[i]) ) {
            success = false;
            break;
        }
        ++i;
    }

    if( !success ) {
        cout << "Invalid Account Number Entered.";
    }

    // discard characters until a newline is found
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );

    // codes ...
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>
#include <cctype>

int main()
{
    unsigned int acct ;
    
    // std::cout << "accout number (exactly six digits): " ;
    
    if( std::cin >> acct )
    {
        if( acct < 100000 )
            std::cout << "*** error: less than six digits were entered\n" ;
    
        else if( acct > 999999 ) 
        {
            while( acct > 999999 ) acct /= 10 ;
            std::cout << "* alert: more than 6 digits were entered. trailing digits are discarded."
                      << " account number: " << acct << '\n' ;
        }
        
        else std::cout << "well done! you entered exactly six digits. account number: " << acct << '\n' ;
    }
    
    else std::cout << "*** error: input is not numeric\n" ;
    
    std::cin.ignore( 10000, '\n' ) ; // throw away everything left in that line
}

http://coliru.stacked-crooked.com/a/dcf5ec41b1d00e2c
Topic archived. No new replies allowed.