0

Lets say I have printed out a value to my console application using std::cout<< and have not ended the line, then later on I wanted to add data to the same line and input it into my programme through getline(cin, MyInput), but I want to make use of the entire line rather than just the values in-putted through the keyboard.

consider the value printed out on the screen is 5 and then * 6 is entered through the keyboard, then my programme should multiply 5 * 6! (this is not the purpose of my programme im just trying to get the idea across)

I have tried using a variable that holds the value printed out onto the console application and then I reused this variable but this is not the ideal solution, what I am looking for is a method to consider everything printed on one line (whether its from cin or cout) as one!

Aboudi
  • 308
  • 1
  • 2
  • 17
  • Store them in a container? – blackmesa May 02 '16 at 09:23
  • 1
    What you consider "not the ideal solution" is actually (part of) the ideal solution, which is that the program manipulates a data structure of some kind, completely disconnected from I/O. – molbdnilo May 02 '16 at 09:28
  • Uhh... maybe you could, but it would be complicated and weird. This answer has stuff you'd need, but it's almost certainly not what you want to do and it's fairly advanced: http://stackoverflow.com/a/9084222/493106 – xaxxon May 02 '16 at 09:28
  • @molbdnilo, the only problem with that is that too many steps are involved which I was hoping to avoid, by using existing functions i didnt know of! – Aboudi May 02 '16 at 09:55

1 Answers1

1

What you are trying to do sounds like a bad design. If there's a background process started by the user that spews garbage to the console, do you really want to take that in as part of your input?

What I think you are trying to do is to have some state stored and retrieved as part of the input/output operations.

The clean way to do this is to have a dedicated component/library that does input/output for you. That way when you output something the library can store it (or discard it if it's not intended for storage, for example debug output). When you want to read something the same library can provide back the data stored and merge it with the data it gets from cin.

Sorin
  • 11,863
  • 22
  • 26
  • Thanks, I'm trying to do that now! looks like there is no way to reduce the amount of steps I'll have to implement! – Aboudi May 02 '16 at 10:04