As the number of responses per student is fixed (in case 5), you can use the function sizeof
only with the first element of the vector:
sizeof(respostas[0])
Responding to the comment: "the program does not show the typed letters", there is a problem in reading the students' answers, because the input is storing these answers in a position beyond the vector boundary (position 5) in: cin >> respostas[i][5];
.
To solve, simply put the index j
:
...
for(int j=0; j < 5; j++) {
cout << "Questão (" << (j+1) << "): ";
cin >> respostas[i][j]; // nesta linha
};
...
The whole program goes like this:
#include <iostream>
#include <string>
#include <locale.h>
#include <stdio.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main() {
setlocale(LC_ALL,"Portuguese");
int n = 0;
cout << "Quantos alunos?";
cin >> n;
char gabarito[5];
char respostas[n][5];
string nome[n];
for(int g = 0; g < 5; g++) {
cout << "Gabarito: ";
cin >> gabarito[g];
};
for(int i=0; i < n; i++) {
cout << std::endl;
cout << "Nome: ";
cin >> nome[i];
for(int j=0; j < 5; j++) {
cout << "Questão (" << (j+1) << "): ";
// Aqui é j
cin >> respostas[i][j];
};
};
cout << std::endl;
cout << "General report"<< std::endl;
for(int i=0; i < n; i++) {
cout << nome[i] << ": ";
// sizeof(respostas[0]) ou sizeof(respostas[i])
for(int j=0; j < sizeof(respostas[0]); j++)
cout << respostas[i][j] << " ";
cout << std::endl;
};
}
After execution, the output of the program:
Quantos alunos?3
Gabarito: a
Gabarito: b
Gabarito: c
Gabarito: d
Gabarito: e
Nome: Aluno1
Questão (1): a
Questão (2): a
Questão (3): a
Questão (4): a
Questão (5): a
Nome: Aluno2
Questão (1): b
Questão (2): c
Questão (3): d
Questão (4): a
Questão (5): e
Nome: Aluno3
Questão (1): e
Questão (2): d
Questão (3): c
Questão (4): b
Questão (5): a
General report
Aluno1: a a a a a
Aluno2: b c d a e
Aluno3: e d c b a
Another possibility, perhaps simpler, is to use the class Std::vector C++ language for storing data.
This class, among other advantages, provides the method size()
, which returns the number of elements of the vector (similar to length
of the Java language.
"This section is in Java" and that function
cout
,cin
? I’ve never seen that in java– CypherPotato
It was in java* I forgot to edit hehe.
– Marcelo T. Cortes
In the solution you gave me using sizeof(replies) the console does not show the letters I typed.
– Marcelo T. Cortes
Um, it’s because of Cout...
– CypherPotato
Do you say the Cout that has the matrix answers[i][j]? What would it be?
– Marcelo T. Cortes
That function
cout
I guess she’s not doing what should be done, I don’t know why, it must be because I don’t know much about C++– CypherPotato
Try to put
std::cout
– CypherPotato