Solved

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

Posted on 2004-04-26
12
214 Views
Last Modified: 2010-04-01
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
Comment
Question by:super_man_1976
12 Comments
 
LVL 8

Expert Comment

by:_corey_
ID: 10922752
I'd simply suggest running this program from the command prompt.
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 10922794
Before the "return 0" statement, add these three lines:
cout << "Hit enter to continue" << endl;
int unused;
cin >> unused;

0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10923341
system("pause"); also works well for this.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:super_man_1976
ID: 10923462
NovaDenizen
I tried that and it works, but when I hit enter the program doesn't exit.  How would I do that?
0
 
LVL 7

Expert Comment

by:jj819430
ID: 10924068
well, it is waiting for an input of type integer. so try hitting 3 and then enter. I bet it closes.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10924500
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
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 20 total points
ID: 10925279
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
 
LVL 7

Expert Comment

by:leflon
ID: 10926038
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 10929022
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
 
LVL 7

Expert Comment

by:leflon
ID: 10929530
@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
 
LVL 8

Expert Comment

by:mnashadka
ID: 10929554
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
 
LVL 7

Expert Comment

by:leflon
ID: 10932226
i think i know what you're talking about :-)

leflon
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question