code into function and stopping a loop

Hi! I have exactly two problems with my program. I need it to stop at the end of file but have it go up to 15 if needed. Also I need to use a boolean function to check if the input is a id number or a grade.

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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int NO_STUDENTS = 15,
		  NO_TEST = 6;

int main ()
{
  double StudGrades[NO_STUDENTS][NO_TEST];
  double StudId[NO_STUDENTS];
  double test_sum,
	     stud_avg;

		ifstream fin;
	    fin.open("grades.txt");
	    if (!fin)
	    {
	          cout << "Failed to open fin file, program terminated.";
	          fin.close();
	          return(1);
	    }

	    ofstream fout;
	    fout.open("prog9_out_acb95.txt");
	    if (!fout)
	    {
	        cout << "Failed to open output file, program terminated.";
	        fin.close();
	        fout.close();
	        return(2);
	    }

        fout << setprecision(0)<< fixed;
	    for(int i= 0; i< NO_STUDENTS; i++)
	    {
	    fout << endl;
	    fin >> StudId[i];
	    fout << "Student ID: " << StudId[i] << endl;
	    test_sum= 0;
	    for (int i = 0; i< NO_TEST; i++)
	    {
	    fin >> StudGrades[NO_STUDENTS][NO_TEST];
	    fout << StudGrades[NO_STUDENTS][NO_TEST] << " ";
	    test_sum += StudGrades[NO_STUDENTS][NO_TEST];
	    }
        fout << endl;
        stud_avg =test_sum / 6;
        fout << "Test Average: " << stud_avg << endl;
	    }
fout.close();
fin.close ();
return 0;
}
- You can put the whole thing in a loop. If you need to go up, just start the loopagain with 'continue' else break the loop.

1
2
3
4
5
6
7
8
9
10
do
{
//do some work here.

if(need to go up)
continue;
else
break;

}while(1);


for number or grade.
when you read it from the file (I think strings are read), check if its a alphabet using isalpha() function; Otherwise, check each character if it lies between '0' and '9', if it does, convert it to a number using atol/atoi.
Topic archived. No new replies allowed.