probably some small stupid error I'm not seeing (printing text for numbers)
Sep 28, 2016 at 5:29am UTC
hi,
this is not working and I'm stumped. I'm guessing it's something small (I hope)and I'm just worn out with it. up till 11 works, then suddenly at 12 and beyond not (it just prints 12 * $1 ) instead of 1 * $10 and 2 * $1.
thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
std::cout << "Enter a positive decimal number between 1 and 1000.." << std::endl;
unsigned int usernumber(1), remainnumber(0);
float smallremainder(0.0);
std::cin >> usernumber;
if (std::cin.fail() || ((usernumber > 1000 ) || (usernumber <= 0)) ){
std::cout << "Input of number failed... exiting..." << std::endl;
}
while (usernumber >= 1){
if (usernumber % 100 == 0){
remainnumber = usernumber / 100;
usernumber = 0;
std::cout << "Number is :" << remainnumber << " * $100" << std::endl;
}
else if ((usernumber > 100 ) && (usernumber % 100 == 1)){
remainnumber = usernumber / 100;
smallremainder = usernumber / 100.00;
smallremainder = (smallremainder - remainnumber) * 100;
usernumber = smallremainder;
std::cout << "Number is :" << remainnumber << " * $100" << std::endl;
}
else if (usernumber % 10 == 0){
remainnumber = usernumber / 10;
usernumber = 0;
std::cout << "Number is :" << remainnumber << " * $10" << std::endl;
}
else if ((usernumber > 10) && (usernumber % 10 == 1)){
remainnumber = usernumber / 10;
smallremainder = usernumber / 10.00;
smallremainder = (smallremainder - remainnumber) * 10.00;
usernumber = smallremainder;
std::cout << "Number is :" << remainnumber << " * $10" << std::endl;
}
else if (usernumber % 1 == 0){
remainnumber = usernumber;
usernumber = 0;
std::cout << "Number is :" << remainnumber << " * $1" << std::endl;
}
else {
//
}
}
Last edited on Sep 28, 2016 at 5:31am UTC
Sep 28, 2016 at 8:08am UTC
(usernumber % 10 == 1)
works only for the number 11 since you explicitely compare against the 1 (and not 2 etc.). Change to:
(usernumber % 10 != 0)
Note that the last if has no effect since everything can be divided by 1.
Topic archived. No new replies allowed.