Jump to content
  • Advertisement
Sign in to follow this  
f&g

Noob C++ Question

This topic is 2536 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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;

}

Share this post


Link to post
Share on other sites
Thats not really a code issue, its your IDE. Are you using Visual C++ (Express)?

If so, just launch your code with CTRL + F5

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!