PDA

Click to See Complete Forum and Search --> : Testing input data


KMMATH
Apr 27th, 2002, 05:18 PM
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

Wynd
Apr 27th, 2002, 05:54 PM
#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;
}
}

KMMATH
Apr 28th, 2002, 12:04 PM
Thank you very much!

Your assistance is appreciated...