Jump to content

  • Log In with Google      Sign In   
  • Create Account

Noob C++ Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 f&g   Members   -  Reputation: 102

Like
0Likes
Like

Posted 12 October 2011 - 03:19 PM

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

Sponsor:

#2 Sharpe   Members   -  Reputation: 144

Like
0Likes
Like

Posted 12 October 2011 - 03:49 PM

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

#3 f&g   Members   -  Reputation: 102

Like
0Likes
Like

Posted 12 October 2011 - 03:59 PM

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;

}


#4 Serapth   Crossbones+   -  Reputation: 5476

Like
0Likes
Like

Posted 12 October 2011 - 04:06 PM

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

If so, just launch your code with CTRL + F5

#5 f&g   Members   -  Reputation: 102

Like
0Likes
Like

Posted 12 October 2011 - 04:13 PM

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

#6 yckx   Prime Members   -  Reputation: 1217

Like
0Likes
Like

Posted 12 October 2011 - 04:18 PM

You can always set a breakpoint on your return line.

#7 rip-off   Moderators   -  Reputation: 8228

Like
0Likes
Like

Posted 12 October 2011 - 04:19 PM

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

*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!

#8 Serapth   Crossbones+   -  Reputation: 5476

Like
0Likes
Like

Posted 12 October 2011 - 04:19 PM


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


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.

#9 SimonForsman   Crossbones+   -  Reputation: 6112

Like
0Likes
Like

Posted 12 October 2011 - 04:25 PM

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[i])<tolower(second[i])) return true;
	else if (tolower(first[i])>tolower(second[i])) 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)
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!

#10 thedodgeruk   Members   -  Reputation: 124

Like
0Likes
Like

Posted 15 October 2011 - 01:07 PM

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;




#11 d128562   Members   -  Reputation: 108

Like
0Likes
Like

Posted 15 October 2011 - 02:13 PM


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;


omg, why don't take a hammer and smash the keyboard instead?

#12 SiCrane   Moderators   -  Reputation: 9599

Like
0Likes
Like

Posted 15 October 2011 - 02:18 PM

omg, why don't take a hammer and smash the keyboard instead?

Please try to use more constructive criticism. Such as: "this is highly inefficient because it spawns at least one new process", "this is non-portable" or "this is a bad idea for reasons already stated in the thread".

#13 Servant of the Lord   Crossbones+   -  Reputation: 19596

Like
1Likes
Like

Posted 15 October 2011 - 02:30 PM


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


omg, why don't take a hammer and smash the keyboard instead?


Because the first idea would help the OP, while the second would not. Posted Image

A self-acknowledged newbie just learning the very bare-bone basics would do well to just use system("pause"). Sure, it's not the best solution at all, but it's by far the easiest for a newbie to understand, instead of trying to explain something like, "Just open your command prompt, change your directory, and execute your application!" (Open my whatnow, change my whajhamjig, and assassinate my job application? Why do people keep throwing random terms at me!), or giving broad sweeping suggestions that aren't really helpful, ("Listen simply: Uninstall your entire IDE, forget everything you ever knew, install -my- favorite IDE, and start from scratch! You have to do this, or you'll forever be doomed!").

He'll have plenty of time to find out why system("pause") is a bad idea, but first he ought to get past page 2 of his programming book. When someone is at that early a stage of learning, you should give the most straightforward and easiest solution, instead of potentially complicating things for them and having them drop programming because it's "too hard" for them.

Seriously... "std::cout << "Please enter your name:" << std::endl;"? He's on page 2 or 3! Make it as easy as possible, before making it as 'correct' as possible. If you are really afraid of the horrible horrendous dangers of the vile system("pause"), then just tell him to use Sleep(3000) or something equally simple, like get(). But before anyone breaks into, "He should structure his programs properly in a loop!". That's correct... and if we let him actually get to the chapter that teaches him about loops, he'll rapidly learn that knowledge in a week or two. Posted Image
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#14 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 15 October 2011 - 02:34 PM

This is the function I use. I just call this at the end of main before returning:

void WaitExit(){
	using namespace std;
	cin.clear(); // just to ensure there's no junk in the stream
	cout << "\nPress ENTER to close...\n";
	cin.ignore(1,0); 
}


#15 f&g   Members   -  Reputation: 102

Like
0Likes
Like

Posted 15 October 2011 - 02:42 PM

Thank you for all the replys i do have this issue not all sorted i do know indeed that system("pause") is bad practice as this opens as new process witch i dont like the idea of once again thank you for all your help and advice

Regards

f&g




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS