The goal of this assignment is to write a program that calculates the odds of pr
ID: 3692538 • Letter: T
Question
The goal of this assignment is to write a program that calculates the odds of profiting if you played several scratch-off lottery games.
Your program should start by asking the user to enter, at the console, the lowest amount that they would like to profit when playing the lottery. Read this value in as an integer. Your program should then prompt the user for anoutput file name, where a formatted table with our results will be written:
Enter the lowest dollar amount that you would like to profit: 10
Enter the output file name: output.txt
Generating report...
Once the minimum profit is known, you can compute the odds of profiting from each game. Your program should read in data from an input file. As your program reads each scratcher game, compute the odds and write them in the same order in a nicely formatted table to the output file. This means you aren’t required to use structures, sorting, or vectors, but they aren’t prohibited.
Input File Format:
Your program must read information about the scratcher games from a text file named scratcher.txt. The file will consist of several multi-line "blocks", where each block represents a scratcher game. Each block will be formatted as follows:
• The first line will contain only the name of the game. The name may contain any kind of character – letters, digits, punctuation, or spaces. Hint: use getline(), you may need to use ws to consume additional whitespace before re-using get line.
• The second line contains two integers. The first is the cost of the ticket in dollars; the second is the number of possible prizes that can be won in that scratcher. The number of prizes has been given so that you can easily use a for-loop to read the following lines.
• After the second line, there is one line for each prize on the scratcher (in other words, if you call the second integer on the line above N, then there are N lines). Each prize line contains three integers. The first is the value of the prize in dollars; the second is the total number of tickets that were printed with that prize; the third is the number of tickets with that prize that have not yet been claimed.
• After the last prize line, there will be a blank line, and then a new block will start (or you've reached the end of the file).
Example Input and Output:
Below is an example input file with three games. You can use this during your own development.
SMALL BEANS
1 5
10000 10 10
5000 50 8
100 100 6
10 1000 4
1 10000 2
PIRATE'S BOOTY, ARRR
10 6
50000 20 0
10000 100 0
1000 500 0
100 2000 738
10 7500 2945
1 10000 4476
BIG MONEY HU$TLA$
20 7
1000000 10 7
500000 50 29
10000 100 78
1000 500 396
100 2000 1439
20 5000 3218
10 10000 6210
The first game is called "SMALL BEANS", a $1 game with 5 possible prizes: $10,000, $5,000, $100, $10, and $1. For the $10,000 prize, 10 tickets were printed and 10 of them have not yet been claimed; for the $5,000 prize, 50 tickets were printed and there are 8 left; and so on. The other two games are designed similarly.
Your program should start by prompting the user to enter a dollar amount that is the smallest amount that they want to profit (meaning that the prize must be larger than the profit plus the cost of the ticket).
Once you know the desired profit (and prompt for the output file name), you can read the games from the input file. For each game, you should print a neatly formatted line of output to the file that includes the following information:
• The name of the game, exactly as it was read from the input file.
• The cost of a ticket for that game, preceded with a dollar sign (you may have spaces between the dollar sign and the amount, for alignment purposes).
• The odds of winning your desired profit in that game, written as "1 in N", where N will be computed as described below. The denominator should be printed to at least two decimal places. If it is not possible to profit from that game, print "Not possible" instead of the odds.
To compute the odds, you need to look at the information for each of the prizes on that scratcher. First, you need to compute a running sum of total number of remaining tickets for that game, regardless of prize value. Then, you also need to compute a running sum of the total number of remaining tickets that would result in a profit; in other words, the number of remaining tickets where the prize value is greater than the desired profit plus the cost of the ticket.
Then, we want the odds to be "1 in something", so we compute the "something" by dividing the total number of remaining tickets by the number of remaining profitable tickets. (Notice that this is the opposite of what we would do if we were computing the probability as a percentage. Also note that we must take special care that we handle the "Not possible" case correctly, since that would involve dividing by zero.)
For the example input file above, imagine that the user said that they wanted to see what their odds were of profiting $1,000 on any of those games. Your output file should look something like this:
Game Cost Odds
----------------------------------------------------------------------------------
SMALL BEANS $ 1 1 in 1.67
PIRATE'S BOOTY, ARRR $10 Not possible
BIG MONEY HU$TLA$ $20 1 in 99.80
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// Declaring variables
int lowestAmountOfProfit;
char filename[]="scratcher.txt";
string gameName;
int CostOfTicket, NumberOfPrizes;
int PrizeValue, NumberOfTickets, TicketsNotClaimed;
double RemainingTickets;
double RemainingTicketsForProfit;
double odds;
// The program will ask the user to enter the lowest amount they would like to
// profit when playing the lottery.
cout << "Enter the lowest dollar amount that you would like to profit: "
cin >> lowestAmountOfProfit;
cout << "Enter the output file name: "
cout << "Generating report..."
//open input file
ifstream fin;
fin.open(filename);
//if input file does not exist
if (!fin)
{
// If the input file cannot be found.
cout << "Input file does not exist." << endl;
return 0;
}
//How the output displace will be formatted.
cout << left << setw(25) << "Game" << setw(10) << "Cost" << setw (10) << "Odds" << endl;
cout << "-----------------------------------------------" << endl;
// Reads the name of the game
while (getline(fin, gameName))
{
fin >> CostOfTicket; // Reads the cost of ticket
fin >> NumberOfPrizes; // Reads the number of prizes
RemainingTickets = 0;
RemainingTicketsForProfit = 0;
for (int i = 0; i < NumberOfPrizes; i++)
{
fin >> PrizeValue; // Reads the prize value
fin >> NumberOfTickets; // Reads the total number of tickets
fin >> TicketsNotClaimed; // Reads number tickets not claimed
//regardless of prize value*/
// The following line computes the running sum of the number of tickets remaining for that game.
RemainingTicketsForProfit = RemainingTicketsForProfit + TicketsNotClaimed;
// The next line with compute a sum of the number of remaining tickets where the user would profit.
if (PrizeValue > lowestAmountOfProfit)
{
RemainingTickets = RemainingTickets + TicketsNotClaimed;
}
}
// Tells program what to do if there are zero remaining tickets
if (RemainingTickets==0)
{
// Formats the output
cout << left << setw(25) << gameName << setw (2) << "$" << CostOfTicket << right << setw(15) << "Not possible" << endl;
}
else
{
// Tells the program to calculate the odds
odds = RemainingTicketsForProfit / RemainingTickets;
cout << left << setw(25) << gameName << setw (2) << "$" << CostOfTicket << right << setw(15) << "1 in " << setprecision(2) << fixed << odds << endl;
}
// Read the blank line
string blankLine;
fin >> blankLine;
}
// Close the input file
fin.close();
return 0;
}
note- the above code can help to answer the given question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.