943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 455
  • C++ RSS
Jan 9th, 2009
0

c++ Problem (Need help)

Expand Post »
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
Similar Threads
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Jan 9th, 2009
0

Re: c++ Problem (Need help)

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.
Reputation Points: 60
Solved Threads: 17
Junior Poster in Training
Yiuca is offline Offline
88 posts
since Sep 2008
Jan 9th, 2009
0

Re: c++ Problem (Need help)

getline as Yiuca said.

Never use macros like that.
template <typename T> void input_cin(T variable);
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 9th, 2009
0

Re: c++ Problem (Need help)

template <typename T> void input_cin(T variable);

doesn't work
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Jan 9th, 2009
1

Re: c++ Problem (Need help)

>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).
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 10th, 2009
0

Re: c++ Problem (Need help)

Quote ...
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
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help with DarkGDK
Next Thread in C++ Forum Timeline: Crazy Unexplained Error (using DarkGDK)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC