Can i improve

Hey guys.Have written some code but wanted to make sure im on the right lines. Is there anyway i can make this more efficient? Although the program works is it generally preferred to try and minimise the if lines and put it intp less lines with more OR's and ANDS? Also is the way i have used variables for validation considered good conduct?

The program reads pre written scores (a, b, c) in a set format (eg. 50 60 80) and returns a pass distinction or fail based on various conditions.


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

int main()
{
int a;
int b;
int c;
int val = 0;
int pass = 0;
int distinction = 0;
string line;

ofstream outfile("output.txt");

ifstream infile ("a1f.txt");





while (getline (infile, line))
	{istringstream iss (line, istringstream::in);
		iss >> a;
		if (iss.fail())
			val++;
		iss >> b;
		if (iss.fail())
			val++;
		iss >> c;
		if (iss.fail())
			val++;
		if (((a < 0) || (a > 95)) || ((b < 0) || (b > 95)) || ((c < 0) || (c > 95)))
			val++;
		if (val > 0)
			outfile << "Bad input line:" << " " << line << endl;
		else
		{
		if ((a >= 50) && (b >= 50) && (c >= 50))
				pass++;
		if ((a >= 60) && (b >= 60) && (c >= 40))
				pass++;
		if ((a >= 60) && (c >= 60) && (b >= 40))
				pass++;
		if ((pass > 0) && (a >= 75) && (b >= 75))
				distinction++;
		if ((pass > 0) && (a >= 75) && (c >= 75))
				distinction++;
		if ((pass > 0) && (b >= 75) && (c >= 75))
				distinction++;
		}
		if ((pass > 0) && (distinction == 0))
			outfile << setw(2) << a << " "  << setw(2) << b << " " << setw(2) << c << " " << "Pass" << endl;
		if ((pass > 0) && (distinction > 0))
			outfile << setw(2) << a << " "  << setw(2) << b << " " << setw(2) << c << " " << "Distinction" << endl;
		if ((pass == 0) && (distinction == 0) && (val == 0))
			outfile << setw(2) << a << " "  << setw(2) << b << " " << setw(2) << c << " " << "Fail" << endl;
		val = 0;
		pass = 0;
		distinction = 0;
	}

}
Was just wanting to ask you about this section:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
else
{
		if ((a >= 50) && (b >= 50) && (c >= 50))
				pass++;
		if ((a >= 60) && (b >= 60) && (c >= 40))
				pass++;
		if ((a >= 60) && (c >= 60) && (b >= 40))
				pass++;
		if ((pass > 0) && (a >= 75) && (b >= 75))
				distinction++;
		if ((pass > 0) && (a >= 75) && (c >= 75))
				distinction++;
		if ((pass > 0) && (b >= 75) && (c >= 75))
				distinction++;
}


You do realise that if a b c is 80 80 80 for example. Pass will equal 3 and destinction will equal 3. Is this what you wanted to happen? Sorry if I'm just misunderstanding, but I don't really get what the purpose of it doing this is.
Topic archived. No new replies allowed.