My C++ program... need help.

My C++ program... need help.

Post by Zelth on Wed Oct 29, 2008 7:29 pm
([msg=14253]see My C++ program... need help.[/msg])

Hey, so I've been learning C++ for about... a week? Around there, anyways... I'm writing this program that, when given dollars and cents, it outputs the amount of hundred dollar bills, fifty dollar bills... all the way till the pennies. (Canadian money :D)

There still this bug that when I enter a character like 'a' as "dollar" or "cents" it'll go on forever. I don't know how to fix that. :cry: So here's the code :
Code: Select all
#include <iostream>
using namespace std;

// Canadian money

int dollar; // Amount in dollars
int cents; // amount in cents
int five; // amount of five dollar bills
int ten; // amount of ten dollar bills
int twenty; // amount of twenty dollar bills
int fifty; // amount of fifty dollar bills
int hundred; // amount of hundred dollar bills
int toonies; // amount of toonies
int loonies; // amount of loonies
int quarters; // amount of quarters
int dimes; // amount of dimes
int nickels; // amount of nickels
int pennies; // amount of pennies
char sign;

int main()
{
    while (true) {
          cout << "Enter amount in dollars and cents"; // Put $ sign in front of dollar separated by space
          cout << " or type 'q' to quit: ";
          cin >> sign >> dollar >> cents; // Gets the values of "sign", "dollar" and "cents"
          cin.ignore();
         
          hundred = dollar / 100;
          fifty = (dollar % 100) / 50;
          twenty = ((dollar % 100) % 50) / 20;
          ten = (((dollar % 100) % 50) % 20) / 10;
          five = ((((dollar % 100) % 50) % 20) % 10) / 5;
          toonies = (((((dollar % 100) % 50) % 20) % 10) % 5) / 2;
          loonies = (((((dollar % 100) % 50) % 20) % 10) % 5) % 2;
          quarters = cents / 25;
          dimes = (cents % 25) / 10;
          nickels = ((cents % 25) % 10) / 5;
          pennies = ((cents % 25) % 10) % 5;
         
          if ((sign == '$') && (dollar >= 0) && (cents >= 0)) {
                      cout << '\n' << dollar << " dollars and " << cents << " cents = \n";
                      cout << hundred << " hundred dollar bills\n";
                      cout << fifty << " fifty dollar bills\n";
                      cout << twenty << " twenty dollar bills\n";
                      cout << ten << " ten dollar bills\n";
                      cout << five << " five dollar bills\n";
                      cout << toonies << " toonies\n";
                      cout << loonies << " loonies\n";
                      cout << quarters << " quarters\n";
                      cout << dimes << " dimes\n";
                      cout << nickels << " nickels\n";
                      cout << pennies << " pennies\n\n";
                      cout << "------------------------------------------------------\n\n";
          }
          else if ((sign == 'q') || (sign == 'Q')) {
               break;
          }
          else if (sign != '$') {
               cout << '\n' << "Not a valid sign, please try again\n\n";
               cout << "-------------------------------------------------\n\n";
          }
          else {
               break;
          }
    }
}


Thanks for any help :D
Z~Epsilon
Newbie Programmer
Learned: HTML, XHTML, CSS
Learning: PHP, C++
Zelth
Poster
Poster
 
Posts: 149
Joined: Mon Sep 08, 2008 7:45 pm
Blog: View Blog (0)


Re: My C++ program... need help.

Post by 193zaitsev on Wed Oct 29, 2008 8:24 pm
([msg=14255]see Re: My C++ program... need help.[/msg])

If the "number" is a character or starts with a character instead of a number, cin.fail() will return true. So you can just check that.
193zaitsev
New User
New User
 
Posts: 36
Joined: Wed May 21, 2008 10:28 pm
Location: USA
Blog: View Blog (0)


Re: My C++ program... need help.

Post by Kurushimi on Wed Oct 29, 2008 8:27 pm
([msg=14256]see Re: My C++ program... need help.[/msg])

Entering a letter (or any non-numeric character) in place of a number will cause an error. It causes the console stream to enter a "failed state". As the person above me said, you can check function to prevent the error.

Code: Select all
else if (sign != '$') {
               cout << '\n' << "Not a valid sign, please try again\n\n";
               cout << "-------------------------------------------------\n\n";
          }
          else {
               break;
          }


Now, you don't need that last else statement. The others cover every possible scenario. In fact, in the top else statement I have quoted, you don't need the "if(sign != '$') ". Just else would work just fine. And this is jut a personal recommendation. When using while statements, bool variable in as the argument. And instead of break you can just change it to false.
Kurushimi
New User
New User
 
Posts: 17
Joined: Thu Oct 23, 2008 6:44 pm
Blog: View Blog (0)


Re: My C++ program... need help.

Post by Zelth on Wed Oct 29, 2008 9:00 pm
([msg=14260]see Re: My C++ program... need help.[/msg])

Sorry, but I didn't learn cin.fail() yet :( Where am I supposed to add it?

EDIT: Oh and thank you for telling me about the unnecessary else statement at the end. :D
Z~Epsilon
Newbie Programmer
Learned: HTML, XHTML, CSS
Learning: PHP, C++
Zelth
Poster
Poster
 
Posts: 149
Joined: Mon Sep 08, 2008 7:45 pm
Blog: View Blog (0)


Re: My C++ program... need help.

Post by 193zaitsev on Mon Nov 03, 2008 1:26 am
([msg=14405]see Re: My C++ program... need help.[/msg])

cin.fail() works like this

Code: Select all
int myInt = 1234;        //Initialized for example below
cin >> myInt;
if (cin.fail())
   cout << "Integer was not entered: " << myInt << endl;
else
   cout << "You entered an integer: " << myInt << endl;


There is a downside to this though
Let show three cases.

Input 1: Correct input
> 1000
You entered an integer: 1000


Input 2: Starting with non-integer
> a1258
Integer was not entered: 1234

When the inputed "integer" does not start with an integer value, cin.fail() returns true and you could exit or break inside that if statement. It can also be noted that if cin fails, the value in the variable stays what it was before, it is not changed.

The bad case
Input 3: Non-integer
> 12a34
You entered an integer: 12

This is the bad case. If the non-digit is in the middle of the input, cin will NOT fail (cin.fail() is false) and you integer will be the value before the non-digit.

If you don't need to worry about the last case, this will work fine. If you do want to account for it, you'll need another method. Off the top of my head, I would suggest getting the integer value from cin to a string variable instead of a integer, checking each character if it's a digit, then changing the string to an int with atoi(). There may be a more efficient way though.
193zaitsev
New User
New User
 
Posts: 36
Joined: Wed May 21, 2008 10:28 pm
Location: USA
Blog: View Blog (0)


Re: My C++ program... need help.

Post by Zelth on Mon Nov 03, 2008 5:26 pm
([msg=14440]see Re: My C++ program... need help.[/msg])

Oh okay, I get it now. Thanks a lot :P
Z~Epsilon
Newbie Programmer
Learned: HTML, XHTML, CSS
Learning: PHP, C++
Zelth
Poster
Poster
 
Posts: 149
Joined: Mon Sep 08, 2008 7:45 pm
Blog: View Blog (0)


Re: My C++ program... need help.

Post by alchemydragon on Tue Nov 04, 2008 9:29 am
([msg=14461]see Re: My C++ program... need help.[/msg])

Two things i saw are first in you part that says "while(true)" that will pretty much cause it to loop because it is not really testing it against anything. Second there is no return 0; at the end on a line right before the closing } for the int main statement.
alchemydragon
New User
New User
 
Posts: 13
Joined: Thu Oct 30, 2008 9:23 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron