Thread: terminating input

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    terminating input

    howdy again..
    How would I make it so a user's input ability is terminated upon entering nothing into a field, here's what the output would look like:
    student#1:
    What's your name?
    *user hits enter with no text*
    Bad input, terminating...
    *program goes onto more stuff...*

    But if the user enters good input, this is what happens:
    What's your name?
    Blah
    What's your hobby?
    Blah

    Here's what I have so far (there's more code...this is just a snippet):
    Code:
      int getinfo(student pa[], int n)
    {
        student temp;
        int i;
        for (i = 0; i < n; i++)
        {
            cout << "Student #" << (i + 1) << ":\n";
            cout << "What's your name?\n";
            cin.get(temp.fullname, SLEN).get();
            if (!cin)
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
                cout << "Bad input, terminating...\n";
                break;
            }
            else
                pa[i].fullname = temp.fullname;
            
            cout << "\nWhat's your hobby?\n";
            cin.get(pa[i].hobby, SLEN).get();
    Now that only checks for wrong input...can that be converted or am I just not doing it right in the first place? Any answers would be awesome...thanks a lot everyone -Chap

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think this statement is allowing your user to get away with just hitting 'enter'

    Code:
    while (cin.get() != '\n')
                    continue;

    I think there should be some sort of consquence in this scenario instead of 'continue'



    Edit: I'm thinking, changing the condition of your IF

    Code:
    if (!cin  || temp.fullname == "\n")
    Last edited by The Brain; 10-07-2004 at 05:37 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. Writing past ...
    By Ana Val sazi in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 08:43 AM