Could you help me fix my code (student scores, files,structure)

Pages: 12
Write your question here.


I have to make a program in which a teacher is inputing the scores of each student in a file.There are several marks:
A 90-100
B 80-89
C 70-79
D 60-69
F 0-59
The teacher should be able to see the number of students who have passed, the avarege score of the whole class and the number of students in each "mark" (the number with A,B,C,D,F)

here is my code, i hope that you will find a way to help me
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
 

#include <iostream>
#include <fstream>
using namespace std;

struct Student
{
	char first_name[20];
	char secound_name[20];
	char last_name[20];
	double points;
};

int main()

{
	int A;
	int B;
	int C;
	int D;
	int F;

	int p;

	ofstream file;
	file.open("file.txt", ios::out);
	Student students[35];
	 int n, i;
	cout << "Number students: ";
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cout << "Name: ";
		cin >> students[i].first_name;
		cout << "Secound: ";
		cin >> students[i].secound_name;
		cout << "Last: ";
		cin >> students[i].last_name;
		cout << "points: ";
		cin >> students[i].points;

		file << struct.students.first_name;
	}
	if (p < 60)

				F++;

			else

			{

				if (60 <= p && 70 > p)

					D++;

				else if (70 <= p && 80 > p)

					C++;

				else if (80 <= p && 90 > p)

					B++;

				else if (90 <= p && 100 >= p)

					A++;
           } // end else

		} // end if

    cout << "# of students in group is " << n << ;

	cout << "# of students with A degree is " << A << ;

	cout << "# of students with B degree is " << B <<;

	cout << "# of students with C degree is " << C << ;

	cout << "# of students with D degree is " << D <<;

	cout << "# of students with F degree is " << F << ;



	int passed;
		passed = (A + B + C + D);
		int avg;
	avg = (p / n);

	file.close();
	return 0;
	system("pause");
}

What is the problem ?

line 43: file << struct.students.first_name;

Why do yuo save only the first_name ?
I was just trying to get something going, if i manage to save the first name in the file i should be able to save everything from the structure. I am trying to understand the basis of the way i should create the program.

The main problem is that my code is not working. (unfunny joke)
The problem is that the code is having a lot of errors I don't know how to deal with, some syntaxx erros that I think that should not be there but maybe i am not doing something right.

Also there isn't any data saved in the text file, I have opening and closing files for saving information in other programs, but it;s not working. :/
Last edited on
The problem is that the code is having a lot of errors I don't know how to deal with, some syntaxx erros that I think that should not be there but maybe i am not doing something right.

Yes your program has syntax errors that your compiler should be telling you about. Perhaps if you posted your compiler error messages someone can help you understand the messages.

Looking at the compiler output from the embedded compiler (press the gear button) the first thing you need to look into is mismatched or misplaced braces.

At global scope: ///////// Big big clue here.
72:5: error: 'cout' does not name a type
as well as jlb's advice above, your compiler should be telling you that line 43 is a bit wrong as well.
http://prikachi.com/images.php?images/868/8514868L.png from 1-26

http://prikachi.com/images.php?images/869/8514869L.png from 26-42

yes, mutexe it is, i was trying to save the information i have inputed from first_name into the file
Please cut and past the errors into a post, trying to read those small pictures is difficult and error prone.

And don't forget to post the actual code that caused those errors.
jib, that it is the actual code (the one I had put in the first post). I cannot understand what you are wanting from me to post :/ aren't the screenshots visable enough? i am able to see everything clearly when i click on the image after I open the link
Look very closely around line 45, and the few after it.
aren't the screenshots visable enough? i

No, not everyone has a giant monitor you know.

Please post the error messages as text and place them in your post.

mutexe, now i understood, they aren't readable because that they aren't one under another :D :D thanks :)

Guys i made a few changes and got rid of few of the errors but i still have some :/

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

struct Student
{
	char first_name[20];
	char secound_name[20];
	char last_name[20];
	double points;
};

int main()

{
	int A;
	int B;
	int C;
	int D;
	int F;

	int p;

	ofstream file;
	file.open("file.txt", ios::out);
	Student students[35];
	int n, i;
	cout << "Number students: ";
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cout << "Name: ";
		cin >> students[i].first_name;
		cout << "Secound: ";
		cin >> students[i].secound_name;
		cout << "Last: ";
		cin >> students[i].last_name;
		cout << "points: ";
		cin >> students[i].points;

		file << struct.students.first_name;
	}
	if (p < 60)

		F++;

	else

	{

		if (60 <= p && 70 > p)

			D++;

		else if (70 <= p && 80 > p)

			C++;

		else if (80 <= p && 90 > p)

			B++;

		else if (90 <= p && 100 >= p)

			A++;
	} // end else
	
	cout << "# of students in group is " << n <<endl;

	cout << "# of students with A degree is " << A <<endl;

	cout << "# of students with B degree is " << B <<endl;

	cout << "# of students with C degree is " << C <<endl;

	cout << "# of students with D degree is " << D <<endl;

	cout << "# of students with F degree is " << F <<endl;
} // end if


int passed(0);
passed = (A + B + C + D);
int avg(0);
avg = (p / n);

file.close();
return 0;
system("pause");
}


errors :http://prikachi.com/images.php?images/2/8515002S.png

I see that now most of them are in the last part of the code, but i still have no idea how to sort that thing in line 42. Now i will try to repair the bottom part.
I suppose that this is what you want, here are the errors from the last screenshot

1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(42): error C2332: 'struct' : missing tag name
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(42): error C2226: syntax error : unexpected type 'main::<unnamed-tag>'
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(87): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(87): error C2374: 'passed' : redefinition; multiple initialization
1> c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(86) : see declaration of 'passed'
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(87): error C2065: 'A' : undeclared identifier
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(87): error C2065: 'B' : undeclared identifier
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(87): error C2065: 'C' : undeclared identifier
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(87): error C2065: 'D' : undeclared identifier
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(89): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(89): error C2374: 'avg' : redefinition; multiple initialization
1> c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(88) : see declaration of 'avg'
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(89): error C2065: 'p' : undeclared identifier
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(89): error C2065: 'n' : undeclared identifier
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(91): error C2143: syntax error : missing ';' before '.'
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(91): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(92): error C2059: syntax error : 'return'
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(93): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(93): error C2365: 'system' : redefinition; previous definition was 'function'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\stdlib.h(560) : see declaration of 'system'
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(93): error C2440: 'initializing' : cannot convert from 'const char [6]' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(94): error C2059: syntax error : '}'
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(94): error C2143: syntax error : missing ';' before '}'

Look at line 79. Is your comment correct? If it is then where is it's matching brace located?
jlb, i thought that it was ok, thanks for pointing it out the first if has its own matching braces, else has it's braces

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

struct Student
{
	char first_name[20];
	char secound_name[20];
	char last_name[20];
	double points;
};

int main()

{
	int A;
	int B;
	int C;
	int D;
	int F;

	int p;

	ofstream file;
	file.open("file.txt", ios::out);
	file.close();
	Student students[35];
	int n, i;
	cout << "Number students: ";
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cout << "Name: ";
		cin >> students[i].first_name;
		cout << "Secound: ";
		cin >> students[i].secound_name;
		cout << "Last: ";
		cin >> students[i].last_name;
		cout << "points: ";
		cin >> students[i].points;

		file << struct.students.first_name;
	}
	if (p < 60)
	
	{

		F++;
	}

	else

	{

		if (60 <= p && 70 > p)

			D++;

		else if (70 <= p && 80 > p)

			C++;

		else if (80 <= p && 90 > p)

			B++;

		else if (90 <= p && 100 >= p)

			A++;
	} // end else
	
	cout << "# of students in group is " << n <<endl;

	cout << "# of students with A degree is " << A <<endl;

	cout << "# of students with B degree is " << B <<endl;

	cout << "# of students with C degree is " << C <<endl;

	cout << "# of students with D degree is " << D <<endl;

	cout << "# of students with F degree is " << F <<endl;
	int passed(0);
	passed = (A + B + C + D);
	int avg(0);
	avg = (p / n);

system("pause");
return 0;
}


the compiler says that I am only having 2 errors both in line 43 :) how should i get rid of them and finaly save the information from the struture in the file?
Please post the exact error messages, as text. Then possibly I or someone else can help explain what your messages are telling you.

And realize that usually an error message tells you where it detects the problem, but the problem can, and often is, on some earlier line.
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(43): error C2332: 'struct' : missing tag name
1>c:\users\todor\documents\visual studio 2013\projects\consoleapplication31\consoleapplication31\source.cpp(43): error C2226: syntax error : unexpected type 'main::<unnamed-tag>'
here are the messages
i don't think my cycles are correct, i managed to get the programm going but it doesn't count the number of students with A,B,C,D,F, no matter how many students i input, the program always outputs that the number of students with F is 1, and that there are no students with A,B,C,D
So this appears to be the problem line:
file << struct.students.first_name;

Look at what Microsoft says about that first error message:
https://msdn.microsoft.com/en-us/library/3ys51zwc.aspx
'typedef' : missing tag name
The compiler found an incomplete type definition.
The following sample generates C2332:


Do you realize that struct is a keyword. This keyword is used when creating a user defined type (like you did starting on line 5), or creating an instance of that type. Are you trying to create a new type, or an instance of some type on the line in question?

Perhaps you should review how to access class/structure member variables.
http://www.cplusplus.com/doc/tutorial/structures/
Or perhaps just look at how you accessed the member variable on line 34.

yes, i think i understood

i was trying a few ways to get the ifs and elses working but so far without any luck


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

struct Student
{
	char first_name[20];
	char secound_name[20];
	char last_name[20];
	double points;
};

int main()

{
	int A(0);
	int B(0);
	int C(0);
	int D(0);
	int F(0);

	int p(0);

	ofstream file;
	file.open("file.txt", ios::out);
	file.close();
	Student students[35];
	int n, i;
	cout << "Number students: ";
	cin >> n;
	for (i = 0; i < n; i++)
	{
		
		cout << "Name: ";
		cin >> students[i].first_name;
		cout << "Secound: ";
		cin >> students[i].secound_name;
		cout << "Last: ";
		cin >> students[i].last_name;
		cout << "points: ";
		cin >> students[i].points;

		file << students[i].first_name;
		file << students[i].secound_name;
		file << students[i].last_name;
		file << students[i].points;
	}
	if (students[i].points < 60)

	{

		F++;
	}

	else

	{

		if (60 <= students[i].points && 70 > students[i].points)

			D++;

		else if (70 <= students[i].points && 80 > students[i].points)

			C++;

		else if (80 <= students[i].points && 90 > students[i].points)

			B++;

		else if (90 <= students[i].points && 100 >= students[i].points)

			A++;
	} // end else

	cout << "# of students in group is " << n << endl;

	cout << "# of students with A degree is " << A << endl;

	cout << "# of students with B degree is " << B << endl;

	cout << "# of students with C degree is " << C << endl;

	cout << "# of students with D degree is " << D << endl;

	cout << "# of students with F degree is " << F << endl;
	
        int passed(0);
	passed = (A + B + C + D);
	cout << "the number of passed students is: " <<passed<<endl;
	
        double avg(0);
	avg += students[i].points;
	avg /= n;
	cout << "the avarage points of the students are: " << avg << endl;
	system("pause");
	return 0;
}
Wouldn't it make more sense to check each score, instead of just one score?
Pages: 12