
Program question!?!?!?!
I am using "C++ how to program" by deitel/deitel forth edition. The problem is as follows:
"Write a program that inputs a fivedigit number, spearates the number into its individual digits and prints the digits separated from one another by three spaces each. (Hint: use the integer division and modulus operators)."
It's in chapter 1, so the only things I have learned so far is the basics: cout, cin, cerr, mathmatic operators (order of operations) and if statements. Here is my code:
Code:
01 #include <iostream>
02
03 int main()
04 {
05 int 5digitnumber;
06 int newoutput;
07
08 std::cout <<"Input a 5 digit number"<<std::endl;
09 std::cin >>5digitnumber <<std::endl;
10
11 newoutput = 5digitnumber / 1; //trying to separate digits...im so lost here.
12
13 std::cout <<newoutput;
14 return 0;
15 }
:confused:

> 5digitnumber
Variable names must begin with a letter
> std::cin >>5digitnumber <<std::endl;
Don't mix << and >> in the same line

Thanks. I am not using a compiler yet, im just writing out on a text editor untill I think I have the program solved then I will compile it a sort out the syntax errors later.

Separate from the coding part, you should work on how to use division (/) and modulus (%) to separate the integer into digits. It is a very common trick, and you should be able to figure it out if you just think about how to get the first digit by dividing the number by something else. Play with it on a calculator, your Operating System's calculator probably has both / and Mod. If you are having trouble coming up with it, you can search for the answer on this board.

Sorry but I am still struggling on this. I searched the board and found this snippet of information:
Code:
digit = x % 10 + '0';
x /= 10;
I have no clue how or why that would work. What does the '/=' operator do?!?
So with that, I applied my program to it and this is what I am coming up with:
Code:
output = bignumber % 10 + '0';
bignumber /= 10;
Doing this gives me the number 49..... I am assuming that the ASCII equivelent of '0' is 49 because I tried substituting the '0' with spaces and nothing was printed to the screen.

I don't think the '0' is necessary, since you can just output the number instead of converting it to a character. Did you try this with a calculator? For example, if your big number is 34567, what do you get when you put in 34567 Mod 10? Mod 100? Div 10? Div 100?
By the way, operator /= simply divides the number on the left by the number on the right, and stores the answer in the number on the left. A little experimentation can show you that: Code:
#include <iostream>
int main()
{
int bignumber = 34567;
std::cout << bignumber << std::endl;
bignumber /= 10;
std::cout << bignumber << std::endl;
bignumber = bignumber / 10; // Same as /=
std::cout << bignumber << std::endl;
}
I have a feeling you won't be wanting to use /=, though. Maybe %=.

Hi after seen your post i had to also try this and it took me ages to figure it out.
I couldn't do it with the modulas operator so can someone show me how?
Maybe just an example?
My code is here and it works but it is horrible.
Code:
#include <iostream>
using namespace std;
int main()
{
int number,
first,
second = 0,
realsecond = 0,
third = 0,
realthird = 0,
actualthird = 0,
forth = 0,
realforth = 0,
fifth = 0,
sixth = 0,
realsixth = 0,
seventh = 0;
cout << "Input a 5 digit number: " << endl;
cin >> number; // 23456 was entered
first = number / 10000;
cout << "first\t " << first << endl; // just to understand it :)
//
second = 10000 * first; // 20000
cout << "second\t " << second << endl;
realsecond = number  second;
cout << "third\t " << realsecond << endl; // 3456
third = realsecond / 1000; // 3
cout << "forth\t\t " << third << endl;
//
realthird = 1000 * third; // 3000
actualthird = realsecond  realthird;
cout << "fifth\t " << actualthird << endl; // 456
forth = actualthird / 100; // 456
cout << "sixth\t\t " << forth << endl;
fifth = 100 * forth; // 400
realforth = actualthird  fifth;
cout << "seventh\t " << realforth << endl; // 56
realsixth = realforth / 10;
cout << "2nd last number\t\t " << realsixth << endl;
seventh = realforth % 10;
cout << "\t\t " << seventh << endl;
return 0;
}

num = 123
123 % 10 = 3
123 / 10 = 12 using integer math where decimals are ignored.
keep repeating until num is less than 10.

Well, since the exercise didn't specify what order the digits needed to be in, you could just do this:
Code:
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a number: ";
if ( !( cin>> num ) ) {
return 0;
}
while ( num != 0 ) {
cout<< num % 10;
num /= 10;
if ( num == 0 )
cout<<'\n';
else
cout<<" ";
}
}
To get them in the right order takes more work or more ingenuity:
Code:
#include <iostream>
using namespace std;
void print_digit ( int num )
{
if ( num == 0 ) {
return;
}
print_digit ( num / 10 );
cout<< num % 10 <<" ";
}
int main()
{
int num;
cout<<"Enter a number: ";
if ( !( cin>> num ) ) {
return 0;
}
print_digit ( num );
cout<<endl;
}

I'm sorry , but it has to be done with alot less c++ knowledge using if if/else  + / % only as it is the first chapter of the book.
I know ou have no idea whats in the chapter but thtas what he was trying and i want to try also.
I could have did it with fucntions also but if i can't do it using the above i'm a retard.
Thanks for your help prelude and elad.

>it has to be done with alot less c++ knowledge using if if/else  + / % only as it is the first chapter of the book
Is this dumb enough? ;)
Code:
#include <iostream>
using namespace std;
int main()
{
int num = 12345;
cout<< num / 10000 <<" ";
cout<< num % 10000 / 1000 <<" ";
cout<< num % 1000 / 100 <<" ";
cout<< num % 100 / 10 <<" ";
cout<< num % 10 <<endl;
}

That code is oozing quality.
Thanks alot i understand it now for the modulas operator.
Cya.:)

Thanks to everyone that helped. What I learned was that if you use an integer of int typed and use any kind of math function to get a different value, if the value is a decimal it will ignore everything past the decimal. :) Thanks once again.