problem with "cout" (solved by Duoas & Ganellon)

i want to be able to create a program where cin is an 8 digit integer and cout is that same integer with two spaces between every digit

pseudocode is

input int 12345678
output 1 2 3 4 5 6 7 8


for the life of me i can't figure out how to have it show on the screen like that!!

here is what i got so far;
it runs but not what i'm looking for
-----------------------------------------------





// learning cout
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
// declaring variables:
int a;

// process:
cout<<"give me an eight-digit integer : "<<endl;

cin>>a;

if (a<100000000)
cout<<a<<" ";

if (a>99999999)
cout<<"I asked for an eight-digit integer. program will now terminate itself"<<endl;

// print out the result:

// terminate the program:
}
Last edited on
Use the remainder operator (%) to get the one's place.
Use the division operator to divide by ten.
1
2
3
4
5
int given = 1234;
int ones = given % 10;
given = given / 10;

cout << given << "  " << ones << endl;

You can modify that to do the same for every power of ten in your number, up to eight places.

Good luck!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
  using namespace std;
  int a;
  cout << "Give me an 8-digit integer.\n";
  cin >> a;

  // make sure it's in the right range

  if((a>=100000000) || (a<10000000))
    cout << "I asked for an 8 digit integer."
    << " Program will now terminate itself." << endl;
  else // if this executes, there is a good 8-digit value
    for (int i=10000000; i>0; i/=10)
    {
      cout << a / i << " ";
      a%=i;
    }
    cout << endl;
  return 0;
}


Sample output:
Give me an 8-digit integer.
12345678
1 2 3 4 5 6 7 8

How it works:
If you have an 8 digit integer MYINT, then integer math says that your integer divided by 10000000 is the first digit of MYINT.
Example:
12345678 / 10000000 = "1"
Similarly:
12345678 / 1000 = "12345"
Also
12345678 / 10 = "1234567"

After you output the digit you're interested in, you can throw it away. You do that with the modulus operator: "%"

12345678 % 10000000 = 2345678

We start out with "i = 10000000"
We divide "a" by "i" to get "1"
We MOD "a" by "i" to get "2345678"
Then, we divide "i" by 10, to remove one "tens" place, so 10000000 becomes 1000000

And the FOR loops keeps doing that until there are no more digits.

So the next iteration of the loop:
i = 1000000
a = 2345678
a / i = 2
a % i = 345678
i / 10 = 100000
Last edited on
Wo-ho! Way to let the OP figure out his own homework!
Thank you Duoas and Ganellon.

Even with Ganellon writing the whole program for me it would've not helped if he didn't explain the process. So far I'm on chapter two of "C++ How to program by" Deitel and Dietal.... C++ is starting to make sense but most of the time when I look at a program its just letters and digits...

Once I'm done with writing my own program to do this problem I'll post it here so you guys can see how I learned from your posts and didn't just copy/paste..

Thank you both for helping me out, I'm sure I'll be bothering you guys a lot this semester.....
Ah, sorry talwar / Duoas. I didn't realize this was a homework assignment.

I'm the new guy here also, trying to learn C++ for myself, not as part of a class. For me, having things explained in detail is more helpful than otherwise. I'm sorry if I said too much, but my heart was in the right place.
Heh, don't feel too bad.

Part of learning to program (or learning anything, for that matter) is struggling to do it yourself. If you are always given the answer, then when the time comes to think for yourself you will be unable to separate the issues and act properly.

After a while you'll also learn to recognize homework assignments fairly readily. The purpose of a homework assignment is to think about what was covered in the lectures and the readings and figure out how to apply that new knowledge properly. It is that endeavor wherein true learning occurs.

Knowledge without the exercise of that knowledge cannot save you when the fire gets hot. (Sorry for the mixed metaphors... :-)

The teacher's task is to direct the learning, not just to spoonfeed knowledge. That too is a learned skill. Alas.


It is good to ask for help.
Last edited on
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
43
#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
int given, one, two, three, four, five; // declaring integers 

cout << "Give me a 6 digit integer.\n";	// asking for input 

cin >> given;	// integer input
if ((given>=1000000) || (given<100000))
    cout << "I asked for a 6 digit integer."
    << " Program will now terminate itself." << endl;
  else 

	one = given % 10;
	given = given / 10;

	two = given % 10;
	given = given / 10;

	three = given % 10;
	given = given / 10;

	four = given % 10;
	given = given / 10;

	five = given % 10;
	given = given / 10;

cout 
<<"\n" /*new line*/
<< given <<	"   " 
<< five  <<	"   " 
<< four  << "   " 
<< three << "   "
<< two   << "   " 
<< one  << "   " 
<< "\n" 
<<endl; //ouput

}

I combined both approaches but if i don't enter 6 digits, after it shows "program will terminate itself" right then I'm getting an [ "Abort" "Retry" Ignore" ] error msg. I think I'm missing a bracket somewhere... I don't know...

thanks again to both of you, for helping me out...
Last edited on
Your If and else need {}
1
2
3
4
5
6
7
8
if()
{
    //do this
}
else
{
    //then do this
}


So you would need to write your program like this - i normally wouldn't post the code but i neatened it up a little.
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
#include <iostream>

int main()
{
	int given, one, two, three, four, five; // declaring integers 

	std::cout << "Give me a 6 digit integer.\n";	// asking for input 

	std::cin >> given;	// integer input

	if ((given >= 1000000) || (given < 100000))
	{
		std::cout << "I asked for a 6 digit integer." << "\nProgram will now terminate itself." << std::endl;
	}
	else 
	{
		one = given % 10;
		given = given / 10;

		two = given % 10;
		given = given / 10;

		three = given % 10;
		given = given / 10;

		four = given % 10;
		given = given / 10;

		five = given % 10;
		given = given / 10;

		std::cout << "\n" << given << "\t" << five << "\t" << four << "\t" << three << "\t" << two << "\t" << one << "\t" << "\n" << std::endl; //ouput
	}

	return 0;
}


I took away the name spacing and added the std:: - changed the void main to an int main(). Gave your if and else a correct {} as you can see how your version didn't have them. And now because we are using int main() - we can return at the end of mains function.

Also instead of you using all those spaces in your end std::cout - i used the "\t" which automatically puts a tab in there for you.

Your tutor may ask you to put your code that breaks apart given (in the else statement) into a loop - do you think you would be able to do that with your current integers?
Last edited on
Topic archived. No new replies allowed.