Help with coin game C++ assignment?

In summary: Incorrect input. Please enter a number between 1 and 20."; cin >> dollar; } }If the user types in n, the program prints out the following:In summary, if the user types in n, the program prints out the following:You Lose by: 5
  • #1
Vitani11
275
3

Homework Statement


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).

Homework Equations

The Attempt at a Solution


Here is the program I have currently:
Code:
#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!
 
Physics news on Phys.org
  • #2
Vitani11 said:
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.

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?
 
  • Like
Likes Vitani11
  • #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.
 
  • #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.
 
  • #5
Vitani11 said:
Okay. I've done what you are saying minus the quotes.

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:

Vitani11 said:
I now have that char x = y,n. I am told that the variables y and n are uninitialized.

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

Vitani11 said:
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.

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?

Vitani11 said:
(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).

You have more than one return statement in your code.
 
  • Like
Likes Vitani11
  • #6
Holy crap I got it! I'll be back if I need more help. Thank you man.
 
  • #7
Vitani11 said:
C:
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
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.
 

1. How do I start the coin game C++ assignment?

To start the coin game C++ assignment, you will need to familiarize yourself with the rules of the game and the requirements of the assignment. Then, you can begin by setting up the necessary variables and functions, and creating the game logic.

2. How can I generate random numbers for the coin game?

There are several ways to generate random numbers in C++. One option is to use the rand() function from the cstdlib library. Another option is to use the random header from the C++11 standard library.

3. How do I handle user input for the coin game?

To handle user input for the coin game, you can use the cin and cout functions from the iostream library. These functions allow you to read and write data from the user through the console.

4. How do I keep track of the score in the coin game?

You can keep track of the score in the coin game by creating a variable to store the score and updating it after each round of the game. You can also display the current score to the user using the cout function.

5. What is the best way to debug my coin game C++ code?

The best way to debug your coin game C++ code is to use a debugger tool. This will allow you to step through your code and see where any errors or issues may be occurring. You can also use cout statements to print out the values of variables at different points in your code to help identify any problems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
759
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top