1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with coin game C++ assignment?

  1. May 31, 2017 #1
    1. The problem statement, all variables and given/known data
    Here is the prompt:
    Write a program that does the following:

    • Welcome the user to the game and ask them if they want to play. They should type y or n for yes or no. Store this value in a char variable. If they type y, continue on with the game. If they type n, print the appropriate message and exit (See sample runs). If they type some other character, print the error message (see sample runs) and abort the program).
    • If the user said they wanted to play continue with the game as such: prompt them for a dollar amount (as a double) that they will attempt to match with their coin entries.
    • Allow the user to enter an integer amount for the number of half dollars, quarters, dimes, nickels, and pennies that they'd like. If any of the values they enter for the number of each coin is negative, display an error message and exit the program. (You may ONLY have one return statement in your code - the one at the end of main, and no exit statements are allowed. This means you need to structure if/elseif/else stuff so that your code will naturally flow to the end if an error condition is encountered).
    • If all of the values they entered are valid, check to see if the user won the game. Add up the total amount of $ they entered and compare it against the value they entered when the program began.
    • If it's the same and they won, print out: "YOU WIN!". If they lost, print out "YOU LOST by: " and then the amount of $ that they were off by
    • You may assume the user will enter a valid dollar amount in the form of dd.cc when asked what $ amount they want to match in the beginning of the program
    • All dollar values printed must be printed in dollar format like $dd.cc (to two decimal places).

    2. Relevant equations


    3. The attempt at a solution
    Here is the program I have currently:
    Code (Text):
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    int main()
    {

        char y;//yes
        char n;//no
        char x; //generic user entry
        double h = 0.50; // half dollars
        double q = 0.25; // quarters
        double d = 0.10; // dimes
        double m = 0.05; // nickels
        double p = 0.01; // pennies
        double dollar;
        cout << "Would you like to play? (y or n)--> ";
        cin >> x;
        if (x==y)
        {
                cout << "What amount of $ would you like to match? --> ";
                cin >> dollar;
                cout << fixed << showpoint << setprecision(2) << "Okay! You need to match the value of $" << dollar << '\n';
                cout << "Enter the number of: ";
                cout << "\n \tHalf dollars: ";
                cin >> h;
                cout << " \tQuarters: ";
                cin >> q;
                cout << " \tDimes: ";
                cin >> d;
                cout << " \tNickels: ";
                cin >> m;
                cout << " \tPennies: ";
                cin >> p;
            if (h >= 0 && q >= 0 && d >= 0 && m >= 0 && p >= 0)
            {
                if (h + q + d + m + p == dollar)
                {
                    cout << "You WIN!\n See you next time!" << endl;
                }
                else
                {
                    cout << fixed << showpoint << setprecision(2) << "You LOST by " << dollar - p - m - d - q - h << endl;
                    cout << "See you next time!";
                }
            }
        else
        {
            cout << "You can't enter a negative number of coins. Aborting game." << endl;
            return 0;
        }
    }
        if (x == n)
        {
            cout << "That's okay. Have a good day.";
            return 0;
        }
        if (x != y && x != n)
        {
            cout << "Invalid entry, aborting program." << endl;
        }
            return 0;

    }
    It is telling me that I have an uninitialized variable y and n. I know this is because I did not put cin>>y or cin >>n. My issue with this is that I'm not sure how to correctly prompt the user to enter the values y or n and get the correct sequence afterwards. I can get to the point where if the user types 'y' the game will begin and if the user types anything other than y the game will shut off. (Actually, I have been able to do this once... I've been messing with the code and haven't been able to do this again but I can probably figure it out.) I believe the issue lies in how I am going about the if statements. I can run the program and have the user enter y and the game will run. If I run the program and have the user enter n, however, the game does nothing and doesn't move down to the prompt that can be seen towards the bottom of the program.
    Another issue I am having is that when the user it prompted to enter the half dollar and quarter amount etc. the amount which I declared to each double (Should this be an int?) does not work but instead the actual number entered is what is taken into account within the formula to verify whether the user has won or lost. What I mean by this is that the user will enter, say, "2" for the half dollar amount. Instead of the program seeing this as $1.00 in the formula h + q + d + m + p == dollar, it sees it as the actual number 2 and hence the user always loses.

    Any tips? This is homework - I do not want the answer. I just need guidance. Thank you!
     
  2. jcsd
  3. May 31, 2017 #2

    Drakkith

    User Avatar

    Staff: Mentor

    Variables Y and N have nothing assigned to them, so you can't compare variable X to variables Y and N (or, at best, you'll be comparing X to a random value that happens to be stored in the memory address of Y).

    Typing "char y" doesn't store the character "y" in that variable, it creates a character variable whose name is "y".

    Do you know what "literals" are?
     
  4. May 31, 2017 #3
    Okay. I've done what you are saying minus the quotes. I now have that char x = y,n. I am told that the variables y and n are uninitialized.
     
  5. May 31, 2017 #4
    Wow, I don't know what's happening. Even ridding of y and n completely in the program this tells me that they are uninitialized variables.
     
  6. May 31, 2017 #5

    Drakkith

    User Avatar

    Staff: Mentor

    Bleh, I didn't realize this was homework. I shouldn't have given you the answer, but I suppose it's too late now. :rolleyes:

    That's correct. They are uninitialized. But since you can check against a literal you don't even need them.
    Note that I made a mistake in my previous response (that I edited out right before you replied). Character literals are declared with single quotes, not double quotes.
    See here: https://www.tutorialspoint.com/cplusplus/cpp_constants_literals.htm

    Yes, because you're overriding the original values stored in those variables and then adding the new values, which are literally the numbers the user just entered.
    If the user enters "2" for the number of half dollars, you can't simply add that value to the others. You need to do convert it into the correct value. Hint: if the user enters "5", what's the actual amount of money that's equal to and what calculation do you need to do to convert it?

    You have more than one return statement in your code.
     
  7. May 31, 2017 #6
    Holy crap I got it! I'll be back if I need more help. Thank you man.
     
  8. Jun 1, 2017 #7

    Mark44

    Staff: Mentor

    In addition to what was already said about your variables y and n, you only need one variable to hold the user's choice.

    Also, and related to the above, the variables h, q, d, m (why not n?), and p should represent the number of each of these types of coins, not their values. These also should be int variables, not double. No one is going to ask for 3.27 nickels.

    Don't confuse the number of coins with their value.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted