Infinite loop after cin c++ - c++

I'm pretty newbie at c++, so this confuses me.
This code seems to loop infinitely after std::cin :
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "My name is Martin. What's yours?" << endl;
string name;
cin >> name; //It seems to loop around here
cout << "Ah, so you are" << name << "." << endl; //It doesn't print this message
return 0;
}
Thanks!
EDIT: I'm not sure I understand your answers, but what happens is that when you enter a name and press enter, it does nothing. The terminal just continues to the next line, letting you write something on that one too. It does this infinitely, a little bit like when you just press enter without any text.
EDIT 2: My bad, I should try to clear up things. This is what comes in the JDoodle console:
My name is Martin. What's yours?
JDoodle - Timeout - Some common reasons for Timeout
Your Program may have a endless loop
Please check the program and try again or contact JDoodle support at jdoodle#nutpan.com for more info.
The reason I believe it is a loop is that in a console that doesn't write timeout, I can continue to input on a new line all the time.

Since you are using JDoodle the console will be waiting on the server. If you don't type anything there it hangs until it eventually gets killed for being too slow.
The way to fix this is to enter the text you wish to type before running the program into the box labeled "Stdin Inputs...".
Alternatively you can set the "Interactive mode" to "On" which will show the console in the website where you can enter some text and press enter. (I tried, it works).

Related

Trying to close c++ program at certain time returns value's (that i don't want)

Running the code returns no errors, the problem is when I try to exit the program.
Simply, when i use the exit, return or abort functions, i get this (picture below)
see code below
//First 2 libs handle info exchange
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;
main(){
string username;
cout << "Please enter a general use handel (\"username\")\n";
cin >> username;
fstream blockUsername;
blockUsername.open("Player_Data/username.txt", ios::out); //out is for writing, in is for reading
blockUsername << username;
blockUsername.close();
return 0;
}
I'm aware that similar questions have been asked, but I have not succeeded in fixing my code with those.
What I want is for the program to close with no exit values, even though I know that they are useful.
PS: It very well could be something to do with my code editor (Dev C++), or my compiler which i broke and had to fix recently.
Also side note how would i go about removing that address at the top of the terminal (and potentially replacing it with something else)?
These are printed by your IDE....if u execute the .exe output of this code...the console would just disappear after program exits and it would not be shown
Peace
try using return 0; wherever you want to exit, it's okay to have more than one return 0; in your code or to simply put it after an if statement.

C++: ctrl+d terminates program (ubuntu)

(New to C++)
I have this simple code (simplified for the question):
int main()
{
string currInput;
while (getline(cin, currInput))
{
}
cout << "wont be printed" << std::flush;
return 0;
}
I have been debugging for a while and I probably miss something:
When running it and pressing ctrl+d (after some strings or right away), it does not print the string that is after the while loop. It just stop running. I thought it might be something with flushing so I added that too.
What am I missing?
PS: When running in debug, it mentions something about sighup signal.
So, with a lot of help from #manni and #rici, we found the problem.
It turns out to be a known problem in cLion.
see sending EOF to stdin in Clion IDE
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206849765-How-to-enter-EOF-Ctrl-z-in-Run-console-
https://youtrack.jetbrains.com/issue/IDEA-12514
Your error is the while loop. You should not be using a loop here. What happens is it hits this line and attempts to get input. Regardless of whether you type in anything or not, if you hit CTRL+D it will end the getline. But you have it in a while loop....so it'll hop back to the top of the loop and get another line...then another line....then another line. Welcome to your first infinite loop.
while (getline(cin, currInput))
{
}
The simplest thing would be to do just
getline(cin, currInput);
If you're starting out programming this is probably what you want to do anyway.
If you feel gutsy, read this page: http://www.cplusplus.com/reference/string/string/getline/
You'll notice that getline returns the stream you pass in. Which will evaluate to true as far the loop is concerned.

C++ , won't display last line of code

Shouldn't this work? I mean, the code is merely a test and is meant so that the dialogue goes this way : What is your name? name here, Hello name here, and yet it does not show the last line of Hello after I type in my name and click enter it just dissapears. Here is the code.
#include <iostream>
#include <string>
int main (void)
{
using std::cin;
using std::cout;
using std::string;
string name = "";
cout << "What is your name, pls?\n";
cin >> name;
cout << "\nHello " << name.c_str() << "\n";
return 0;
}
My guess is that you are running from the debugger, or double clicking the executable. In either of those cases, when the program ends, the console will close. So, the program produced output, but you just could not see it before the console closed.
Run the program from a pre-existing console so that the console remains after your program ends. Or, just whilst debugging, arrange that your program does not terminate immediately after emitting its final output. A simple way to do that is to place a break point at the end of the program.
It probably showed it right before it disappeared. If you're going to write console programs, and if you're going to send output to a console, you should run them from a console so the output has some place to go.
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Make sure you put a breakpoint before main goes out of scope. I guess your console disappears under VS?
Also, you don't need to extract the char* in the last cout statement:
cout << "\nHello " << name << endl;
Open a terminal (or a command prompt window).
Navigate to the folder that contains the executable.
Run it.
It's not disappearing. It is just running really fast.
Every IDE has a keyboard shortcut that allows you to run code and pause after the execution has finished.
This keyboard shortcut is Ctrl-F5 in Visual Studio.
I have no idea what IDE you're running, but that is your basic problem.
The other thing you can do is to test your code in ideone : ideone.com/hb4Cel (it's the same code. There is no point pasting it here)
A dirty workaround is to add something like this
cin >> name;
at the end, just before return 0;. It forces the window to wait for input (i.e. hitting return) before returning (which closes the program).
This isn't necessarily good design, but if all you want to do is run some tests then it'll do the trick.
Basically when you enter your name it displays your last line and exits after return 0.
Here are the following things to avoid that
1- use command line to run the application
Start->accessories->command prompt
Go to folder in which your application is using cd command
c:>cd c:\path\foldername
Now run the application by typing the program name e.g
c:\path\foldername>my_application.exe
It will display your last line.
2- Now if your are using microsoft visual c++ press ctrl+F5 to run your program
3- This is not recommended but you an use it as long as your are debugging then remove it from the code afterwards. Include conio.h header file and add getch(); line before return statement. It would hold the screen for you till you press a key.

C++ beginner, execution window disappears quickly

I am a beginner in C++ and I was trying to write a program that finds the average of two numbers, but when I run the program, the window disappears without allowing me to see the result. Can someone please help me?
Thanks
#include <iostream>
using namespace std;
int main()
{
int number1,number2,answer;
cout << "number1? ";
cin >> number1;
cout << "number2? ";
cin >> number2;
answer = (number1 + number2)/2;
cout << answer<<endl;
return 0;
}
Solution #0 (proper):
Run program from shell (cmd.exe, bash)
Solution #1 (proper):
Run program from orthodox file manager. Far Manager or midnight commander.
Solution #2 (alternative);
Redirect output to file. program.exe >file.txt from command line.
Solution #3 (improper):
Launch message box, use "sleep"/"Sleep" to delay program termination.
Solution #4 (improper):
Request user input at the end of program.
Solution #5 (improper):
Set breakpoint on "return 0", debug the program.
Solution #6 (windows+msvc):
Launch program from msvc by Ctrl+F5 using debug build. You'll get "press key to continue" prompt.
Put a breakpoint at your return statement. It won't stop on an uncaught exception but that can be fixed with a try/catch block at the outermost part of main.
Before you return add system("PAUSE"); and this should fix your problem.
If you are using Visual Studio, hit CTRL+F5 to run. That inserts a "Hit RETURN to continue" for a console application.
Include this header:
#include <stdio.h>
And add a call to getchar() before you return:
cout << answer<<endl;
getchar(); // wait for user input
return 0;
Solution #6 from SigTerm (above) is not guaranteed to work. In Visual Studio you should right-click your project and choose:
Properties | Configuration Properties | Linker | System | SubSystem
and in the dropdown choose Console (/SUBSYSTEM:CONSOLE).
I always hated that...I always find the easiest to be system("pause"); right before you return, but that's not a portable solution (cin.get is though). There are many other ways as well, some of which are mentioned in the link below.
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787
Edit: system("pause"); is terrible, should never be used, and may or may not end life on this planet as we know it, even if by a beginner in a project called 'Hello'. Using system("pause") is expensive and dangerous, and if you use it, you'll never see anyone you love again. Don't do it. Use cin.get or something else.

How do I input data using the eclipse console? (c++)

I'm trying my hand at c++ and am using fedora eclipse (3.4.2) as my IDE.
At the moment I'm trying to enter a string of numbers into the console, get the program to sort them and spit them back out. The program is straight out of a book and works with xcode and through a normal terminal - so I know it's correct.
Basically I run the program and enter a few numbers into the eclipse console, the numbers are coloured green so I know its accepting the input correctly.
When I hit enter, the console jumps to a new line and nothing happens. When I press control+shift+D, nothing happens. When I press control+d, nothing happens.
I use eclipse for python too, and the console works properly. Just hitting enter inputs data to the program.
Am I missing something here? I've spent the last half hour or so trying to figure this out. Can anyone help me? Thanks.
What version of ecplise and what complier are you using? The following worked for me on Eclipse Ganymede with GCC version 3.4.5:
#include <iostream>
using namespace std;
int main() {
int x = 0;
cout << "Type your input here:";
cin >> x ;
cout << "You entered " << x << endl;
return 0;
}
How does your program know that input has ended? It sounds like it accepts multiple lines of input in the console window. Isn't there some magic case that pops you out of that loop so you can process the input that's been collected?
As other said, without the code there's no answer.