I wrote a little test program to try to figure out some problems I am having in another program I'm working on.

Code:

# include <iostream>
# include <string>
using namespace std;

void ahoy(string nameEntered)
{
	cout << "Ahoy, " << nameEntered << "!" << endl;
}

int main()
{
	string name;
	cout << "enter your name: ";
	getline(cin, name);
	ahoy(name);	
	return 0;
}

I am using Visual C++ 6.
Problem 1: After I enter keyboard input I have to press enter twice. Why not once? I see this both when I am testing the program in the editor, and also when I run the .exe.
Problem 2: In the .exe, the window closes so I never see the output from the function. How can I make it pause?
I haven't dealt with C++ in a couple of years so I may have just forgotten some simple things.

Recommended Answers

All 6 Replies

first don't use the Visual c++ 6.0 it's bad and non comfartable

use visual studio 2010 and i tried your code in it and it works perfectly.

First problem : it won't happen in visual studio 2010.

Second problem : the windows closes because you are makeing the normal debug and

that's never work because you didn't give it the line to make it pause,

there are 2 ways to fix this and i suggest the second one.

the first : using system("pause"); .
the second : some thing called start without debuging and it's which all using
it's made to make you must (press any key to close the console windows) and it's
shortcut is : CTRL + F5...

this is all

aha and using the system("pause");

must be before the line of return 0; excatly

Well delle, I feel that XMasterrrr is right in suggesting you to use Vs2010.
Actually, vs 6 is very good for designing stand-alone application for windows using VB 6. I also used it a lot.
There seem to be some problem with all the old C++ compilers such as vC 6++, TC or Borand compilers.
They seem to handle stream operations and input/Output operation in an inconsistent behaviour at
run-time may be due to improper algorithm used in there API design or God known what.
I myself had upgraded to vs2010 and I am working on it for quite sometime now. I am very happy that inconsistent behavior in I/O and stream operation in this period. It is very frustrating to see a program fall no mistake on our part. If VS2010's cost is issue for you, you can always look for vs express editions.

Regarding your second query, you can use system ( "pause" ), just before return statement in your program.
you will get the output to pause, when running exe file.
Thanks
-Manoj

These are my personal views and if any anybody have any differences with it. Don't brother me with cross-reference
back posts. They can use their ideas.

in the end of main, write
cin.get() which will wait for you to press enter so it won't close

Don't use System("pause"), instead as LordNemrod suggested use cin.get().

>>Don't use System("pause"), instead as LordNemrod suggested use cin.get().

Yes, never use System("pause").

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.