1

In GNOME terminal and XTerm in Ubuntu, I'm facing this problem: I am forced to input values for all cin statements, irrespective of where they appear in source code, and only at the end executes all the cout statements. For instance:

int main()
{
    int a;
    cout<<"Enter a :";
    cin>>a;
    cout<<"\n";
    return 0;
}

When I run this code (using g++), I am forced to to input the value for a before the first cout statement runs.

kanishk509@kanishk509-hp:~/Hackerearth$ g++ -Wall -o sample sample.cpp kanishk509@kanishk509-hp:~/Hackerearth$ ./sample 5 Enter a :

image

The '5' is the input that I am forced to provide to the statement cin>>a before the any cout statement runs.

Kanishk
  • 21
  • 4
  • *"I have to input the value for a before the first cout statement runs..."* - That sounds about right. What is the problem? – jww Jul 02 '17 at 17:43
  • Are you allowed to change the code? – Code-Apprentice Jul 02 '17 at 17:48
  • @jww The problem is that I want the cout statement to run first, and then input the value for a. – Kanishk Jul 02 '17 at 17:56
  • @Code-Apprentice Yes, of course. This is just a sample I wrote to demonstrate the problem – Kanishk Jul 02 '17 at 17:57
  • Since you have complete control over the code, just do the `cin` first. – Code-Apprentice Jul 02 '17 at 17:58
  • @Code-Apprentice So what if I have to print a prompt before taking an input, like : "Enter a : "? – Kanishk Jul 02 '17 at 18:00
  • @Kanishk Then do it! You cannot have it both ways. – Code-Apprentice Jul 02 '17 at 18:01
  • I believe you have left out some significant details about what you really want to do. Is this for a school assignment? – Code-Apprentice Jul 02 '17 at 18:02
  • @Code-Apprentice No, this is not for a school assignment nor is it for any specific program that I want to write. I just switched to Linux from Windows and want to know how to make the cin and cout statements run in the order that they appear in the code. This is my first question and I would be grateful if you can point out what kind of details I should add to the question. – Kanishk Jul 02 '17 at 18:08
  • @Code-Apprentice "_Then do it!_" I am trying to do it, have a look at the image, the prompt is getting printed AFTER providing the input to the cin statement(which appears after the cout statement in the code) – Kanishk Jul 02 '17 at 18:10
  • hmm...I misunderstood. For some reason I thought you **wanted** the prompt to appear after the input... – Code-Apprentice Jul 02 '17 at 18:11
  • @jww I think we both had similar misunderstandings. The output as shown in the screenshot **is** the problem because the prompt should appear before the line that accepts input. (I assume that the 5 is the input.) – Code-Apprentice Jul 02 '17 at 18:13
  • @Code-Apprentice Yes, I have to provide inputs for _all_ `cin`s before any `cout` runs. – Kanishk Jul 02 '17 at 18:17
  • @Kanishk how do you compile and run your code? – Code-Apprentice Jul 02 '17 at 18:23
  • @Kanishk when you say "I have to..." I thought you meant that is what you **want** because of the requirements of the problem you are trying to solve. Instead, you are describing what actually happens when your program runs. You might want to edit your question to make this more clear. – Code-Apprentice Jul 02 '17 at 18:27
  • @Code-Apprentice Using the command line: `g++ -Wall -o sample sample.cpp` – Kanishk Jul 02 '17 at 18:27
  • Then how do you run it? With `./sample`? Your screenshot doesn't show this. Since you are doing command line, you can copy and paste an example run just like you do the actual code. – Code-Apprentice Jul 02 '17 at 18:29
  • Possible duplicate of [c++ force std::cout flush (print to screen)](https://stackoverflow.com/questions/22026751/c-force-stdcout-flush-print-to-screen) – Thomas Dickey Jul 02 '17 at 18:38
  • @ThomasDickey Solved! Thanks. Should I delete this question since it is possibly a duplicate or post an answer myself? – Kanishk Jul 02 '17 at 18:45

1 Answers1

1

Using std::flush solved the problem.

int main()
{
    int a;
    cout<<"Enter a :"<<flush;
    cin>>a;
    cout<<"\n";
    return 0;
}



kanishk509@kanishk509-hp:~/Hackerearth$ g++ -Wall -o sample sample.cpp
kanishk509@kanishk509-hp:~/Hackerearth$ ./sample
Enter a :5
Kanishk
  • 21
  • 4