How to find out the size of an array of characters?

1
#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 << "Nome: ";
        cin >> nome[i];

    for(int j=0;j < 5;j++){

        cout << "Questão ("<<(j+1)<<"): ";
        cin >> respostas[i][5];

    }

}


    cout << "General report"<<"\n";

    for(int i=0;i<n;i++){
        cout << nome[i] <<": ";

        for(int j=0;j<sizeof(respostas);j++){
            cout << respostas[i][j];
        }
    }





}

    
asked by anonymous 20.02.2016 / 03:40

2 answers

1

As the number of responses per student is fixed (in case 5), you can use the function sizeof with only the first element of the vector:

sizeof(respostas[0])

Responding to comment: "the program does not show the typed letters", there is a problem in reading student responses, since the input is storing these responses in a position beyond the vector limit (position 5) in: cin >> respostas[i][5]; .

To solve, just put the index j :

...
for(int j=0; j < 5; j++) {
     cout << "Questão (" << (j+1) << "): ";
     cin >> respostas[i][j]; // nesta linha
};
...

The complete program looks 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 running, exit 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 std :: vector class of C ++ to store the data.

This class, among other advantages, provides the size() method, which returns the number of vector elements (similarly to length of the Java language.)

    
20.02.2016 / 06:18
0

Use the sizeof function:

#include <iostream>
...

for (j = 0; j < sizeof(respostas); j++) {
   cout << respostas[i][j];
}
    
20.02.2016 / 04:30