Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include / public / read . h #include #include #include #include using namespace std; void die ( ) { cout < < You

#include "/public/read.h"
#include
#include
#include
#include
using namespace std;
void die(){
cout << "You dun goofed.
";
exit(0);
}
int main(){
//This is equivalent to string filename; cout << "Please enter a filename
"; cin >> filename;
//But on one line instead of three, because I'm cool like that
string filename = read("Please enter a filename:
");
ifstream File(filename);
if (!File){
die();
}
int k;
File >> k;
if (File.fail()|| k <2){
die();
}
vector cases(k);
for (int i =0; i < k; i++){
File >> cases[i];
if (File.fail()){
die();
}
}
int value =0;
for (int i =0; i < k; i++){
value += cases[i];
}
while (true){
int b;
cout << "Please enter a briefcase to open:
";
cin >> b;
cout<<"That briefcase held "<< cases[b]<<" dollars
";
value -= cases[b];
cases[b]=-1;
k--;
//cout<<" I will offer you $"<< value/k<<" dollars to walk away.
";
if (b ==-1){
cout <<" You won "<< value / k <<" dollars!
";
break;
}
if (b <0|| b >= k || cases[b]==-1){
die();
}
/* cout << "That briefcase held "<< cases[b]<<" dollars
";
value -= cases[b];
cases[b]=-1;
k--;
*/
if (k ==1){
for (int i =0; i < cases.size(); i++){
if (cases[i]!=-1){
cout << "You won "<< cases[i]<<" dollars!
";
break;
}
}
}
}
cout <<" I will offer you $"<< value / k <<" dollars to walk away.
";
return 0;
}
For this assignment:
0) Prompt the user with: "Please enter a filename:
";
1) You will read a filename in from cin, which will be a single word.
You will open that file.
2) That file will contain:
The number of briefcases (N)(N must be >=2)
Followed by N dollar amounts, one on each line. ALL ARE POSITIVE INTS.
You will read that into a vector of ints of size N.
3) After that, you will go into an infinite loop:
3.0) Prompt the user with: "Please enter a briefcase to open:
";
3.1) The user will input a briefcase to open (an integer which will range
from 0 to N-1)
3.2) Print "That briefcase held X dollars"
3.3) You will then calculate the average value left in the cases.
Use INTEGER DIVISION. (I.e. if the cases hold 1,2,3,4, the average is 2.)
3.4) Output the average to the user: "I will offer you X dollars to walk
away.
"
The loop will terminate if the user ever types -1(indicating that a
deal is made) or if there is only one briefcase left.
In both cases, print to the screen how much money they won. This will either
be the last offer (if they took the deal) or the money in the last briefcase.
It will look like this: "You won X dollars!
"
If an error occurs, print out "You dun goofed.
" and quit.
Possible error cases you have to handle:
1) If you are told to open a file that doesn't exist
2) An error reading from the file like if it has letters, or ends before N
dollar amounts are read.
2) The user trying to open the same briefcase twice
3) The user trying to open a negative briefcase (other than -1) or a briefcase
higher than N-1.
4) If there are less than 2 briefcases to begin with.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

More Books

Students also viewed these Databases questions