- #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!