• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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?
0
jandhb
Asked:
jandhb
  • 4
  • 2
  • 2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now