c++ Problem (Need help)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 151
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster

c++ Problem (Need help)

 
0
  #1
Jan 9th, 2009
What I am trying to do is change the

cin >> varible;

into something different so when I find it easier when I code.

The line that I am changing it to is

input (varible);

i am using this:

#define input(i) cin >> (i);

it works, however it will only allow me to input one thing and not loads for example:

say ("Welcome to my first script, what is your name");
input(name);
-- will work

but, this will not:

say ("Welcome to my first script, what is your name");
input(name);
say ("That's great! How old are you?")
input(age);

any ideas, please help thank you
If you ask me questions through Private messaging I won't reply.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: c++ Problem (Need help)

 
0
  #2
Jan 9th, 2009
getline(cin, variable);

Generally the more preferable option in the majority of cases as far as I'm aware. (It'll get an entire line including whitespace, use enter to terminate input)
Last edited by Yiuca; Jan 9th, 2009 at 9:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 972
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: c++ Problem (Need help)

 
0
  #3
Jan 9th, 2009
getline as Yiuca said.

Never use macros like that.
template <typename T> void input_cin(T variable);
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 151
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster

Re: c++ Problem (Need help)

 
0
  #4
Jan 9th, 2009
template <typename T> void input_cin(T variable);

doesn't work
If you ask me questions through Private messaging I won't reply.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: c++ Problem (Need help)

 
1
  #5
Jan 9th, 2009
>template <typename T> void input_cin(T variable);
>doesn't work
Well... you need to actually write the function, too. That's just the prototype (and you probably want it to return something, so don't use void).
  1. template <typename T> T input_cin(T variable)
  2. {
  3. std::cin >> variable;
  4. return input;
  5. }

However, I recommend using getline() instead of cin for obvious reasons. It effectively removes nearly all issues associated with clearing the input buffer, as described here, and will fix your original problem regarding cin only grabbing a single word from the input buffer.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: c++ Problem (Need help)

 
0
  #6
Jan 10th, 2009
What I am trying to do is change the

cin >> varible;

into something different so when I find it easier when I code.
Why do this? Is cin hard to remember? You need to get used it, you'll see it everywhere!! Think of it as "console input", this might help you remember. When I started C++ I was forgetting everything but as I learned I remember them, so do not worry
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC