Thanks for the reply. Any idea about how I can fix this? Should I use a string instead? If I use a string, I am not sure how to keep it in the 10000 to 99999 range.
EDIT: You'll notice that I didn't write complete code, well that's for a reason. The reason being is I don't want to help anyone cheat, but if you want to take the code above, run with it and make it great(?) then be my guest. Also there probley is a better way to go about this...
EDIT:EDIT: If it helps you sleep at night: "I'm your professor for [insert course name here] and If i see this exact code on my [assignment name here] then I will fail you so much you won't see the light of the sun for [http://www.googolplexian.com/] days.
and if your not in college than go ahead and continue on, civilian.
Almost, sargon94. But I would say, entering a new value for each line is an annoyance and you should never ask a customer to adhere to an interface that is not natural.
1 2 3 4 5 6 7 8 9 10 11
char accountNumber[5];
cout << "Enter a 5-digit number: ";
cin >> accountNumber;
// Just verify the first number is > 1
if(accountNumber[0] > 0) {
// input is valid
} else {
// input is invalid
} // end if
you dont really need to use chars, you can get the number of digits in an int by calculating num/10 + 1 and then to get rid of leading zeros loop through each digit until a non zero digit is found
You could read the input as a string, checking for size/leading zeros and range between("100000", input, "99999");
But if later you need to operate it as a number, you could peek at the input. if you see a '0', reject it. (instead of convert it)
string accNum; //where we will store the bank account number
getline(cin, accNum); //or if you prefer:
// cin >> accNum
bool isValid = accNum.length() <= 5; //the number is valid at first if the string's length makes sense
for( int i = 0; isValid && i < accNum.length(); i++ ) //loop through each character in the string as long as it's still a valid string
isValid = '0' <= accNum[i] && accNum[i] <= '9' ) //the '0'-'9' ascii values are in sequential order, so this check is easier then:
// isValid = accNum[i] == '0' || accNum[i] == '1' || ... || accNum[i] == '9'
/*
* If the string is still valid here then it was less then of equal to 5 characters in length,
* and each character was between '0' and '9'.
* If not then somewhere the string became marked as invalid.
*/
if( isValid )
...
else
...
For a question like this you must use strings to gather then input, and then parse them yourself.