none
New to C++ , How to add check if user inputs string or char instead of int RRS feed

  • Question

  • I want to add the check-in my c++ code that user can not enter not integral values in reg. If he inputs, he is prompted again. I saw the solutions in the stack overflow that was of <g class="gr_ gr_37 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="37" id="37">2011 .</g> Is there some modern or good way now or is it same?

    I tried using <g class="gr_ gr_34 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="34" id="34">fdigit</g>() in <ctype.h

    // Example program
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    int main()
    {
        int x;
        cout<<"Type X";
        cin>>x;
        if(!isdigit(x))
        {
         cout<<"Type Again";
         cin>>x;
        }
    }

    but it <g class="gr_ gr_35 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="35" id="35">didnt</g> worked

    here is my actual problem where I want to add check

     cout << "Type Reg # of Student # " << i + 1 << endl;
     do
     {
          cin >> arr[i][j];
     } while (arr[i][j] < 999 || arr[i][j] > 9999);

    where i and j are int dec. in for loop. I just want to add check that input is not <g class="gr_ gr_44 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" data-gr-id="44" id="44">string</g> or something like this.


    • Edited by Ahmad123a Saturday, September 14, 2019 6:36 PM Showing <g> tags
    Saturday, September 14, 2019 6:35 PM

All replies

  • I want to add the check-in my c++ code that user can not enter not integral values in reg. If he inputs, he is prompted again. 

    here is my actual problem where I want to add check

    Since this appears to be school work, our replies should be in the form of
    general advice and examples rather than coded solutions for the assignment.

    There are many different ways to code a solution for this problem. Things to
    keep in mind:

    - Detect invalid input where the first character isn't a digit, a sign (+ or -),
      or a space.  e.g. - X123,  .789, $123
      (Leading spaces will be skipped by default.)

    - Detect when the first character (and possibly following) is numeric, but
      there are non-numeric characters after the numeric prefix. e.g. - 123.45
      
    Here's one approach:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        int x;
        string str;
        cout << "Type X ";
        while(true)
        {
            cin >> x;
            if (cin.fail()) // 1st character of input not a digit or a sign
            {
                cin.clear(); // clear the fail bit
                getline(cin, str); // flush the stream
                cout << "Type Again ";
                continue;
            }
    
            if (cin.peek() != '\n') // if characters other than newline left in stream
            {            
                getline(cin, str); // flush the stream
                cout << "Type Again ";
            }
            else break;
       }
        cout << "You entered " << x << endl;
        return 0;
    }
    

    - Wayne

    Sunday, September 15, 2019 5:36 AM