validating user input

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?
LVL 1
jandhbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

efnCommented:
It would help to convert it AFTER reading it, instead of before.
0
jandhbAuthor Commented:
please show me what you mean.
0
jandhbAuthor Commented:
i dont understand what your saying. can you show me?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

efnCommented:
    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
itsmeandnobodyelseCommented:
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
jandhbAuthor Commented:
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
itsmeandnobodyelseCommented:
>> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jandhbAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.