Thread: just a quick problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    just a quick problem

    alright so in my program, the user is prompted 3 times to enter an integer value. my problem is, i don't know how to make it so that if the user inputs say a letter for instance, the program won't screw up. is there a way i can avoid that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use fgets() to read the input as a string, then validate and convert it.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What i/o lib do you use?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    i use iostream.h.

    ok so the program is simple: to calculate percentages of the gross profit of a movie company. the user is supposed to input the amount of tickets sold for seniors, adults and juniors. i have the program working properly, but i want to make it so that if the user inputs a letter or something instead of a number for the amount of tickets sold that the program will say "please enter a number" or something along those lines and then prompt the user again.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You have the
    Code:
     cin>>integer;
    After that write this
    Code:
     
    if(cin.fail()) 
    { 
          cin.clear();      
          cin.ignore(500,'\n');
         //Asking for integer again, etc.
    }
    Last edited by siavoshkc; 01-24-2006 at 10:21 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i use iostream.h.

    That is an old, non-standard header that doesn't work on many modern compilers. Consider using iostream instead.

    Either way, siavoshkc's code is good, although I would make it a while loop and place the read directly into the control: while (!(cin >> integer)). That way it will keep prompting for a number until a number is pressed.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Daved, you mean this?
    Code:
     
    using namespace std;
    #include <iostream>
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I meant <iostream> like you have it, yes. You would normally put the using directive after the #include, and of course you don't need the using directive if you add std:: to the name of the stuff from the standard library (which is how I prefer to do it).

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    alright so i used the cin.ignore and it's better now because it doesn't give a random value anymore, but now i need it to reprompt the user instead of just skipping the prompt and going to the next.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> now i need it to reprompt the user instead of just skipping the prompt and going to the next.

    See my first post above. Use the read as the control of a while loop.
    Code:
    while (!(cin >> integer))
    {
          cin.clear();      
          cin.ignore(500,'\n');
          // Re-prompt here.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM