Assignment critique and question.

I wanted to let people check to see if I did anything silly or unnecessary.

Also I had a question, one of the requirements is that the first three integers of the input file which looks like this, 0 777 0 4 1 1 team1, can be as large as 100,000. Does that mean I have to store them in long ints? It works currently even when the values are larger then 100,000.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int ReadInputRecord(int&, int&, int&, int&, int&, int&, string&, ifstream&);
int Math(int&, int&, bool&);
void WriteOutputRecord(int, int, int, string team, ofstream &output);

int main()
{
	int num1, num2, num3, num4, num5, num6, score_total, success_amount;
	bool passcount = true;
	int column = 0;
	ifstream rawdata;
	string team;
	rawdata.open("RawScore.dat");
	if (rawdata)
	{
		ofstream output("output.dat");
		while (ReadInputRecord(num1, num2, num3, num4, num5, num6, team, rawdata) == 1)
		{
			//successful = Success(num1, num2, num3, num4, num5, num6);
			num1 = Math(num1, num4, passcount);
			num4 = Math(num1, num4, passcount);
			num2 = Math(num2, num5, passcount);
			num5 = Math(num2, num5, passcount);
			num3 = Math(num3, num6, passcount);
			num6 = Math(num3, num6, passcount);
			score_total = num1 + num2 + num3;
			success_amount = num4 + num5 + num6;
			column++;
			WriteOutputRecord(column, score_total, success_amount, team, output);
		}
		rawdata.close();
		output.close();
	}
	else
	{
		cout << "Can't open the input file.\n";
	}
}

int ReadInputRecord(int &num1, int &num2, int &num3, int &num4, int &num5, int &num6, string &team, ifstream &rawdata)
{
	if (rawdata >> num1)
	{
		rawdata >> num2;
		rawdata >> num3;
		rawdata >> num4;
		rawdata >> num5;
		rawdata >> num6;
		rawdata >> team;
		return(1);
	}
	return(0);
}


int Math(int &seconds, int &attempts, bool &firstpass)
{
	static int TIME_PENALTY = 1200;

	if (seconds > 0 && firstpass == true)
	{
		seconds = seconds + attempts * TIME_PENALTY - TIME_PENALTY;
		firstpass = false;
		return(seconds);
	}
	if (seconds > 0 && firstpass == false)
	{
		firstpass = true;
		return(1);
	}
	return(seconds);
}

void WriteOutputRecord(int column, int score_total, int success_amount, string team, ofstream &output)

{
	output << setw(3) << column << ": " << left << setw(40) << team << right << setw(2) << success_amount << setw(7) << score_total << endl;
}
Ah, I was remembering short int as int, sorry.
Last edited on
Does that mean I have to store them in long ints?
int is required to be at least 16 bytes long, so you will be able to store numbers at least as large as 32767. Common implementation have int with size at least 32 bits.

Do not receive your variables as mutable references unless you want to modify them. In case of primitive types take them by value:
int Math(int seconds, int attempts, bool firstpass)

static const int TIME_PENALTY = 1200;
Make constant const. Do not make variables static unless you need it. For primitive constants there is no need to make them static.

1
2
3
4
5
6
7
8
istream& ReadInputRecord(int &num1, int &num2, int &num3, int &num4, int &num5, int &num6, string &team, istream& rawdata)
{
    if (/*...*/) {
        //
        return(1);
    }
	return rawdata;
}
Make it consistent with standard library input functions. Do not specify that you need ifstream instead of istream unleyy you really need it. Same for of/ostream and WriteOutputRecord

Store data in single struct and then operate on it instead of manually doing all that.

Ah, I was remembering short int as int, sorry.
Even short int should be large enouh to suit you
Last edited on
Okay, thank you very much I made some changes. I had a follow up question. I was asked to do error checking for opening the input and output, but do you need to error check the output file? Doesn't it just create a new one if one does not exist?
but do you need to error check the output file?
NO
Haha, thank you very much shadow.
Topic archived. No new replies allowed.