C++: how to input 0000 as and ID number?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
include<iostream>
using namespace std;
int main()
{

int id, grade_1, grade_2, grade_3, average;
cout<<"Please enter your ID:\n";
cin>>id;//example 0001 or 0000 or 0912
cout<<"Please enter your THREE grades:\n";
cin>>grade_1>>grade_2>>grade_3;
average = (grade_1 + grade_2 + grade_3)/3;
cout<<"My ID number is: "<<id<<endl
<<"My three grades are: "<<grade_1<<" "<<grade_2<<" "<<grade_3<<endl
<<"The average of the three grades is: "<<average<<endl;
system("PAUSE");
return 0;
}
Make id a string.
As mochops has said, if you want to ensure that the user enters a 4 digit id, even with leading zeroes, you'll need to make id a string (as least for input).

You might want to check the length of the i/p string and use isdigit() to check that the id entered was in the correct format.

And while you could convert it to an int to make processing easier, you would then have to remember to add the 0s when you display it.

Andy
Last edited on
Topic archived. No new replies allowed.