Simple Program Counting Coins. What is Wrong With My Logic. Never Programmed Before.

This is a discussion on Simple Program Counting Coins. What is Wrong With My Logic. Never Programmed Before. within the C++ Programming forums, part of the General Programming Boards category; Hi Everyone - This is my first post here, and I made sure to use the search utility before posting ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Simple Program Counting Coins. What is Wrong With My Logic. Never Programmed Before.

    Hi Everyone -

    This is my first post here, and I made sure to use the search utility before posting this.

    Today, we were given our first programming assignement on the first day of class. The class is intro to programming, and we are using the C++ language which apparently is the best out there.

    Well, we were given a program to count coins and from the limited knowledge I have with C++ ( I never progammed before until today ) I am doing everything correctly except for where I am computing the value of my coins.

    Could anyone give me a clue on...
    1) what I am doing wrong?
    2) and how to convert my total value of money into a dollar amount such as if I had 8 quarterrs and 12 pennies it would display $2.12

    Thanks for any assistance provided.


    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	// used int data type because you can never have a fraction of a coin , ideally
    	int quarters, dimes, nickles, pennies;
    	quarters = dimes = nickles = pennies = 0;
    
    
    	cout << "Enter the number of quarters:";
    	cin  >> quarters;
    	cout << "Enter the number of dimes   :";
    	cin  >> dimes   ;
    	cout << "Enter the number of nickles :";
    	cin  >> nickles ;
    	cout << "Enter the number of pennies :";
    	cin  >> pennies ;
    
    
    	cout << quarters << " " << dimes << " " << nickles << " " << pennies << " " << endl << endl << endl << endl;
    
    	float money = 0.0 // amount of money i have
    	money = (quarters * .25) + (dimes * .10) + (nickles * .5) + (pennies * .1);
    
        cout << "you have this amount        :" << money << endl;
    
    	return 0;
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    There are two mistakes
    First, you forgot the semicolon at the end of
    Code:
    float money = 0.0 // amount of money i have
    Second
    Code:
    money = (quarters * .25) + (dimes * .10) + (nickles * .5) + (pennies * .1);
    should read
    Code:
    money = (quarters * .25) + (dimes * .10) + (nickles * .05) + (pennies * .01);

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    WoW!

    Thanks for the prompt reply. I am amazed.

    I had a semicolon at

    Code:
    float money = 0.0;
    but unsure what happend to it when I pasted in my code.


    I now see my mistake. I feel so silly and dumb for making it.

    Thank you.

  4. #4
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    First off, dont use more than 1 endl ever. Use "\n" if you want to add a verticle space, but dont use several endl's because it becomes useless to flush the buffer so many times.

    Ok, first:

    Code:
    	float money = 0.0 // amount of money i have
    	money = (quarters * .25) + (dimes * .10) + (nickles * .5) + (pennies * .1);
    This is bad. You are attempting to turn an int into a float. (Why are you using a float? Use a double, its more accurate.) You have to cast an int into a float before you can use it in equasions with floating numbers! The simplest way to fix this is to convert all your ints to floats (or doubles). Also, the way you use cin >> to get number input is very bad. If the user types in a letter, cin wont understand it.

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main() {
    	double quarters, dimes, nickles, pennies;
    
    	cout << "Enter the number of quarters:";
    	cin  >> quarters;
    	cout << "\nEnter the number of dimes   :";
    	cin  >> dimes   ;
    	cout << "\nEnter the number of nickles :";
    	cin  >> nickles ;
    	cout << "\nEnter the number of pennies :";
    	cin  >> pennies ;
    
    
    	cout << quarters << " " << dimes << " " << nickles << " " << pennies << " " << endl;
    
    	float money = 0.0; // amount of money i have
    	money = (quarters * .25) + (dimes * .10) + (nickles * .05) + (pennies * .01);
    
        cout << "you have this amount        : $" << money << endl;
    
    	return 0;
    }
    That should work, I know you cant have a fraction of a coin, but you have to cast int's to float's before you can multiply them by decimals, I would tell you how, but I dont remember -,-.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> You have to cast an int into a float before you can use it in equasions with floating numbers!

    No, you don't have to. When you are dividing ints and you want a floating point result, you should cast one of them to a double first, but in this case it isn't necessary.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Why are you using a float? Use a double, its more accurate.
    Better yet, dont use any floating point for money at all.

    Use an integer to represent the number of cents, and use /100 and %100 to extract the $ and cent amounts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Replies: 5
    Last Post: 01-13-2007, 01:14 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21