I cannot declare variables as a string in my C++ code

Asked

Viewed 1,808 times

1

#include <iostream>
#include <stdio.h>
#include <string>

void print(float arr[], string nome[], int x){

    for (int i=0; i < x; i++){
        printf("O aluno %s obteve media de %2.2f\n", nome[i],arr[i]);
    }

}

void ordem(float arr[], string nome[], int x){

    float aux;
    string caux;

    for (int i=0; i < x; i++){
        for (int j=i+1; j < x; j++){
            if (arr[j] > arr[i]){
                    aux = arr[i];
                    arr[i] = arr[j];
                    arr[j] = aux;

                    caux = nome[i];
                    nome[i] = nome[j];
                    nome[j] = caux;
            }
        }
    }

    print(arr, nome, x);

}


int main(){
    float A1[19], A2[19];
    float media[19];
    int i=0;
    const int x=5;
    string nome[x];

    while (i < x){
        printf("\nDigite o nome do Aluno: ");
        scanf("%s", &nome[i]);

        printf("\nDigite a nota A1 do %d aluno: ", i+1);
        scanf("%f", &A1[i]);
        printf("\nDigite a nota A2 do %d aluno: ", i+1);
        scanf("%f", &A2[i]);
        media[i] = (A1[i] + A2[i])/2;
        i++;
    }

    ordem(media, nome, x);

    return 0;
}

I used Codeblocks and Dev-C++ Ides and continued to error.LOGs

  • Avoid mixing C and C++. Specify Std:: before string.

  • 1

    Since you are using C++, use Std::vector instead of C arrays. Remember to use Std::, which is the namespace where the standard functions are. If you don’t want to, declare: using namespace Std;. But I recommend reading this at SO https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

2 answers

1


To use the string in C++, you must specify it from the namespace from which it is coming. In the case of string, the namespace is the std. In your case, just insert before each string use the text std::

#include <iostream>
#include <stdio.h>
#include <string>

void print(float arr[], std::string nome[], int x){

    for (int i=0; i < x; i++){
        printf("O aluno %s obteve media de %2.2f\n", nome[i],arr[i]);
    }

}

void ordem(float arr[], std::string nome[], int x){

    float aux;
    std::string caux;

    for (int i=0; i < x; i++){
        for (int j=i+1; j < x; j++){
            if (arr[j] > arr[i]){
                    aux = arr[i];
                    arr[i] = arr[j];
                    arr[j] = aux;

                    caux = nome[i];
                    nome[i] = nome[j];
                    nome[j] = caux;
            }
        }
    }

    print(arr, nome, x);

}


int main(){
    float A1[19], A2[19];
    float media[19];
    int i=0;
    const int x=5;
    std::string nome[x];

    while (i < x){
        printf("\nDigite o nome do Aluno: ");
        scanf("%s", &nome[i]);

        printf("\nDigite a nota A1 do %d aluno: ", i+1);
        scanf("%f", &A1[i]);
        printf("\nDigite a nota A2 do %d aluno: ", i+1);
        scanf("%f", &A2[i]);
        media[i] = (A1[i] + A2[i])/2;
        i++;
    }

    ordem(media, nome, x);

    return 0;
}

You can also declare using for the namespace to avoid typing Std::. In case, it would be:

using namespace std;

With the above code, preferably stated in a . cpp, you avoid the need to type Std:: before using string or any other namespace member.

I recommend you read this post too: Why is "using namespace Std" considered bad Practice?

Just as anonymously commented, avoid mixing C and c++. Use only if a third-party library you’re using needs it or if it works in legacy code.

In your case, I recommend reading about Std::vector that can be useful to you.

1

You need to specify the namespace that encompasses the class string, in that case the namespace is std. To define an object of type string you need to use the type std::string.

If you need to use this class often in the code you can avoid having to specify the namespace every time using the command using to reference the namespace or a specific function of the namespace within a code scope, example:

Specifies the namespace std globally:

#include <string>
using namespace std; // referencia todo o namespace std da biblioteca string

int main(int argc, char *argv[]) {
    string s = "exemplo";
    // code ...
}

To specify a specific function or class (it is useful when you will use only one or a few functions of the library):

#include <string>
using std::string; // usa a classe std::string

int main(int argc, char *argv[]) {
    string s = "exemplo";
    // code ...
}

Browser other questions tagged

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