Solved

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

Posted on 2004-04-26
12
212 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_
Comment Utility
I'd simply suggest running this program from the command prompt.
0
 
LVL 22

Expert Comment

by:NovaDenizen
Comment Utility
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
Comment Utility
system("pause"); also works well for this.
0
 

Author Comment

by:super_man_1976
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 10

Accepted Solution

by:
Sys_Prog earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
i think i know what you're talking about :-)

leflon
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now