You are viewing our Forum Archives. To view or take place in current topics click here.
C++ Constant Format Help
Posted:

C++ Constant Format HelpPosted:

Neon-Vibe
  • New Member
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
Hay, I just wondered whether anybody can help me with a format to put my constant in so that I can take more than one inputted word using "cin >>". For example, if I use the 'char' format it only takes the first inputted word and ignores the rest. Here is an example of the code I need it for:

'format' input;
cin >> input;
cout << input;


So what format should I use so that I can take more than one word?
#2. Posted:
Imp
  • Blind Luck
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
Neon-Vibe wrote Hay, I just wondered whether anybody can help me with a format to put my constant in so that I can take more than one inputted word using "cin >>". For example, if I use the 'char' format it only takes the first inputted word and ignores the rest. Here is an example of the code I need it for:

'format' input;
cin >> input;
cout << input;


So what format should I use so that I can take more than one word?


cin only allows one datum input from a user, and that is terminated by a valid blank separator, such as space, tab, or a newline.

To achieve what you want, you have to set the data type as string. Then, rather than using cin >> input use getline (cin,input)

This will allow you to collect the entire string, upto the newline.
#3. Posted:
Neon-Vibe
  • New Member
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
Imp wrote
Neon-Vibe wrote Hay, I just wondered whether anybody can help me with a format to put my constant in so that I can take more than one inputted word using "cin >>". For example, if I use the 'char' format it only takes the first inputted word and ignores the rest. Here is an example of the code I need it for:

'format' input;
cin >> input;
cout << input;


So what format should I use so that I can take more than one word?


cin only allows one datum input from a user, and that is terminated by a valid blank separator, such as space, tab, or a newline.

To achieve what you want, you have to set the data type as string. Then, rather than using cin >> input use getline (cin,input)

This will allow you to collect the entire string, upto the newline.


Thanks for this, as you can probably guess I am a learner. Thanks again.
#4. Posted:
Imp
  • Retired Staff
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
No Problem, as you can tell I spend most of my time in here when on, so any problems, just PM me
#5. Posted:
Neon-Vibe
  • New Member
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
I used the method you surgested but I have an issue. When i get to the section of the code where it is it doesnt wait for me to enter an input, instead it carries out the code automatically without pausing. Then when i get to the section where it should display the inputted value it just has a blank line in its place.
#6. Posted:
Imp
  • Shoutbox Hero
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
Neon-Vibe wrote I used the method you surgested but I have an issue. When i get to the section of the code where it is it doesnt wait for me to enter an input, instead it carries out the code automatically without pausing. Then when i get to the section where it should display the inputted value it just has a blank line in its place.


Add this just above the getline

ws(cin);


this will ignore whitespaces, let me know if that helps
#7. Posted:
Neon-Vibe
  • New Member
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
Status: Offline
Joined: Jan 18, 201311Year Member
Posts: 39
Reputation Power: 1
Yeah , it works great now... thanks for all of the help
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.