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

Dani AI

Generated

A few focused notes that build on the replies here.

A macro alias for input hides real problems and makes debugging harder. was right to discourage it. Typical causes for a second read to "fail" are: (a) a previous extraction left the stream in a fail state (e.g., non-numeric text when reading an integer), (b) mixing token-based extraction and line-based reads leaves a trailing newline in the buffer, or (c) your prompt wasn't flushed so you never actually saw it. 's suggestion to read whole lines is often a good fix for string input, and 's point about checking function prototypes and stream state is useful when wrapping input.

A small, safe helper avoids the macro pitfalls: take the variable by reference, check the stream, clear and discard bad input when needed, and return success/failure so callers can respond. For example:

#include <iostream>
#include <limits>

template<typename T>
bool read_input(T& out)
{
    if (!(std::cin >> out)) {
        std::cin.clear(); // clear failbit
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // drop rest of line
        return false;
    }
    return true;
}

Practical checklist:

  • If you use line-based reads for names, use a line reader; if you mix both, consume the leftover newline (see std::istream::ignore or std::ws).
  • Always check the stream (if(!std::cin) or the returned bool) and clear/ignore on failure.
  • Avoid macros for I/O aliases; prefer a tiny inline function or template like above.
  • If prompts don't appear before input, flush the output (use endl or flush).

For reference on behavior when mixing extraction and line-based reads, see std::getline and std::istream::ignore. 's encouragement about getting used to the standard names is sound — learning the idioms pays off.

Recommended Answers

All 5 Replies

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)

getline as Yiuca said.

Never use macros like that. template <typename T> void input_cin(T variable);

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

doesn't work :(

>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).

template <typename T> T input_cin(T variable)
{
  std::cin >> variable;
  return input;
}

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.

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.