New to C++

So I'm playing around trying to teach myself some stuff in C++ from this book I got. Can you take a peek at my code and read what its suppose to do from the comment lines and tell me what I'm doing wrong or how to improve upon what I'm doing. I guess I'm just not understanding why its not printing the "spaces". Can anyone help?

// This program is designed to print 3 integers seperated by a space of any 3 digit number input.
// Example, if you enter "300" the computer should return "3 0 0"

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
string DIGIT1;
string DIGIT2;
string DIGIT3;

cout << "Enter any 3 digit number and hit enter. EXAMPLE: (001-999)" << endl;
cin >> DIGIT1,DIGIT2,DIGIT3;
cout << DIGIT1 << DIGIT2 << DIGIT3 << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
use [code ] [/code ] tags.

A "string" is, as the name implies, a string of characters. When you from cin into a string using cin>> you are reading all characters until a whitespace (or similar) character is hit. Change the "string"s to "char"s and it will work.
spaces are not automatically inserted. You need to write this: cout << DIGIT1 << ' ' << DIGIT2 << ' ' << DIGIT3 << endl;

Furthermore 'cin' expects that you hit the return key when you're finished with entering the number/string/... So the 300 is not store as DIGIT1='3', DIGIT2='0', DIGIT3='0'

cin >> DIGIT1,DIGIT2,DIGIT3; The comma operator is wrong here. It just stores the entered value in one of the provided strings and simply ignores the rest.
Actually Hanst99 changing the strings to chars will not make the program work as the syntax used to obtain the user input is incorrect.

Also, needtopass C++ can not read your mind :P If you want it to print a space, you must tell it to print a space! Further more, three strings are not needed, you can do this with one string fairly easily. Try the following...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
	string allDigits;

	cout << "Enter any 3 digit number and hit enter. EXAMPLE: (001-999)" << endl;
	cin >> allDigits;
	if(allDigits.length() == 3){
		cout << allDigits.at(0) << " " << allDigits.at(1) << " " << allDigits.at(2) << endl;
	}else{
		cout << "I said THREE digits you fool!" << endl;
	}

	system("PAUSE"); 
	return EXIT_SUCCESS;
}


Depending on how strict you want to be with user input you might also consider checking to ensure that only numbers can be entered by the user. At present the user can put what they like in.

Edit: coder777 hit the nail on the head!
Last edited on
So what should my code look like if using "char" instead of a string for this? I understand having to add the space now if I want it to show up on "cout" and that 300 is not being stored as digit1/2/3 but, How do I go about assinging each piece of the number to each string?

for instance:
if you type in 300

I want it to store as: DIGIT1='3', DIGIT2='0', DIGIT3='0' so I can pull it back out as

 
cout << DIGIT1 << " " << DIGIT2 << " " << DIGIT3 << endl;
Change
1
2
3
string DIGIT1;
string DIGIT2;
string DIGIT3;
->
1
2
3
char DIGIT1;
char DIGIT2;
char DIGIT3;
and cin >> DIGIT1,DIGIT2,DIGIT3; -> cin >> DIGIT1 >> DIGIT2 >> DIGIT3;

You still have to press return. You can enter more than 3 chars but the rest is ignored
True, didn't catch that one. I admit I didn't read his code very carefully, cause reading stuff without any highlighting always makes my eyes hurt.
Makes sense, sorry forgot about the [code] tags! Thanks for the help though, its good to see theres a few options too, I didn't even consider using "if" "else" as an option. I do dig the "I said THREE digits you fool!" part you added as well haha
Topic archived. No new replies allowed.