Solved

validating user input

Posted on 2004-10-27
205 Views
Last Modified: 2010-04-01
Can someone PLEASE show me how to accept only integer input from the user?

I have tried this...

      string s;
      int intVersion = atoi(s.c_str());

      do
      {
            cout << "Enter the Fibonacci number to compute: " << endl;
            cin >> s;
            if (intVersion == 0)
            {
                  cout << "Not an integer\n";
            }
            else
            {
                  cout << "Is an integer\n";
                  cout << intVersion;
            }
      }while(intVersion == 0);

But even when I enter 1 it says its not an integer. How do I do this?
0
Question by:jandhb
    8 Comments
     
    LVL 15

    Expert Comment

    by:efn
    It would help to convert it AFTER reading it, instead of before.
    0
     
    LVL 1

    Author Comment

    by:jandhb
    please show me what you mean.
    0
     
    LVL 1

    Author Comment

    by:jandhb
    i dont understand what your saying. can you show me?
    0
     
    LVL 15

    Expert Comment

    by:efn
        string s;

         do
         {
              cout << "Enter the Fibonacci number to compute: " << endl;
              cin >> s;
              int intVersion = atoi(s.c_str());
              if (intVersion == 0)

    The call to atoi should occur after s has acquired a value from the input stream.
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    Actually, atoi() isn't best choice to check numeric input as it accepts all string input and would give a non-zero return for "-123XT" or "7AA123". Better use strtol:

        string s;
        char* pstop;
       
         do
         {
              cout << "Enter the Fibonacci number to compute: " << endl;
              cin >> s;
              int intVersion = strtol(s.c_str(), &pstop, 10);
              if (*pstop != '\0')   // not a number (stopped before end)


    Regards, Alex

    0
     
    LVL 1

    Author Comment

    by:jandhb
    Alex,

    Is this what your saying....

                 do
         {
              cout << "Enter the Fibonacci number to compute: " << endl;
              cin >> s;
              int intVersion = strtol(s.c_str(), &pstop, 10);
              if (*pstop != '\0')   // not a number (stopped before end)
              {
                   cout << "Not an integer\n";
              }
              else
              {
                         cin >> intVersion;
              }
           }while(*pstop != '\0');
    0
     
    LVL 39

    Accepted Solution

    by:
    >> Is this what your saying....

    you would have to enter the (valid) number twice

           int intVersion = 0; // define outside of loop or the value is lost after loop
           char* pstop;
           // begin an infinite loop
           while (true)
           {
              cout << "Enter the Fibonacci number to compute: " << endl;
              cin >> s;
              intVersion = strtol(s.c_str(), &pstop, 10);
              if (*pstop == '\0')
                   break;     // break loop if valid number
              cout << "Not an integer\n";
           }
           
    Normally, i avoid using same condition twice as while condition and if condition. An alternative to that is using an input function

        bool getNumber(int& intVersion)
        {
              char* pstop;
              string s;
              cout << "Enter the Fibonacci number to compute: " << endl;
              cin >> s;
              intVersion = strtol(s.c_str(), &pstop, 10);
              return (*pstop == '\0'); // true if good number
         }

        int main()
        {
              int intVersion = 0;
              while (!getInput(intVersion))
              {
                       cout << "Not an integer\n";
              }
              return 0;
        }

    Looks great?

    Regards, Alex

               
    0
     
    LVL 1

    Author Comment

    by:jandhb
    Alex, again thank you for your help here.

    If you can I would like for you to take a look at my post here on Fibonacci number() as I need some help.

    http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21187247.html
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now