Integer Question

EOM
Last edited on
It goes to the next step(starts the loop again) because you checking the value of the number not if they put a extra zero.

because 5 is five, and this: 000000000005 is still five I just put a bunch of zeros in front of it.
Last edited on
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.
Somthing like this perhaps?

1
2
3
4
5
6
7
8
int myvalue = 0;

for (int temp = 0; temp < str.size(); temp++) {
    char A = str[temp];
    if (A == '5') {
        myvalue += 5;
    }
}        

Although I'm not too confident about this line: char A = str[temp] if that lines weird look into it: http://cplusplus.com/reference/string/string/operator[]/
Wish I had a compiler on this computer...

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.
Last edited on
First, please use code tags like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
do
{
cout << "Enter your 5-digit customer account number: ";
cin >> accountNumber;

if (accountNumber < 10000 || accountNumber > 99999){
condition = true;
cout << "Invalid entry, please try again" << endl << endl;
}
else {
condition = false;
}
}while (condition);

What I'de suggest is just using 5 chars instead.
Here's a code snippet:
1
2
3
4
5
6
7
8
char one, two, three, four, five;
cout<<"Enter a 5-digit number: ";
cin>>one;
cin>>two;
cin>>three;
cin>>four;
cin>>five;
cout<<"You entered "<<one<<two<<three<<four<<five<<endl;

Cheers!
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 
Last edited on
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.
king214 wrote:
5 is five, and this: 000000000005 is still five
However "5" != "000000000005"
Last edited on
Topic archived. No new replies allowed.