2d array not outputting correctly

I'm trying to figure out why this 2d array is not outputting what I need it to. This program is inputting from two files. First problem is that the array is only showing the number of responses for answer "A" in all 5 columns. Second problem is that I need an "*" next to the correct answer. It is not doing it. PLEASE HELP! Here is my code:

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

void main()
{
  ifstream finans, fintest, fin;
  ofstream fout;

  char ch, ans[21], id[12], responses[21]; 
  int numstudents=0, numcorrect, i, j;
  int counter[20][5]={0};

  finans.open("Answers.dat");
  fintest.open("Tests.dat");
  fout.open("Output.dat");

  finans >> ans; //reads 20 char in a row   
  fintest >> id >> responses; //reads first student's id and that one student's responses

  fout << setw(15) << "Student ID" << "\t\t" << setw(15) << "Number Correct" << endl << endl;

  while(!fintest.eof())
  {
    numstudents++;
    numcorrect=0;
    for(i=0; i<=19; i++)
    {
	  if(ans[i]==responses[i])
	  {
		++numcorrect;
	  }
	  for(j=0; j<5; j++)
	  {
		if(responses[i]=='A') //THIS IS WHERE I'M CONFUSED
		{
			counter[i][j]++;
		}                     //END OF CONFUSING SECTION
	  }
	  cout << endl;
	}
	
	fout << setw(15) << id << "\t\t" << setw(15) << numcorrect << endl;
    fintest >> id >> responses;
  }

  fout << "\n\n" << setw(37) << "Number of students taking exam = " << numstudents << "\n\n\n\n";
  fout << setw(47) << "Question      A      B      C      D      E" << endl << endl;

  for(i=0; i<=19; i++) 
    {
	  fout << setw(10) << i+1 << "   ";
	  for(j=0; j<5; j++)
	  {
		  fout << setw(6);
		  if(ans[i]==responses[i]) //NOT SURE WHY THIS SECTION ISN'T WORKING
			fout << counter[i][j] << "*";
		  else if(ans[i]!=responses[i])
			fout << counter[i][j] << " ";
	  }
	  fout << endl;
	}
}


Here is my output:

     Student ID		 Number Correct

    430-52-6192		             13
    112-81-5225		             19
    120-49-5322		             12
    456-65-3211		              2
    990-45-0978		              4
    324-98-5445		              6
    112-34-3443		             12
    435-56-8790		             18
    452-23-5675		              6
    776-45-5454		              7
    567-71-9909		              6
    345-54-7834		              9
    555-12-2341		             18
    568-09-0096		             20
    664-61-0987		             10
    612-45-7687		              5
    567-23-1125		              3
    561-43-6781		              6
    498-12-4321		              3
    479-01-4867		              4
    489-90-0999		             17
    456-09-1111		             19
    561-66-6657		              3


    Number of students taking exam = 23



    Question      A      B      C      D      E

         1        5      5      5      5      5 
         2        4      4      4      4      4 
         3        2      2      2      2      2 
         4        2      2      2      2      2 
         5        2      2      2      2      2 
         6        5*     5*     5*     5*     5*
         7        5      5      5      5      5 
         8       11     11     11     11     11 
         9        3      3      3      3      3 
        10        0      0      0      0      0 
        11        6      6      6      6      6 
        12        2      2      2      2      2 
        13        9      9      9      9      9 
        14        4      4      4      4      4 
        15        3      3      3      3      3 
        16        1      1      1      1      1 
        17        6      6      6      6      6 
        18        4      4      4      4      4 
        19        0      0      0      0      0 
        20        2*     2*     2*     2*     2*


Here is what I need the LAST PART to look like:

Question        A	B	C	D	E
1		5	1	13*	3	1
2		4	7*	1	7	4
3		2	3	2	2	14*
etc.
etc.
20		2	0	1	1	19*
(I had a longer post, but my internet went down).

The for loop at line 34 is making every 2nd dimension of "counter" the amount of letter 'A' answers.

Are you required to have a for loop there? It is confusing, I think if you wanted two for loops then the first one (line 28) needs to be inside this one.

Without a loop it makes more sense to me:

1
2
3
4
5
if (responses[i]=='A') counter[i][0]++;
else if (responses[i]=='B') counter[i][1]++;
else if (responses[i]=='C') counter[i][2]++;
else if (responses[i]=='D') counter[i][3]++;
else if (responses[i]=='E') counter[i][4]++;


I think this will work. Taking advantage of chars being numeric:

counter[i][responses[i] - 'A']++;

Here, if the response is 'A' then counter[i][0] gets increased ('A' == 65. 65 - 65 = 0). If the response is 'B', then counter[i][1] gets updated ('B' == 66, 66-65 = 1).
AWESOME! That helped get the correct numbers in the array. However, I still don't know how to get the "*" in the right place. See the last section in the code where I print the array.
For that loop you don't really care who said what, you only care about what the correct answer is. So, forget "responses", and focus on how to get the "*" in the right position depending upon what the answer actually is.
Last edited on
I tried this code but keep getting the asterisk on every number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for(i=0; i<=19; i++) 
    {
	  fout << setw(10) << i+1 << "   ";
	  for(j=0; j<5; j++)
	  {
		  fout << setw(6);
		  if(ans[i]=='A')
			fout << counter[i][j] << "*";
		  else if(ans[i]=='B')
			fout << counter[i][j] << "*";
		  else if(ans[i]=='C')
			fout << counter[i][j] << "*";
		  else if(ans[i]=='D')
			fout << counter[i][j] << "*";
		  else if(ans[i]=='E')
			fout << counter[i][j] << "*";
		  else
			fout << counter[i][j] << " ";
	  }
	  fout << endl;
	}


Output:

Question      A      B      C      D      E

         1        5*     1*    13*     3*     1*
         2        4*     7*     1*     7*     4*
         3        2*     3*     2*     2*    14*
         4        2*     1*     3*    13*     4*
         5        2*     8*     1*     4*     8*
         6        5*     1*     3*     3*    11*
         7        5*     7*     0*     4*     7*
         8       11*     4*     0*     3*     5*
         9        3*     2*     1*     2*    15*
        10        0*     1*    12*     3*     7*
        11        6*     2*     4*    10*     1*
        12        2*     2*     4*     9*     6*
        13        9*     4*     0*     6*     4*
        14        4*     2*     2*     2*    13*
        15        3*     3*     1*     1*    15*
        16        1*     8*     4*     3*     7*
        17        6*     3*     1*     7*     6*
        18        4*     1*     2*     5*    11*
        19        0*     1*     1*    10*    11*
        20        2*     0*     1*     1*    19*
Do you see what is happening? If the answer is 'A', 'B', 'C', 'D', or 'E', print a "*".

You always print counter[i][j], so :

1
2
3
4
5
for (int j = 0 ; j < 5; j++){
   fout << setw(6) << counter [i][j];

   // Now use an if statement to figure out whether an "*" or not
}


Then I believe a similar method can be used in my first post. Think about the relationship between j and ans[i].

edit: sorry if I'm leading you on too much.
Last edited on
I don't get it. I am a mechanical engineering student and not a comp sci. Wish I understood programming better.
I tried this code like you said but it's still doing the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for(i=0; i<=19; i++) 
    {
	  fout << setw(10) << i+1 << "   ";
	  for(j=0; j<5; j++)
	  {
		  fout << setw(6) << counter[i][j];
		  if(ans[i]=='A')
			fout << "*";
		  else if(ans[i]=='B')
			fout << "*";
		  else if(ans[i]=='C')
			fout << "*";
		  else if(ans[i]=='D')
			fout << "*";
		  else if(ans[i]=='E')
			fout << "*";
		  else
			fout << " ";
	  }
	  
	  fout << endl;
	}
Right, your if statement is set up so that it will always print an astrix. You know, if its 'a', print "*", else if 'b', print "*"...

You have to figure out how to print an astrix based only on the value of j and the value of ans[i].
THANK YOU SO MUCH! Thanks to your input I finally finished the program. Here's what I changed. Thanks again! That totally makes sense now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for(i=0; i<=19; i++) 
    {
	  fout << setw(10) << i+1 << "   ";
	  for(j=0; j<5; j++)
	  {
		  fout << setw(6) << counter[i][j];
		  if(ans[i]=='A' && j==0)
			fout << "*";
		  else if(ans[i]=='B' && j==1)
			fout << "*";
		  else if(ans[i]=='C' && j==2)
			fout << "*";
		  else if(ans[i]=='D' && j==3)
			fout << "*";
		  else if(ans[i]=='E' && j==4)
			fout << "*";
		  else
			fout << " ";
	  }
	  
	  fout << endl;
	}
Topic archived. No new replies allowed.