grading a quiz program

Apr 15, 2014 at 8:33pm
I have a homework assignment that I need to get done and here is the problem
This exercise is designed to introduce you to arrays and strings (i.e., arrays of characters) in c++.
suppose a class of tudents takes a 20-questions multiple choice exam; each question has five coices a-e onle one of which is correct and there are no blank responses. In the answers file there are a list of kids with their id numbers and 20 answers with no space between them. the program is to read through the file and produce the following

student id    num correct
430-52-6192            13
112-81-5225            19
etc
etc

number of students taking exam = 23

and should also output


question  a   b   c   d   e
1         5   1   13* 3   1
2         4   7*  1   7   4
3         etc etc etc etc etc

and so on where the correct answer has a * by it and there are twenty questions. This is what I have so far.

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
  #include <iostream>
#include <fstream>
using namespace std;
void main ()
{
	char key[21], id[12], responses[21];
int i
	ifstream testin, ansin;
	testin.open(“test.dat”)
ansin.open(“answers.dat”);
ansin>>key;
testin>>id>>responses[21];
	
	while(!fin.eof())
	{
		numstudents++;
		numcorrect=0;
		for(1=0; i<20; i++)
			if(responses[i]==key[i])
				numcorrect++;
			cout<<id<<numcorrect<<endl;
		
testin>>id>>responses;
	}
	
cout<<numstudents<<endl;
	
Apr 15, 2014 at 8:35pm
My bad this is the updated
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
  #include <iostream>
#include <fstream>
using namespace std;
void main ()
{
	char key[21], id[12], responses[21];
int i numstudents=0, numcorrect=0;
	ifstream testin, ansin;
	testin.open(“test.dat”)
ansin.open(“answers.dat”);
ansin>>key;
testin>>id>>responses[21];
	
	while(!fin.eof())
	{
		numstudents++;
		numcorrect=0;
		for(1=0; i<20; i++)
			if(responses[i]==key[i])
				numcorrect++;
			cout<<id<<numcorrect<<endl;
		
testin>>id>>responses;
	}
	
cout<<numstudents<<endl;
	
Apr 15, 2014 at 8:38pm
Line 4: main must return an int
Line 7: Why are you declaring i here without a comma instead of declaring i on line 18 where you wrote 1 instead of i?

You're missing some closing braces. You should really fix your indentation.
Apr 15, 2014 at 8:40pm
sorry i missed that 1=0 that should be an i.

[code]
#include <iostream>
#include <fstream>
using namespace std;
void main ()
{
char key[21], id[12], responses[21];
int numstudents=0, numcorrect=0;
ifstream testin, ansin;
testin.open(“test.dat”)
ansin.open(“answers.dat”);
ansin>>key;
testin>>id>>responses[21];

while(!fin.eof())
{
numstudents++;
numcorrect=0;
for(int i=0; i<20; i++)
if(responses[i]==key[i])
numcorrect++;
cout<<id<<numcorrect<<endl;

testin>>id>>responses;
}

cout<<numstudents<<endl;
}
[\code]

why do I need to return an int on line 4 I am using a void function
Apr 15, 2014 at 8:41pm
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
#include <iostream>
#include <fstream>
using namespace std;
void main ()
{
char key[21], id[12], responses[21];
int numstudents=0, numcorrect=0;
ifstream testin, ansin;
testin.open(“test.dat”)
ansin.open(“answers.dat”);
ansin>>key;
testin>>id>>responses[21];

while(!fin.eof())
{
numstudents++;
numcorrect=0;
for(int i=0; i<20; i++)
if(responses[i]==key[i])
numcorrect++;
cout<<id<<numcorrect<<endl;

testin>>id>>responses;
}

cout<<numstudents<<endl;
}
Apr 15, 2014 at 8:42pm
The main function is a special function, and one of its special characteristics is that it must have a return type of int. For the main function only, the return statement is optional.
Apr 15, 2014 at 8:44pm
so you are saying something like 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
#include <iostream>
#include <fstream>
using namespace std;
void main ()
{
	char key[21], id[12], responses[21];
int numstudents=0, numcorrect=0;
	ifstream testin, ansin;
	testin.open(“test.dat”)
ansin.open(“answers.dat”);
ansin>>key;
testin>>id>>responses[21];
	
	while(!fin.eof())
	{
		numstudents++;
		numcorrect=0;
		for(int i=0; i<20; i++)
			if(responses[i]==key[i])
				numcorrect++;
			cout<<id<<numcorrect<<endl;
		
testin>>id>>responses;
	}
	
cout<<numstudents<<endl;
int;
}
Apr 15, 2014 at 8:47pm
I don't know what line 27 is for, we're talking about line 4 here.

https://en.wikipedia.org/w/index.php?title=Main_function&oldid=579287652#C_and_C.2B.2B
Apr 15, 2014 at 8:52pm
so
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
#include <iostream>
#include <fstream>
using namespace std;
int main (void);
{
	char key[21], id[12], responses[21];
int numstudents=0, numcorrect=0;
	ifstream testin, ansin;
	testin.open(“test.dat”)
ansin.open(“answers.dat”);
ansin>>key;
testin>>id>>responses[21];
	
	while(!fin.eof())
	{
		numstudents++;
		numcorrect=0;
		for(int i=0; i<20; i++)
			if(responses[i]==key[i])
				numcorrect++;
			cout<<id<<numcorrect<<endl;
		
testin>>id>>responses;
	}
	
cout<<numstudents<<endl;
}
Apr 15, 2014 at 8:54pm
It says expects a declaration on the int main (void);
{
the bracket says it expects a declaration but thats the only red line error that comes up
Apr 15, 2014 at 10:53pm
You added a semicolon for some reason by mistake.
Apr 15, 2014 at 11:06pm
I'm now getting test.dat to have an error it says test is undifined and ansin.open says expected a ; lastly the !fin.eof says fin is undifined. Any ideas?
Apr 15, 2014 at 11:08pm
ok i put the ; on the test.dat") and now the ansin.open isnt errored but answers.dat") says answers is undifined any ideas?
Apr 15, 2014 at 11:10pm
ok now it just says the fin is undefined
here is what i have
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
#include <iostream>
#include <fstream>
using namespace std;
int main (void)
{
	char key[21], id[12], responses[21];
int numstudents=0, numcorrect=0;
	ifstream testin, ansin;
	testin.open("test.dat");
ansin.open("answers.dat");
ansin>>key;
testin>>id>>responses[21];
	
	while(!fin.eof())
	{
		numstudents++;
		numcorrect=0;
		for(int i=0; i<20; i++)
			if(responses[i]==key[i])
				numcorrect++;
			cout<<id<<numcorrect<<endl;
		
testin>>id>>responses;
	}
	
cout<<numstudents<<endl;
}
Apr 15, 2014 at 11:39pm
The only input file streams you have are testin and ansin, so you need to use one of them instead of fin.
Apr 15, 2014 at 11:42pm
ok i got that fixed and now i have
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
#include <iostream>
#include <fstream>
using namespace std;
int main (void)
{
	char key[21], id[12], responses[21];
int numstudents=0, numcorrect=0;
	ifstream testin, ansin;
	testin.open("test.dat");
ansin.open("answers.dat");
ansin>>key[21];
testin>>id>>responses[21];
	
	while(!testin.eof())
	{
		numstudents++;
		numcorrect=0;
		for(int i=0; i<20; i++)
		
			if(responses[i]==key[i])
				numcorrect++;
			cout<<id<<numcorrect<<endl;
		
testin>>id>>responses;
	}
cout<<numstudents<<endl;
}


and its printing out

19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19

in and infinite loop... any suggestions on whats wrong
Topic archived. No new replies allowed.