-4

I am quite new to c++ and programming in general. In my program, I am attempting to calculate the weight of an object on other planets by asking the user for a earth weight and then the planet they want to compare the earth weight with. Using switch(planetnumber), I was hoping that based on the number they picked(1-6) the program would define the variable otherweight based on the case selected. I ran into a error: "lvalue required as left operand of assignment weight * 0.78 = otherweight;" This did not work the way it was intended, could anyone point me in the right direction?

#include <iostream>

int main()
{
    double weight;
    int planetnumber;
    double otherweight;
    std::cout << "Enter weight\n";
    std::cin >> weight;
    std::cout << "Please select your arena\n";
    std::cin >> planetnumber;
    switch (planetnumber)
    {
        case 1:
            weight * 0.78 = otherweight;
            break;
        case 2:
            weight * 0.39 = otherweight;
            break;
        case 3:
            weight * 2.65 == otherweight;
            break;
        case 4:
            weight * 1.17 = otherweight;
            break;
        case 5:
            weight * 1.05 = otherweight;
            break;
        case 6:
            weight * 1.23 = otherweight;
            break;
        default:
            std::cout << "Not a valid input\n";
            break;
    }
    std::cout << "your weight is " << otherweight << std::endl;
}
  • `==` is test for equality. `=` is an assignment. Your compiler should be spitting out a whole bunch of warnings. Don't ignore the warnings. They are the first line of defense against logic errors. – user4581301 Dec 18 '18 at 01:01
  • 5
    `weight*0.78==otherweight;` -- this takes the value of `weight`, multiplies it by 0.78, and compares it with the value of `otherweight`. The results of the comparison are completely ignored. This does absolutely nothing. Whatever you're trying to do here, this is not the right way to do it. You're not going to go very far learning C++ by guessing the C++ syntax. The only way to learn C++ is by reading a good book, and practicing its examples. Attempting to write your own code by guessing what the right C++ syntax is, is not going to be very productive. – Sam Varshavchik Dec 18 '18 at 01:01
  • What book or tutorial are you working from? – Galik Dec 18 '18 at 01:27
  • lvalue means left value ([or it least is used to](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues)). In most math the value being assigned to goes on the left. This error means you cannot assign to what's currently on the left. Take Sam's advice and [get a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Until you know what good C++ looks like, you can't tell it from bad C++, leading many into bad books and websites. – user4581301 Dec 18 '18 at 01:48
  • a suggestion, don't leave uninitialized variables that you're going to read. For instance, you have "otherweight" which has no value, and if the input is not a number between 1 and 6 you enter the default case, where otherweight will still have no value, and then you try to print it. It's undefined behaviour, and it can print literally any possible number without consistency. Just assign 0 in the 5th line after you declare the variable ( double otherweight = 0; ) – Barnack Dec 18 '18 at 03:00

1 Answers1

2

In C / C++ the result goes on the left, the equation goes on the right. You just need to transpose e.g.

otherweight = 0.78 * weight;
rwhenderson
  • 84
  • 1
  • 6