CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1

    Structs, cin.get and input failure...

    Can someone offer me some suggestions on how to detect input failure? I can find examples out the wazoo about how to clear an input stream when failure occurs:
    Code:
    cin.clear(200,'\n');
    cin.ignore();
    to show just one example... but I can not really find any kind of explanation of how to programatically detect input failure and correct it. I can see it quite obviously at runtime, but by then its a little too late to fix the issue...

    So here is my specific situation. I have a struct set up like this:
    Code:
    struct customer //struct to hold the customer data we need
    {
        int id;
        char name[25];
        char address[25];
        char city[15];
        char state[2];
        char zip[5];
        double balance;
    };
    and I want to add data... so I have something like this to add information to a new customer variable like this:
    Code:
    customer newcust;
    initStruct(newcust); //a function that fills newcust with blank data (whitespace for char[] and 0 for int and doubles)
    cout<<"Please enter the following:"<<endl;
        cout<<"Name     : "; cin.get(newcust.name,26); cin.ignore(); //.getline(newcust.name,26,'\n');
        cout<<"Address  : "; cin.get(newcust.address,26); cin.ignore(); //.getline(newcust.address,26,'\n');
        cout<<"City     : "; cin.get(newcust.city,16); cin.ignore();//.getline(newcust.city,16,'\n');
        cout<<"State    : "; cin.get(newcust.state,3); cin.ignore();//.getline(newcust.state,3,'\n');
        cout<<"Zip      : "; cin.get(newcust.zip,6); cin.ignore();//.getline(newcust.zip,6,'\n');
        cout<<"Current Balance: "; cin>>newcust.balance;
    the comment stuff after each cin.get (the .getline stuff) is the remains of something else I tried. THis works fine... UNTIL the user enters a longer string of characters than is supposed to go in. For example, newcust.state is supposed to be a char array of 2. As the code is written, the user can enter NC<enter> and it works fine. If the user enters NCA<enter> however, input failure occurs, and the trailing char gets dumped into the double newcust.balance, which just drives the program insane.

    So I was trying to find a not very long way (maybe something in boost libs??) that will check my user input for out of bounds stuff, or catch input failure when it occurs, or something to avoid the psychosis that the program currently suffers from when user input exceeds its expected bounds.

    I think I am just reading too far into it, so I need someone with a clear vision to point me in a direction. I was under the impression that the get would handle this, so that if I needed 5 chars, and I used
    Code:
    cin.get(newcust.zip,6);
    the program would read the input stream, take the first 5 chars and place them in newcust.zip and ignore everything else to the newline.

    Instead, what I am getting is it puts the first 5 chars into the newcust.zip then dumping the rest of the stream into the next get.

    In case it is necessary, I am attaching the cpp file with what I have so far. ITs ugly as I am trying a lot of things and havent cleaned any of it up yet...

    cheers
    Jeff
    Attached Files Attached Files
    Last edited by bladernr; March 5th, 2006 at 11:31 PM. Reason: fat fingered the keyboard and forgot to preview...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured