Scoring Students

closed account (ShUk4iN6)
I need to write a program using arrays and strings.Here is the question.
A class of students takes a 20 question multiple-choice exam;each question has 5 choices(a,b,c,d,e)only one of them are correct.In "tests.dat"Each record of which consists of a student id, followed by a blank, followed by students 20 responses. In "answers.dat" which consists of a single record, namely the correct answers to the exam.Then It needs to produce a grade summary report like this.If the answer is correct it should put a "*" by the correct answer


student-id number correct
1231231212312 12
1233424325435 25
.... ...






question A B C D E
1 5 1 13* 3 1
2 4 7* 5 12 7
.
.
...



this is what i got so far.Thank you

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

using namespace std;
int main()
{
ifstream fin ans,fin tests;
char key[21],id[21],response[21];
int count[20][5];

fin ans>>key;
fin tests>>>id>>response;

for(i=0,i<20,i++)
{
if(key[i] ==responses[i])
{
	num correct ++;
count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A'] &&"*"<<endl;
else(key[i] !=responses[i]){
	count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A']<<endl;
}
}
}




fin ans.open("answers.dat");
if(fin.fail("answers.dat"))
{
	cout<<"Error opening stream"<<endl;
	abort();
}
fin tests.open("tests.dat");
if(fin.fail("tests.dat"))
{
	cout<<"Error opening stream"<<endl;
	abort();
}

cout<<id<<correct<<<endl<<endl;
cout<<count<<"A		B		C		D		E"<<
	<<key<<count[i][responses[i]-'A']

return 0;

}
are you using spaces in variable names? (object name for input stream)

don't think ifstream fin ans,fin tests; will work, but I could be wrong. how about an underscore...

fin_ans fin_tests or camel case... finAns, & finTests

 
cout<<id<<correct<<<endl<<endl;


there's one too many "<" in there.


and that block of code is virtually unreadable, if you have problems using copy paste, simple do a quick edit on code please.

Personally I wouldn't be using that many arrays, possibly two, 1 to store answers, and 1 to read in correct results.

also I wouldn't be using abort() without the proper namespace. To add on that, abort is equal to exit(1); so you should use exit as it is more generic and useful.

cout<<count<<"A B C D E"<<

instead of adding so many spaces to output statements you can use escape character '\t'
which will put a tab between each letter, by default 8 spaces.


cout<<count<<"A\tB\tC\tD\tE"<<
Last edited on
closed account (ShUk4iN6)
Thanks for the help. yes i know it is unreadable. I dont know what i am suppose to do with this part
i know it is confusing

1
2
3
4
5
6
7
8
9
10
{
	num correct ++;
count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A'] &&"*"<<endl;
else(key[i] !=responses[i]){
	count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A']<<endl;
}
}
}
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
#include <iostream>
#include <cstdlib>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::exit;

int main()
{
    ifstream finAns;     // need to have correct variables names
    ifstream finTests;
    char key[21];
    char id[21];
    char response[21];          // indented properly
    int count[20][5];

    finAns  >>  key;     // this isn't going to work because you have not declared the file
    finTests  >>  id  >>  response;     //  same deal.

    for(int i=0 ; i < 20 ; i++)   // i was not initialized so you need to declare it
         // also you were using commas instead of semi-colon
    {
        if(key[i] ==responses[i])     
        {
	    num correct++;    // what is num correct ?  you can't do this, variable names can't contain spaces
            // and they need to be initialized eg:   int numCorrect=0;

            count[i][responses[i]-'A']++;    // what is this statement trying to do ?

           // cout  <<  count[i][responses[i]-'A'] &&"*"  <<  endl;   dont use && operator to append stuff use <<.
           cout << count[i][responses[i]-'A'] << "*" << endl;
        }   // <-- added close of 'if statement'
        else ( key[i] != responses[i] )
        {
	    count[i][responses[i]-'A']++;
            cout  <<  count[i][responses[i]-'A']  <<  endl;
        }
    }

    // return 0;   //   <--- returned an integer for return type "int" main() 
//}   i'm guessing this is not the end of main, but because you added extra '}' it is

    finAns.open("answers.dat");

   // if(fin.fail("answers.dat"))     // <---- this makes no sense  what is fin.fail? needs to be the variabels name

    if (! finAns.is_open())
    {
	cout  <<  "Error opening stream"  <<  endl;
	// abort();
        exit (1);    // exit is sexier because it's more useful and generic.
    }

    finTests.open("tests.dat");

    //  if(fin.fail("tests.dat"))   // same deal... as before

    if (! finTest.is_open())
    {
	cout  <<  "Error opening stream"  <<  endl;
	exit (1);
    }

    cout  <<  id  <<   correct  <<  endl  <<  endl; 
    // cout<<count<<"A		B		C		D		E"<<
    //	<<key<<count[i][responses[i]-'A']
    // you are using two sets of '<<' here and the begining of next line

    cout << count << "A\tB\tC\tD\tE" <<  key 
                << count[i][responses[i]-'A'] << endl;
 //  not quite sure what this last output is supposed to be,
// there is a missing '[' which makes no sense, and also 2 array elements, however you not 
// traversing an array anywhere so this will only ever be one value which the compiler may even
// complain that there variable is not initialized. no << endl; at end, and/or no flush()

return 0;

}
Last edited on
in future, simply start with:

1
2
3
4
5
6
7
// #includes

int main()
{

    return 0;
}


compile this, then write a bit more code and compile, write a bit more and compile. so on and so forth until you are done and the program is completely written you will find that your code works.
The easiest way to program even from early on is using pseudo-code. write down in bullet format what the program needs to do. Then you find the code can simply mimic english.
eg:

1. declare variables
2. open input files
3. check if file opening succeeded.
4. store input files into arrays.
5. compare results.
6 ... etc.

I dont know what i am suppose to do with this part
i know it is confusing

1
2
3
4
5
6
	num correct ++;
count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A'] &&"*"<<endl;
else(key[i] !=responses[i]){
	count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A']<<endl;


Explain what you are trying to do in english, then you might find you will be able to write the code, or if you are unsure of syntax then I can, or someone else can help you with it.
Last edited on
closed account (ShUk4iN6)
do i need to add this

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
for(i=0;i<20;i++)
    {
        if(key[i] ==responses[i])
        {
	    num correct ++;
            count[i][responses[i]-'A']++;
            cout<<count[i][responses[i]-'A'] &&"*"<<endl;
        } 
        else(key[i] !=responses[i])
        {
	    count[i][responses[i]-'A']++;
            cout<<count[i][responses[i]-'A']<<endl;
        }
		if(key[i] ==responses[i])
        {
	    num correct ++;
            count[i][responses[i]-'B']++;
            cout<<count[i][responses[i]-'B'] &&"*"<<endl;
        } 
        else(key[i] !=responses[i])
        {
	    count[i][responses[i]-'B']++;
            cout<<count[i][responses[i]-'B']<<endl;
        }
		if(key[i] ==responses[i])
        {
	    num correct ++;
            count[i][responses[i]-'C']++;
            cout<<count[i][responses[i]-'C'] &&"*"<<endl;
        } 
        else(key[i] !=responses[i])
        {
	    count[i][responses[i]-'C']++;
            cout<<count[i][responses[i]-'C']<<endl;
        }
		if(key[i] ==responses[i])
        {
	    num correct ++;
            count[i][responses[i]-'D']++;
            cout<<count[i][responses[i]-'D'] &&"*"<<endl;
        } 
        else(key[i] !=responses[i])
        {
	    count[i][responses[i]-'D']++;
            cout<<count[i][responses[i]-'D']<<endl;
        }
		if(key[i] ==responses[i])
        {
	    num correct ++;
            count[i][responses[i]-'E']++;
            cout<<count[i][responses[i]-'E'] &&"*"<<endl;
        } 
        else(key[i] !=responses[i])
        {
	    count[i][responses[i]-'E']++;
            cout<<count[i][responses[i]-'E']<<endl;
        }
    }
closed account (ShUk4iN6)
this wat i got
and its still wrong can you help me pls?

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

using namespace std;

int main()
{
	ifstream finans;
	ifstream fintests;
	char key[21];
    char id[12];
    char responses[21];
    int count[20][5];
	int i;
	int countA = 0;
	int countB = 0;
	int countC = 0;
	int countD = 0;
	int countE = 0;
	int numcorrect =0;
	int bob[2]={Student-id, Number Correct};

	finans.open("Answers.dat");
	if(fin.fail())
	{
		cout<<"Error opening stream"<<endl;
		exit(1);
	}
	fintests.open("Tests.dat");
	if(fin.fail())
	{
		cout<<"Error opening stream"<<endl;
		exit(1);
	}
	while(fintests.open("Tests.dat") && finans.open("Answers.dat")
	{
	fintests>>id>>responses
	for(int i=0 ; i < 20 ; i++)
	{
		fintests>>id[i]>>responses[i];
	}
	for(int i=0; i<20; i++)
	{
		finans>>key[i]
		if(key[i] ==responses[i])
		{
			numcorrect ++;
			if(responses=='A')
			{
			count[i][responses[i]-'A']++;
			}
			cout<<countA"*"
			else
			{
				cout<<countA" "
			}
			else
			{
				if(responses=='B')
				{
				count[i][responses[i]-'B']++;
				}
				cout<<countB"*"
				else
				{
					cout<<countB" "
				}
				else
				{
					if(responses=='C')
					{
						count[i][responses[i]-'C']++;
					}
					cout<<countC"*"
					else
					{
						cout<<countC" "
					}
					else
					{
						if(responses=='D')
						{
							count[i][responses[i]-'D']++;
						}
						cout<<countD"*"
						else
						{
							cout<<countD" "
						}
						else
						{
							if(responses=='E')
							{
								count[i][responses[i]-'E']++;
							}
							cout<<countE"*"
							else
							{
								cout<<countE" "
							}

						}

					}
				}
			}
		else(key[i] !=responses[i])
		{

		}
		
		cout<<bob[2]<<endl;
		cout<<id[12]<<"\t\t"<<numcorrect<<endl;


		cout<<count[20][5]<<endl;
		}
	}
return 0;
	
	}	
I wrote this code a long time ago. It's similar to what you want done, so I figured you can use it as a foundation for what you want done.

This isn't what you want. But using a few minor adjustments, you can accomplish what you need to do.

Not to mention it's cleaner.

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
176
177
178
// Student Grading system

//int i is used as a local variable alot to keep track of stuff

#include <iostream> // Mandatory
#include <fstream> // File I/O
#include <string> // Mandatory for strings

void getInfo(); // Function getInfo
void findAvg(); // Function findAvg
void giveGrade(); // Function giveGrade
void output(); // Function output

using namespace std; //Standard Namespace

//Declarations
float avg = 0;
int ID[200];
string name[200];
float score[200];
string grade[200];
signed int pk=-1; //PK is the incrementer
ofstream file;
string filename;

//Main
int main(void)
{
    //START OF PROGRAM
    system("color 0a"); // Changes system text to green/0a
    system("cls"); // Clears the screen
    // Introduction
    //Make it look good
    cout << "-------------------------------------------" << endl;
    cout << "|Welcome to my Student grading organizer!!|" << endl;
    cout << "-------------------------------------------" << endl << endl;
    // Notifies where file will be saved
    cout << "This program will create a file called 'Grades.txt'" << endl; 
    cout << "in the ORIGINAL folder it was placed in." << endl;
    system("pause"); // Give time to read
    // Calls getInfo
 getInfo ();
    // Calls findAvg 
 findAvg ();
    // Calls giveGrades
 giveGrade();
    // Calls output
 output();
 
 // This is for after output.. Notification of saving
 cout << "Saving file 'Grades.Txt'" << endl;
 // Give Time to Read
 system("Pause"); // I know system calls are bad, but.. what the heck
 //END OF PROGRAM
 return 0;
}

void getInfo() //Used to Gather information
{
     string more = "y"; //While More = True.... Loop
     int i = 0; //To ensure Score is actually integer
     
     while (more=="y")
     
     {     
           pk++; //Incremente
           ID[pk] = pk + 1; //ID Count = PK..
           system("cls");
           cin.sync(); // Clear buffer for each loop
           
           cout << "What is the name of the student? "; //Get name
           getline( cin, name[pk] ); //Put into name[pk]
           
           
           cout << "What is the score of the student? "; //Get score
           
           //Ensure that score is actually an integer
           while (!(cin >> i))
           {
		   cin.clear();
		   cin.ignore(1000,'\n');
		   //If not an integer, repeat until so
		   cout << "What is the score of the student (integer value's only): ";
           }
           score[pk] = i; //If so.. Put score in here
           

           cout << "Would you like to add another student? ";
           cin >> more; //Ask if they'd like to add another student
           
           while (more != "y" && more != "n")
           //While more is not y & not n
           {
                 cout << "(y or n) only please: ";
                 cin >> more;
           }
           /*If it's n... it goes back to main.. to continue next function
             Otherwise it will go back to the beginning of this function*/
           }
}

void findAvg() //Used to calculate the average
{
for (int i=0; i<pk + 1; i++) /* As long as i is less than pk
                                score[1], score[2], and so on
                                will be added to avg*/
{
avg = avg + score[i];
}
avg = avg / (pk + 1); /*To find the average.. 
                      Take the total number of scores (currently in avg)
                      and divide it by pk + 1 because pk starts at -1*/

cout << "Average = " << avg << "%" << endl; /*Display avg and add %
                                              so it looks professional*/
system("pause");    //Give user time to see average
}

void giveGrade()
{ 
 
 /*Standards are as follows
  A student within 5 points of the average gets a B
  A student with more than 5 points above the average gets an A
  A student with more than 5 and within 15 points below the average gets a C
  A student with more than 15 points below the average gets a D 
 */
 
  for(int i = 0; i < pk + 1;i++) //While i is less than pk.. do this
  {
        if (score[i] >= avg + 5) //Standards for an A
        {            //Greater than or = to the average + 5
        grade[i] = "A";
        }
        else if  (score[i] <= avg + 4 && score[i] >= avg - 4)
        {                  //Less than or = to +4 & greater than or = to -4
             grade[i] = "B"; //Standards for a B
             }
             else if (score[i] <= avg -5 && score[i] >= avg - 14)
             {
                  grade[i] = "C"; //Standards for a C
                  }
                  else if (score[i] < avg - 15)
                  {
                      grade[i] = "D"; //If none of the above apply.. Give D
                      }
                                        
  }
}

void output()
{
     //ofstream file declared above
     //Open a file called Grades.txt
     //If non-existant it will be created
     file.open("Grades.txt");
     //Display average in text file
     file << "The Average is: " << avg << "%" << endl << endl;
     
     //Local Variable i is used again
     //while i is less than pk put the array into the textfile
     //For example
     /*
     ID[1] Produces 1
     name[1] Produces Name that was put first
     score[1] Produces score that was put in first
     grade[1] Produces first grade calculated
     << ends the line
     the "" are to make it look neater
     */
     for(int i = 0; i < pk + 1;i++)
     {
     file << ID[i] << ", " << name[i] << ", " << score[i] << "%, " << grade[i] << endl;
     cout << ID[i] << ", " << name[i] << ", " << score[i] << "%, " << grade[i] << endl;
     }
     //Save and close the file.. Go Back to main
     file.close();
}


The actual post of it is --> http://www.cplusplus.com/forum/beginner/6076/
closed account (ShUk4iN6)
legend do i need to use void??
I did something like this C and C++ mix.I get the first part of the question right but not the second part.Do you have any idea I should do?


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

using namespace std;
int question[20][5];
int main()
{
	ifstream fanswers, fcorrect_keys;
	
	char correct_keys[21];
	char student_id[100][12], answers[100][21];
	char id_buf[12], answers_buf[21];
	
	int student_count = 0;
	
	fcorrect_keys.open("Answers.dat");
	while(!fcorrect_keys.eof())  {		
		fcorrect_keys >> answers_buf;
		strcpy(correct_keys, answers_buf);
	}
//	cout << correct_keys << endl;
	fcorrect_keys.close();
	fanswers.open("Tests.dat");
	while(!fanswers.eof())  {		
		fanswers >> id_buf >> answers_buf;
		strcpy(student_id[student_count], id_buf);
		strcpy(answers[student_count],answers_buf);
		student_count++;  		
   	}
//	for(int i=0; i < student_count;i++)
//		cout << student_id[i] << "  " << answers[i] << endl;
 	 fanswers.close();// close the file  
	cout << " student_id      number_correct" << endl << endl;
	for(int i = 0; i < student_count; i++)  {
		int correct_count = 0;
		for(int j = 0; j < 20; j++)  { 
			if (answers [i][j] == correct_keys[j])
				correct_count++;
			question[j][answers[i][j] - 'A']++;
		}
		printf("%11.11s         %2d\n", student_id[i],correct_count);
	}	
	cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl; 
	
	printf("\nquestion     A    B    C    D    E\n");
	
	for(int k = 0; k < 20 ; k++)  {
		printf("   %2d      ",1+k);
		for(int l=0;l < 5; l++)
			printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
		printf("\n");
	}
	return 0;
}
Topic archived. No new replies allowed.