Results 1 to 3 of 3

Thread: Testing input data

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    California
    Posts
    25

    Testing input data

    This is so simple I am embarrassed. Well not really being a first semester C++ student.

    What is the code to perform the following:

    I want to test 'answer' to be sure it contains ONLY digits....
    Any help will greatly be appreciated!!
    Thank you



    Ask user to input a value
    //start of snippet
    int answer;
    cout << "What is the answer? ";
    cin >> answer;
    //end of snippet

  2. #2
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Code:
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cctype>
    using namespace std;
    
    int main()
    {
        string answer;
        cout << "What is the answer? "; 
        cin >> answer;
    
        for (int i = 0; i < answer.length(); i++)
        {
            if (!isdigit(answer[i])
            {
                cout << "Invalid answer" << endl;
                break;
            }
        }
    Alcohol & calculus don't mix.
    Never drink & derive.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    California
    Posts
    25

    Thumbs up Re: Testing Input Data

    Thank you very much!

    Your assistance is appreciated...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width