Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Write a program which emulates the final game of a famous price relat

ID: 638421 • Letter: O

Question

Objective:

Write a program which emulates the final game of a famous price related game show. The program must read from a file (prizeList.txt) and populate an array of 50 prizes. Each item has an associate name and price, which is in the file and also in the order named. The name and price are separated by a tab. The program must then randomly pick 5 items out of that array for the showcase showdown, in which repeated items are allowed. Then, the program must prompt the user with the names (individual prices hidden) of the prizes that have been randomly selected and ask them to enter the total cost closest without going over. Since there is not another contestant in this version of the game, the program must also check to see if the price guessed is within $2,000 of the actual retail price or else they lose as well. The game will continue until the user chooses to quit.

Suggested Methodology

You can solve this in any number of ways, and here

Explanation / Answer

#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>

using namespace std;

// prize and its value must be separated by space in input file.

class Prize{
   public:
       string name;
       int value;
};

int main(){
   ifstream infile;
   infile.open("prizeList.txt");
   string s;
   string temp;
   Prize** prize=new Prize*[50];
   int i = 0;
   while (!infile.eof()){
       getline(infile,s);      
       stringstream line(s);
       Prize *p = new Prize();
       line >> temp;
       p->name = temp;
       line >> temp;
       p->value = atoi(temp.c_str());
       prize[i] = p;
       i++;
   }
   srand (time(NULL));
   while (true){
       cout << "Welcome to the showcase show down! Your prizes are: ";
       int count = 0;
       for (int j = 0; j < 5; j++){
           i = rand() % 50;
           cout << prize[i]->name << endl;
           count += prize[i]->value;
       }
       int k;
       cout << "You must guess the total cost of all without going over Enter your guess ";
       cin >> k;
       if (count - 2000 <= k || k <= count+2000){
           cout << "You guessed " << k << " the actual price is " << count << endl;
           cout << "Your guess was under! You win! " << endl;
       }
       else{
           cout << "You guessed " << k << " the actual price is " << count << endl;
           cout << "I'm sorry but that guess was bad. You lose for being bad. " << endl;
       }
       string sh;
       cout << "Would you like to play again? Enter 'no' to quit" << endl;
       cin >> sh;
       if (sh == "no"){
           cout << "GOOD BYE ";
           break;
       }
   }
   return 0;
}