How to find the size of a character array?

Asked

Viewed 1,024 times

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];
        }
    }





}

Basically how do I do answers[i]. length in c++? To show the characters that were in the array?

  • "This section is in Java" and that function cout, cin? I’ve never seen that in java

  • 1

    It was in java* I forgot to edit hehe.

  • In the solution you gave me using sizeof(replies) the console does not show the letters I typed.

  • Um, it’s because of Cout...

  • Do you say the Cout that has the matrix answers[i][j]? What would it be?

  • 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++

  • Try to put std::cout

Show 2 more comments

2 answers

1


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.

  • Thank you very much, just a maybe silly doubt I’m new in c++, which means the semicolon after the brackets?

  • @Jarwin It is a (bad) custom of mine, because I usually program in C. In C++ language, it is not necessary :)

  • @Jarwin: Only to complement: after the end of a block, after the key };, it is not necessary to put the ;. But after the clasps ]; , the semicolon is required to indicate the end of the line

0

Use the function sizeof:

#include <iostream>
...

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

    I want the feedback to come out along with the names. I put the whole code to improve understanding.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.