Noob C++ Question

Started by
13 comments, last by f&g 12 years, 6 months ago
Good evening to all at GameDev im very much a noob to C++ and i am looking for some advice to my issue
below is the source to a small program where i get the user to enter there name and then there name is displayed back to them on the screen all this works 100% fine but the main issue is that the console window closes and opens very fast below is the source code


#include <iostream>
#include <string>

int main()
{
std::string FirstName;

std::cout << "Please enter your first name:";
std::cin >> FirstName;
std::cout << FirstName << "\n";
std::cin.get();

return 0;

}


Thank you very much in advance
Advertisement
I'm a total newbie, so take what I say with a grain of salt as you await someone with experience to answer. But, the way I keep it from closing is by adding add a line ("method?") where the user has to enter something, anything. That way, the program doesn't close until the user presses enter even though the program doesn't actually do anything with the user's input.

In Python, it can be input(). In C# it can be Console.ReadLine(). It looks like in your code, it's std::cin.get(), but that's just a wild guess.

I hope that helps.
A 30-year-old interested in learning to program by making an old-school, simple, text-only or 2D dungeon fantasy video game.

  • As of 9/28/2011, currently attempting to learn the basics of Python 2.7.
  • As of 10/14/11, Dabbling in C#.
  • As of 10/24/11, decisively surpassed my small knowledge of Python in C#.

I'm a total newbie, so take what I say with a grain of salt as you await someone with experience to answer. But, the way I keep it from closing is by adding add a line ("method?") where the user has to enter something, anything. That way, the program doesn't close until the user presses enter even though the program doesn't actually do anything with the user's input.

In Python, it can be input(). In C# it can be Console.ReadLine(). It looks like in your code, it's std::cin.get(), but that's just a wild guess.

I hope that helps.


Hi Sharpe thank you very much for your reply but i have just got it working here is the working code below



int main()
{
std::string FirstName;
std::string LastName;

std::cout << "Please enter your first name:";
std::cin >> FirstName;
std::cin.ignore();
std::cout << "Please enter your second name:";
std::cin >> LastName;
std::cin.ignore();
std::cout << "Hello " << FirstName << LastName << "\n";
std::cin.get();

return 0;

}
Thats not really a code issue, its your IDE. Are you using Visual C++ (Express)?

If so, just launch your code with CTRL + F5

Thats not really a code issue, its your IDE. Are you using Visual C++ (Express)?

If so, just launch your code with CTRL + F5


Hi Serapth yes i am using visual studio C++ express 2010 i have just recompiled the first lot of code at the very top and used CTRL+F5 and still did the same thing just opened and closed very fast after i hit return but the second lot of code is working fine
You can always set a breakpoint on your return line.
Real console programs are generally not executed in isolation, but rather from an interactive command prompt. The behaviour you are describing stems from the fact that when you run your program runs, Windows notices that there is no current console, and conjures up one for you. However, when your program completes no one is using the console any more, so Windows kindly closes it.

There are a couple of ways of dealing with this. One is to accept this as how console programs work. You can place a breakpoint on the last line of the main() function, which will keep the console open long enough to see the output. Alternatively, have a separate console window open in the directory your executable is built in, and run your program from there*.

The other is to add an artificial delay in the code. You seem to be trying to do this with std::cin.get(). However, there may already be input in the buffer from your previous stream extraction operation. You would need to account for this.

Other options include the common std::system("pause"), but I would not recommend this (it involves creating a process to do the work, and is not a portable solution).

*[size="1"]If you do this, and have an artificial delay, you'll see just how annoying it is when console programs require input to close normally!

[quote name='Serapth' timestamp='1318457206' post='4872003']
Thats not really a code issue, its your IDE. Are you using Visual C++ (Express)?

If so, just launch your code with CTRL + F5


Hi Serapth yes i am using visual studio C++ express 2010 i have just recompiled the first lot of code at the very top and used CTRL+F5 and still did the same thing just opened and closed very fast after i hit return but the second lot of code is working fine
[/quote]

Well CTRL +F5 just causes a "Press any key" message to appear, so its the same as a single .cin() call. So, your return call is closing the window.

Good evening to all at GameDev im very much a noob to C++ and i am looking for some advice to my issue
below is the source to a small program where i get the user to enter there name and then there name is displayed back to them on the screen all this works 100% fine but the main issue is that the console window closes and opens very fast below is the source code


#include <iostream>
#include <string>

int main()
{
std::string FirstName;

std::cout << "Please enter your first name:";
std::cin >> FirstName;
std::cout << FirstName << "\n";
std::cin.get();

return 0;

}


Thank you very much in advance


The correct way to deal with this is to run the application from the console (adding a cin.ignore(); followed by cin.getline(); works for simple tests but will make the program a pain to use in shell scripts.

consider for example an application that takes a set of strings, sorts them and prints the sorted list


#include <iostream>
#include <list>
#include <string>

bool compare_nocase (string first, string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first)<tolower(second)) return true;
else if (tolower(first)>tolower(second)) return false;
++i;
}
if (first.length()<second.length()) return true;
else return false;
}

void main() {
std::string tempString;
std::list<std::string> stringList;
while (std::cin >> tempString) {
stringList.push_back(tempString);
}
stringList.sort(compare_nocase);

std::list<std::string>::iterator it;

for (it=stringList.begin();it!=stringList.end(); ++it) {
std::cout << *it << std::endl;
}
}


In this case you can for example run the program as:
for %x in (*.txt) do sort.exe < %x > sorted_%x

to sort the words in all textfiles in the directory and save them with the sorted_ prefix on the filename.

If the sort program blocks at the end this would become quite a pain to do (As the user would have to press return after each file)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

Good evening to all at GameDev im very much a noob to C++ and i am looking for some advice to my issue
below is the source to a small program where i get the user to enter there name and then there name is displayed back to them on the screen all this works 100% fine but the main issue is that the console window closes and opens very fast below is the source code


#include <iostream>
#include <string>

int main()
{
std::string FirstName;

std::cout << "Please enter your first name:";
std::cin >> FirstName;
std::cout << FirstName << "\n";
std::cin.get();

return 0;

}


Thank you very much in advance


add the line system("pause"); before return;


This topic is closed to new replies.

Advertisement