Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Learning C++, Need help with part of my code.

Hello Experts,
I'm New to programming and C++.  I've been working on a simple program for learning purposes.  Everything works fine, I'm just adding things and modifying it.  I'm using MS visual C++ 6 for a compiler and when click on Execute program it work fine.  But when I actually  double click the exe, the first question comes up, I input a number then it just flashes and goes away.  Is there a way to have display before the dos window exits?

int main
{
  int Pennies = 0;
  int OrgPennies  = 0;

  // This program will calculate any amount of Pennies into: $100's, $50's, $20's, $10's , $5's, 1's, Quarters, Dimes, Nickles and the remaining pennies.
  cin >> Pennies;
  // Possible error handling.
  OrgPennies = Pennies,

  // Splitting up the money
  int Bill100 = Pennies/10000;
  Pennies = Pennies%10000;.

  int Bill50 = Pennies/5000;
  Pennies = Pennies%5000;.

  int Bill20 = Pennies/2000;
  Pennies = Pennies%2000;.

  int Bill10 = Pennies/1000;
  Pennies = Pennies%1000;.

  int Bill5 = Pennies/500;
  Pennies = Pennies%1000;.

  int Bill1 = Pennies/100;
  Pennies = Pennies%100;.

  int Coin25 = Pennies/25;
  Pennies = Pennies%25;.

  int Coin10 = Pennies/10;
  Pennies = Pennies%10;.

  int Coin5 = Pennies/5;
  Pennies = Pennies%5;.

  int Coin1 = Pennies/1;
  Pennies = Pennies%1;.

  cout << "For " << OrgPennies << " You will get:" << endl;

  cout << Bill100 << "One Hundred dollar bills" << endl;

  cout << Bill50 << "Fifty dollar bills" << endl;

  cout << Bill20 << "Twenty dollar bills" << endl;

  cout << Bill10 << "Ten dollar bills" << endl;

  cout << Bill5 << "Five dollar bills" << endl;

  cout << Bill1 << "One dollar bills" << endl;

  cout << Coin25 << "Quarters" << endl;

  cout << Coin10 << "Dimes" << endl;

  cout << Coin5 << "Nickels" << endl;

  cout << Coin1 << "Pennies" << endl;

  cout << Pennies << "pennies" << endl;

  return 0;  // program end
}
0
super_man_1976
Asked:
super_man_1976
1 Solution
 
_corey_Commented:
I'd simply suggest running this program from the command prompt.
0
 
NovaDenizenCommented:
Before the "return 0" statement, add these three lines:
cout << "Hit enter to continue" << endl;
int unused;
cin >> unused;

0
 
mnashadkaCommented:
system("pause"); also works well for this.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
super_man_1976Author Commented:
NovaDenizen
I tried that and it works, but when I hit enter the program doesn't exit.  How would I do that?
0
 
jj819430Commented:
well, it is waiting for an input of type integer. so try hitting 3 and then enter. I bet it closes.
0
 
mnashadkaCommented:
system("pause"); will exit no matter what you type (space or otherwise), and cin.getline will exit with the enter key without typing anything else.  Good luck.
0
 
Sys_ProgCommented:
I completely agree with mnashadka

Should consider using
system ( "PAUSE" ) ;

You would have to include stdlib.h for the system () call

#include <stdlib.h>


Amit


0
 
leflonCommented:
hi super_man,

if you like staying with the C++ 'cin' you can try  something like

cout << "Hit enter to end prog!" << endl;
char c=0;
cin.getline(&c,1);

but i think either way works fine.
leflon
0
 
mnashadkaCommented:
leflon, that could be dangerous. If the user enters more than one character, you could get a gpf, or at least cause a memory fault. If you're going to use getline, you should at least allocate a little bit of space in case the user is trying to screw with you, or just plain dumb, or both.
0
 
leflonCommented:
@mnashadka,
i understand that this may not be the way it's supposed to be. but it is really dangerous? ok the fail() func returns true after the above getline call, but there is no memory corrupted (as far as i can see). i'm telling getline to just read one char, actually this will become no char at all cause c holds the '\0' after the call.
i'm just curious and like to learn, so is there a problem i don't see right now.

as said, this may not be the best way to do it. it just jumped to my fingers while reading this thread :-)

cheers
leflon
0
 
mnashadkaCommented:
You're right, I did miss the 1 character thing.  It's been a long morning already.  Sorry about that.  It's not dangerous.
0
 
leflonCommented:
i think i know what you're talking about :-)

leflon
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now