[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-04-26
12
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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!

 

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 80 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

656 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