function validation

closed account (2Lyv0pDG)
i am writing a program that calculates the sum of three numbers. i understand that part. however i must validate my code in such a way that i could only enter an integer not a character. i heard there is a function that i could use. i would greatly appreciate some form of help.
Last edited on
You can try this if you want

1
2
3
4
5
6
7
8
9
10
11
12
13
int i;   
cout << "What is the score of the student? "; //Get score
        

           //Ensure that score is actually an integer
           while (!(cin >> i))
           {
		   cin.clear();
		   cin.ignore(1000,'\n');
		   //If not an integer, repeat until so
		   cout << "What is the score of the student (integer value's only): ";
           }
           score[pk] = i; //If so.. Put score in here 


This is off a program I wrote the full source is here if this doesn't help..

http://www.cplusplus.com/forum/beginner/6076/#msg27743

All it does is say while cin is not going into i (because its an integer)... do SOMETHING until cin can put information into ... When finally true.. then (i was workin on something) score[pk] will equal i..
Last edited on
Please consider reading the following posts from OP before replying here. OP is clearly looking to have the code written for him/her.

http://www.cplusplus.com/forum/unices/6252/
http://www.cplusplus.com/forum/unices/6243/
Topic archived. No new replies allowed.