1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
//Homework 5
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
ifstream in_stream;
ofstream out_stream;
void read_write_out_file();
void in_file(ifstream& in_stream);
void out_file(ofstream& out_stream);
void calculate_frame_scores();
void display_player_score();
char frame_score;
char score_board;
void in_file(ifstream& in_stream)
{
in_stream.open("frame_score.dat");
if(in_stream.fail()) //check to see if file exists
{
cout << "Input file failed to open, program will now exit!\n";
cout << endl << endl;
system("PAUSE");
exit(1);
}
}
void out_file(ofstream& out_stream)
{
out_stream.open("score_board.txt");
if(out_stream.fail()) //check to see if file exists
{
cout << "Output file failed to open, program will now exit!\n";
cout << endl << endl;
system("PAUSE");
exit(1);
}
}
int main()
{
in_file(in_stream);
out_file(out_stream);
cout << "A set of raw bowling scores has been taken from an input file.\n" << endl << endl;
cout << "And the final score will be calculated.\n" << endl << endl;
cout << "The calculated final score will be written to an output file.\n" << endl << endl;
cout << "From the raw bowling score, the running totals are: \n" << score_board << endl;
cout << "FRAME: 1 2 3 4 5 6 7 8 9 10" << endl;
read_write_out_file();
display_player_score();
calculate_frame_scores();
out_stream.close();
in_stream.close();
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void read_write_out_file()
{
ofstream outstream;
outstream.open("bowling_score.txt");
int frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8, frame9, frame10;
in_stream >> frame1 >> frame2 >> frame3 >> frame4 >> frame5 >> frame6 >> frame7 >> frame8 >> frame9 >> frame10;
if(outstream.is_open())
{
outstream << "10" << endl;
outstream << "7" << setw(3) << "3" << endl;
outstream << "7" << setw(3) << "2" << endl;
outstream << "9" << setw(3) << "1" << endl;
outstream << "10" << endl;
outstream << "10" << endl;
outstream << "10" << endl;
outstream << "2" << setw(3) << "3" << endl;
outstream << "6" << setw(3) << "4" << endl;
outstream << "7" << setw(2) << "3" << setw(2) << "3" << endl << endl << endl;
}
else
cout << "Sorry, unable to open file." << endl << endl;
}
void calculate_frame_scores()
{
ifstream instream;
in_file(instream);
out_stream << "SCORE: ";
cout << "SCORE: ";
int total = 0;
int count = 0;
int next, current, newnext;
instream >> current;
instream >> next;
while(count < 10)
{
if(current==10) //calculate strike score
{
instream >> newnext;
total = total - current + next + newnext;
current = next;
next = newnext;
}
else if(current + next >= 10) //calculate spare score
{
instream >>newnext;
total = total + current + next + newnext;
current = newnext;
instream >> next;
}
else //calculate open frame score
{
total = total + current - next;
instream >> current >> next;
}
out_stream << total << " " << " ";
cout << total << " " << " ";
count++;
}
}
void display_player_score()
{
ifstream instream;
in_file(instream);
out_stream << "PLAYER: ";
cout << "PLAYER: ";
int count = 0;
int next, current, newnext;
instream >> current;
instream >> next;
while(count < 10)
{
if(current==10)
{
out_stream << "x";
cout << "x";
instream >> newnext;
current = next;
next = newnext;
}
else if(current + next >=10)
{
out_stream << current << "/";
cout << current << "/";
instream >> newnext;
if(count==9)
{
out_stream << newnext;
cout << newnext;
}
out_stream << " ";
cout << " ";
current = newnext;
instream >> next;
}
else
{
out_stream << current << " " << next << " ";
cout << current << " " << next << " ";
instream >> current >> next;
}
out_stream << endl;
cout << endl;
}
}
|