Hello, I'm new to the forum and currently taking my first C++ course. We're using Programming: Principles and Practice Using C++ and I'm having trouble with Chapter 6, Exercise 9. Question reads as this:
Write a program that reads digits and composes them into integers. For example, 123 is read as characters 1,2, and 3. The program should output "123 is 1 hundred and 2 tens and 3 ones". The number should be output as an int value. Handle numbers with one, two, three, or four digits.
My problem is that I don't know how to really begin with this one. How do I specify input to a degree where I can show what 123 is really broken down into? All the output stuff I can figure out just fine, it's the input portion is my problem.
You just have the user input a number as normal. Then you can check if that number is over 1000, and divide by 1000 if it is. So if the inputted number is 2567, then dividing by 1000, would give a integer of 2. Subtract 2*1000 from the beginning number, leaving 567. Then divide by 100, etc. till your left with the ones. Have a separate variable or use an array like 'int numbers[4]', and store each result in the array or in the variable.
I'm gonna guess.... if it is 4 digits such as 4234 use something that divides it by 1ooo so its 4.234. Subtracting this number by 4 and multiplying by 10 would get 2.34. Subtracting that result by 2 and multiplying by 10 will get you 3.4. subracting this number by 3 and multiplying by ten will get you 4. To get each value of the corresponding digit use a floor function which rounds that number down. I didn't really plan this out so i don't know how it will exactly work but its an idea... Thanks whitenite for the suggestion
Actually... since you're not using the number for any calculations, it might be easier to read it in as a string (or char array).
Starting from the last digit, go back. Output the last digit and say "x ones", then go one step forward. The good thing is that you can even have correct grammar, because the last digit (the ones) is ALWAYS the one that carries the "and" instead of a comma [unless the number has only one digit].
Actually... since you're not using the number for any calculations, it might be easier to read it in as a string (or char array).
Not only that, the assignment specifically tells him to read the data in as a char array, but then it also tells him to convert to an int prior to outputting:
For example, 123 is read as characters 1,2, and 3. The program should output "123 is 1 hundred and 2 tens and 3 ones". The number should be output as an int value. Handle numbers with one, two, three, or four digits.
int main()
{
cout<<"Input a number: \n";
int val1;
while(cin>>val1&&val1!='|')
if(!cin){
return 1;
}
elseif(val1>=1000&&val1<10000){
int val2=val1/1000;
cin>>val2;
int val3=val1-val2*1000;
cin>>val3;
int val4=val3/100;
cin>>val4;
int val5=val3-val4*100;
cin>>val5;
int val6=val5/10;
cin>>val6;
int val7=val5-val6*10;
cin>>val7;
cout<<val1<<" is "<<val2<<" thousands and "<<val4<<" hundreds and "<<val6<<" tens and "<<val7<<" ones.\n";
keep_window_open();
}
elseif(val1>=100&&val1<1000){
int val2=val1/100;
cin>>val2;
int val3=val1-val2*100;
cin>>val3;
int val4=val3/10;
cin>>val4;
int val5=val3-val4*10;
cin>>val5;
cout<<val1<<" is "<<val2<<" hundreds and "<<val4<<" tens and "<<val5<<" ones.\n";
keep_window_open();
}
elseif(val1>=10&&val1<100){
int val2=val1/10;
cin>>val2;
int val3=val1-val2*10;
cin>>val3;
cout<<val1<<" is "<<val2<<" tens and "<<val3<<" ones.\n";
keep_window_open();
}
else
cout<<"Sorry, bad input!\n";
keep_window_open();
return 0;
}
A lot of this seems a bit excessive, is there a shorter way I should have done it? The program works exactly how I wanted it to. And I don't mind writing all of it out but it'd be nice to know for future reference.