error "was not declared in this scope" c ++ [closed]

1
#include < iostream >
using namespace std;

int main() {
    int n1, n2, resultado;
    char operacion(15);
    cout << "Ingrese número 1           ";
    cin >> n1;
    cout << "Ingrese número 2           ";
    cin >> n2;
    cout << "Ingrese operación          ";
    if (operacion == suma) {
        resultado = n1 + n2;
        cout << "Elresultado de la suma es  " << resultado;
    } else {
        if (operacion == resta) {
            resultado = n1 - n2;
            cout << "El resultado de la resta es  " << resultado;
        } else {
            if (operacion == multiplicación) {
                resultado = n1 * n2;
                cout << " El resultado de la multiplicación es " << resultado;
            } else {
                if (operacion == división) {
                    resultado = n1 / n2;
                    cout << " Elresultado de la división es" << resultado;
                }
            }
        }
    }

    return 0;
}
4:1: error: stray '3' in program
24:1: error: stray '3' in program
29:5: error: stray '3' in program
29:5: error: stray '3' in program
 In function 'int main()':
13:18: error: 'suma' was not declared in this scope
19:19: error: 'resta' was not declared in this scope
24:19: error: 'multiplicaci' was not declared in this scope
24:33: error: expected ')' before 'n'
29:22: error: 'divisi' was not declared in this scope
29:30: error: expected ')' before 'n'

Another thing that I wanted to know if there is a type of variable to put signs ("# $% & /% /%)

    
asked by meli 09321 17.10.2018 в 21:02
source

3 answers

1

I give you the answer here:

Errors you have

  • You are not stating the variables for the operations you are going to compare, or you are not placing them as text strings.
  • You are not receiving the last value of the operation.
  • int main() {
    
        int n1, n2, resultado;
        string operacion ;
    
        string suma = "suma";
        string resta = "resta";
        string multiplicacion = "multiplicacion";
        string division = "division";
        cout << "Ingrese numero 1           ";
        cin >> n1;
        cout << "Ingrese número 2           ";
        cin >> n2;
        cout << "Ingrese operacion          ";
        cin >> operacion;
        if (operacion == suma) {
            resultado = n1 + n2;
            cout << "Elresultado de la suma es  " << resultado;
        }
        else {
            if (operacion == resta) {
                resultado = n1 - n2;
                cout << "El resultado de la resta es  " << resultado;
            }
            else {
                if (operacion == multiplicacion) {
                    resultado = n1 * n2;
                    cout << " El resultado de la multiplicación es " << resultado;
                }
                else {
                    if (operacion == division) {
                        resultado = n1 / n2;
                        cout << " Elresultado de la división es" << resultado;
                    }
                }
            }
        }
    
        return 0;
    }
    
        
    answered by 17.10.2018 в 22:11
    1

    I have read the errors and they are several: You have not declared the variables of addition, subtraction, multiplication and division. To do this, you can do it this way:

    string suma = "suma";
    string resta = "resta";
    string division = "division";
    string multiplicacion = "multiplicacion";
    

    Another error is that you have missed a ) in some line of your code. Try to put the line where you get the error to know where it is.

    Finally, you have asked if there is a variable to put signs. Yes, there is, it is the variable type char , that you have used it at the beginning of the code and, for the exercise that you propose, I do not think it is useful. You could create a variable of type string string operacion; and not one of type char; . Also, instead of declaring variables addition, subtraction etc ... you can simply put:

    if(operacion == "suma"){
    
        //Código.
    
    }
    

    Instead of saving the value in a variable of type string , you put the value directly in the if . This way you save yourself creating more variables, although they always come in handy.

    NOTICE

    You have used unclaimed variables (multiplication, for example) named with a tilde, that is, NO declare a variable like this: int multiplicación; Declare it with this name: int multiplicacion; . Always without accents or ñ , those characters are not allowed and are annoying if you are working with others who do not have them.

        
    answered by 18.10.2018 в 10:55
    -5
    char   operacion (15) ;
    

    sera?

    char   operacion [15] ;
    

    or better:

    std::string operacion;
    

    Or how do you want to define the operacion ? Yes with the words "addition", "subtraction", etc. the string serves you, comparing it operacion == "suma" , etc. If you prefer a symbol '+' , '-' , etc, you define the operation as char and you compare it with =='+' , etc.

        
    answered by 17.10.2018 в 21:07