-11

I am new to C++ and to programming in general. I am trying to make this small code but when I want to use the if function I get this error.

I have no idea what it could be. I tried several conventions and none of them work. I was hoping someone could give me advice on how to write this line of code better. here is the thing

#include <iostream>
#define EXIT_SUCCESS 0
using namespace std;

int main()
{
    //variables
    char cod_ref[6];
    char des_prod[60];
    char disp;
    int size;
    int cost;
    int price;
    int num;
    //impresiones y asignación de variables
    cout<<"Digite el codigo de referencia del producto (5 digitos)" << endl;
    cin.getline(cod_ref, 6);
    cout<<"Digite la descripción del producto"<<endl;
    cin.getline(des_prod, 60);
    cout<<"Digite la talla del producto"<<endl;
    cin>>size;
    cout<<"Cantidad de productos"<<endl;
    cin>>num;
    cout<<"¿Disponible para la venta? S/N"<<endl;
    cin>>disp;
    cout<<"Digite el costo por unidad"<<endl;
    cin>>cost;
    cout<<"Digite el precio de venta por unidad"<<endl;
    cin>>price;

    //limpieza de pantalla
    system("cls");
    //impresión final
    cout<<"REFERENCIA: "<< cod_ref <<endl;
    cout<<"DESCRIPCIÓN: "<< des_prod  <<endl;
    cout<<"DISPONIBILIDAD: ";
    if( disp == "s"||"S" ){
        cout<<"Disponible";
    }else{
        cout<<"No Disponible";
    }
    cout<<"COSTO UNIDAD: "<< cost <<endl;
    cout<<"COSTO TOTAL: "<< cost * num <<endl;
    cout<<"PRECIO UNIDAD: "<< price <<endl;
    cout<<"PRECIO POR "<< num <<" UNIDADES: "<< price * num <<endl;
    cout<<"GANANCIA POR UNIDAD: "<< price - cost <<endl; 
    cout<<"GANANCIA TOTAL: " << (price * num) - (cost * num) <<endl;
    //cout<<"PORCENTAGE DE GANANCIA: "<<
    
    system("pause");
    return EXIT_SUCCESS;
}


here is where i get the error if( disp == "s"||"S" ){

1
  • A string such as “s” gets replaced by a pointer. The pointer is then tested for being nullptr, if it isn’t it’s equivalent to true and so the if is always true.
    – QuentinUK
    Sep 22 at 22:42

2 Answers 2

4

Use single quotes for char character comparisons, double quotes implies a string which is technically a char * type.

ie.

if (disp == 's' || disp == 'S')
2
  • 3
    Good answer because it fixes both problems with that if, but I strongly recommend a quick edit to add a one-liner explaining why you split up disp == "s"||"S". I know why. Clearly you know why, but just as clearly, the asker doesn't. Sep 22 at 22:38
  • 3
    "a string ... is technically a char * type" - that is incorrect. A string literal is a const char[N] array, where N is the length of the string +1 for the null terminator. An array decays into a pointer to its first element in certain contexts. Sep 23 at 1:04
2

The statement if (disp == "s"||"S") does not do what you think it does.

Due to the operator precedence rules, the == operator has a higher precedence than the || operator, so the statement is processed AS-IF you had written it like this instead:

if ( (disp == "s") || "S" )

And in the case of the expression disp == "s", disp is a single char that gets promoted to int in comparisons, and "s" is a string literal of type const char[2] which decays into a const char* pointer in this context. Thus, the compiler complains, because comparing an integer to a pointer is not a valid operation.

Since disp is a char, you would need to use ' instead of " in your comparison:

if ( disp == 's' || "S" )

That will at least get rid of the compiler error, but the code will still be logically wrong, because now the compiler will process it like this:

if ( (disp == 's') || "S" )

If disp equals s then the if evaluates as true, otherwise "S" is evaluated and can never decay into a null pointer, so it will always evaluate as true, thus your if will ALWAYS evaluate as true regardless of the value of disp. Same thing if you were to change "S" into 'S':

if ( (disp == 's') || 'S' )

So, you might consider moving the parenthesis around the values instead, eg:

if ( disp == ('s' || 'S') )

And that would compile, but still be logically wrong, because the expression 's' || 'S' will ALWAYS evaluate as true, which converts to integer 1, and so you would be comparing disp to 1, which will NEVER evaluate as true for user input.

To do what you are attempting, you need a separate == comparison for each value you want to compare, eg:

if ( (disp == 's') || (disp == 'S') )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.