How to end getline Method (and Move on to Next Part of Code)

Hello and thanks! Her is my code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


string mystr;

int main(int argc, char** argv) {
cout <<"Hello World" << endl;
cout <<"Please enter your name: ";
getline(cin, mystr);
cout <<"Your name is: " <<mystr;
return 0;
}

My problem is that when I'm entering input, despite that I press enter, the program does not continue to the print out.
I appreciate the help!
Last edited on
i don t have any problem with your program. i coppied everything and it runs perfectly.. what happens when you enter name and press enter??
I'm using the NetBeans IDE.
So I run my code and I get:

Hello World
Please enter your name:

Just like I'm suppose to. And then I enter some strings and press enter but nothing happens.
At the bottom, it continues to say: Hello World (Build, run) (1 more...)

I think it is worth nothing that I cannot actually see what I type. I thought that was just how C++ works but now I'm doubting everything.
Curiously, when I just use the cin method or operator or whatever it is, I do get input, but once again I cannot actually see what I typed and it stops input in curious ways.
I.e., if I'm inputting into an int type like this:

int a;
cin >> a;

It only stops inputting when I enter a letter on my keyboard.

And if it a string:

str myString;
cin >> myString;

It stops when I press spacebar.
Try
either
cout <<"Your name is: " <<mystr << endl;

or put
cout.flush();
on the next line after the cout.
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;


string mystr;

int main(int argc, char** argv) {
cout <<"Hello World" << endl;
cout <<"Please enter your name: ";
getline(cin, mystr);
cout <<"Your name is: " <<mystr <<endl;
cout.flush();
return 0;
}

The "endl;" at the end does nothing
Adding cout.flush() procurs this response:

c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: cannot open output file dist/Debug/MinGW-Windows/hello_world.exe: Permission denied
collect2.exe: �G���[: ld �̓X�e�[�^�X 1 �ŏI�����܂���
make[2]: *** [dist/Debug/MinGW-Windows/hello_world.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 762ms)
Okay wait, I take it back.
I'm not sure why but I got rid of all my code and the build still fails.
Like, it is just the main method with return...
Ugh.
Is the program still running in the background when you try to compile? That could cause a "permission denied" message.
Sorry, it may have been that. I just redownloaded MinGW.

Anyway, so same problem. cout.flush() did nothing.
I mean, how could it? The problem is that I cannot get out of the line that reads input.

So if it works for you, that means something must be wrong with my compiler. I swear I've configured my IDE it right. That is strange though. Do you see what you type?
This is what I see:
Hello World
Please enter your name: fred
Your name is: fred


May I know what IDE you are using? I will just use that :).
Downloaded it:

"Hello World - Debug": The compiler's setup (GNU GCC Compiler) is invalid, so Code::Blocks cannot find/run the compiler.
Probably the toolchain path within the compiler options is not setup correctly?!
Goto "Settings->Compiler and debugger...->Global compiler settings->GNU GCC Compiler->Toolchain executables" and fix the compiler's setup.
Skipping...
Nothing to be done (all items are up-to-date).

I swear it said that it came with ITS OWN GNU GCC Compiler...
Last edited on
Gahhh, AVG is going nuts that there is a virus in CodeBlocks?!?

I manually set the path via settings - > compiler -> Path.

For some reason, it was looking for the compiler in C:\MinGW instead of C:\CodeBlocks\MinGW

But when I attempt to run it, AVG stops it and I get a warning saying it is a trojan horse.
Last edited on
Seemed to be a common problem. I just disabled it.
NetBeans is stupid that means. Thanks a lot for your help. I'll be using code blocks from now on!
Just, if you get to see this message, is the MinGW that came with code blocks up to date?
I'm not using AVG. but a quick google shows others have found anti-virus problems too. But the general opinion seems to be a false positive.
Topic archived. No new replies allowed.