Public Group

# Code problem...(beginner's question)

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

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

cin >> you;

cin >> score_1;

cin >> score_2;

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 on other sites
Try putting system("PAUSE"); at the end of the program, before the return.

##### Share on other sites
It works fine for me, but you should remove the last comma in this section ->
int score_1, score_2, score_3,;

##### Share on other sites
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 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 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 on other sites
an easier way to do it using conio would be to just add:

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

##### Share on other sites
Quote:
 Original post by FalconDragoonan 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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 13
• 9
• 11
• 15