Search code examples
c++switch-statementoperator-keywordcout

I cannot understand the compiler C++ switch, invalid operand error


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float Litr;
    int opcja;

    cout << "Konwerter" << endl;
    switch(opcja){

case 1:
    cout << "Litr na barylke amerykanska i galon amerykanski" << endl;
    cin >> Litr;
    cout << Litr << " litrow to " << Litr * 159 << " barylek i " << Litr * 3,78 << "galonow.";
    break;
    }
    return 0;
}



Error in line 16 (e.g cout << Litr << " litrow to " << Litr * 159 << " barylek i " << Litr * 3,78 << "galonow."; )

||=== Build: Debug in aeiou (compiler: GNU GCC Compiler) ===| C:\Users*file loaction*\main.cpp|16|error: invalid operands of types 'int' and 'const char [9]' to binary 'operator<<'| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Don't understand what's wrong and what compiler tries to tell me.


Solution

  • In this case, they are simple syntactic errors. When you have to express a decimal number in C ++ you have to use the "." and not the ",". There was also the problem that the command to execute was not asked in input and a simple cin was inserted.

    #include <iostream>
    
    using namespace std;
    
    int main() {
        int opcja;
        
        cout << "Konwerter: ";
        cin >> opcja;
        
        switch(opcja) {
            case 1: {
                float Litr;
                cout << "Litr na barylke amerykanska i galon amerykanski" << endl;
                cin >> Litr;
                cout << Litr << " litrow to " << Litr * 159 << " barylek i " << Litr * 3.78 << "galonow.";
                break;
            }
        }
    
        return 0;
    }