Jump to content
  • Advertisement
Sign in to follow this  
Maytridy

Code problem...(beginner's question)

This topic is 4830 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

Hey guys, Before I say anything, I'd just like to let you guys know that I'm a total newb and I have no programming experience whatsoever. I just purchased Beginning C++ Programming by Michael Dawson today, and I just completed chapter one. I was doing the last exercise, which was to write a program that took 3 scores and gave the average. It's all working well, except when I enter the 3rd score, the console just closes immediately. The same thing happens when I enter my name into Michael's "Lost Fortune" program. I thought at first that I made an error in my code, but when I couldn't find it I took a look at Michael's and his did the same thing. Do you guys have any idea what's going on?? Here's my code:
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    int score_1, score_2, score_3,;
    string you;
    
    cout << "Please enter your name ";
    cin >> you;
    
    cout << "Please give me your first score: ";
    cin >> score_1;
    
    cout << "Please give me your second score: ";
    cin >> score_2;
    
    cout << "Please give me your third score: ";
    cin >> score_3;
    
    cout << you << "has an average of " << (score_1 + score_2 + score_3) / 3 << "Yay!";
    
    cout << "\nPress Enter to exit!";
    cin.ignore(cin.rdbuf()->in_avail() + 1);
    
    
    
	return 0;
}





P.S.: I included the
cout << "\nPress Enter to exit!";
    cin.ignore(cin.rdbuf()->in_avail() + 1);


because Michael said that it would prevent the program from performing the calculations then automatically closing, but it doesn't seem to work. If you guys could spot the problem, that would be great! Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Well, I took the comma out and added the system("PAUSE") and one of the two fixed it, thanks a lot for the quick fix guys!!

Share this post


Link to post
Share on other sites
The pause is what's doing it. An extra comma in that position won't cause a problem (although it looks a bit sloppy).

The cin.ignore() bit is supposed to induce the same sort of pause, but apparently it doesn't work so reliably. o_O

However, artificially pausing a program at the end is not necessarily a good idea, especially when it's done in that way (invoking another program via a system() call - which assumes that the program is there, with that name, and does what you want it to). If you do want to invoke a separate program to put in a pause, a better way is to use a batch file (.bat - I assume Windows; on Linux you can get the same effect with a .sh file). You will have more flexibility that way, and make it easier for other scripts to call *your* program and work with it (though for games this benefit is rather dubious :) ).

In any event, try not running the program from the IDE, but instead opening a console window in Windows and running the program from there. The window will persist. (What's happening with Dev-Cpp or whatever you're actually using is that it is effectively making its own system() call to run your program, and since that window is no longer useful at the end of your program run, Windows closes it - before you really get to see the last bit of output.)

Share this post


Link to post
Share on other sites
You could also simulate you own pause using conio.


#include <cinio.h>

int main(int argc, char* argv[])
{
/* stuff */

bool exit = false;

while (!exit)
{
std::cout << "Press any key to continue" << std::end;

if (kbhit())
{
exit = true;
}
}
}




That's if you wanted to that is..:)

Not compiled or tested above, but looks okay.

Share this post


Link to post
Share on other sites
Quote:
Original post by FalconDragoon
an easier way to do it using conio would be to just add:


Cout<< "Press A Key to continue";
Getch();


Yeah, that one or cin.ignore() are the ones I generally use.

The problem is just that the program is supposed to run in a console, so when you start it, it create a console, the program runs, and once the program is finished, there's no need for the console, so it closes again immediately. [wink]

Easiest solution is one of the above, which simply makes the program pause just before the end.

An alternative is to actually open a command console yourself, and start the program from there. Then you'll just get the command prompt after the program finishes, and all the output will still be visible above it.

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!