The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++ help

BushiBushi Registered User regular
edited September 2008 in Help / Advice Forum
I'm have an assignment to make a simple C++ calculator, but I've hit some kind of snag I can't find. The program accepts all of the input fine, but it doesn't explain any of the results afterwards, it just closes the program. Here's the code:

#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string name;
float pay;
float hours;
cout << " Hello welcome to the Pay Calculator Program made by: James.\n " ;
cout << " \tPlease enter your name: " ;
cin >> name;
cout << endl;
cout << " \tPlease enter your payrate: ";
cin >> pay;
cout << endl;
cout << " \tPlease enter the hours worked: ";
cin >> hours;
cout << endl;
cout << " \tHello " << name << "!\n";
cout << " \tYou worked " << hours << "hours at $" << pay << "per hour.\n";
cout << " \tYour salary will be $" << hours * pay << ".\n" <<endl;
return 0;
}

What am I missing here? It complies and executes fine, and the debugger says it's fine, I just can't see any of the output.

Bushi on

Posts

  • TheGreat2ndTheGreat2nd Registered User regular
    edited September 2008
    before the last line
    return 0;

    you'll want to put in a while loop to keep the window open
    so, before the last line, put in this piece of code
    while(1) {}
    return 0;

    TheGreat2nd on
    BinghamtonUniversity.png
    I'm Jacob Wilson. | facebook | thegreat2nd | [url="aim:goim?screenname=TheGreatSecond&message=Hello+from+the+Penny+Arcade+Forums!"]aim[/url]
  • Jimmy KingJimmy King Registered User regular
    edited September 2008
    I'm pretty sure what you are running into is a setting in windows which says to close the command prompt window as soon as the running app finishes. I don't remember where you change that and am not finding it at first glance.

    A quick workaround is to just stick another line at the end of the app waiting for you to press a key.

    Jimmy King on
  • BushiBushi Registered User regular
    edited September 2008
    I knew I was missing something, thanks guys. Everything's running fine now, give or take some minor formatting errors. Do cin.ignore and cin.get have to be used together, or can I just use cin.get?

    Bushi on
  • Q_PrimeQ_Prime Registered User regular
    edited September 2008
    if you run the program from cmd prompt it should display everything without closing.. the other option is putting in an extra cin and just display a message before that, that says "press enter to close the program"

    Q_Prime on
Sign In or Register to comment.