0

I am new to C++ and started learning it just for fun. I am using a C++ course in Codecademy I wrote the program below but unable to compile it. I keep getting the error "Else without a previous else if"...

    #include <iostream>

int main() {

  double earthw = 0;
  double planet = 0;
  double weight = 0;
  //Ask and receive earth weight
  std::cout << "What is your earth weight? \n";
  std::cin >> earthw;
  //Ask which planet to figth on
  std::cout << "Which Planet Would you like to fight in? \n";
  //List all planets
  std::cout << "1 - Venus\n";
  std::cout << "2 - Mars\n";
  std::cout << "3 - Jupiter\n";
  std::cout << "4 - Saturn\n";
  std::cout << "5 - Uranus\n";
  std::cout << "6 - Neptune\n";
  //Receive planet number
  std::cin >> planet;
  //Calculations and inputing weight on specific planet
  if (planet == 1) {
    weight = earthw * 0.78;
  }

    else if (planet == 2) {
    weight = earthw * 0.39;
  }

    else if (planet == 3) {
    weight = earthw * 2.65;
  }

    else if (planet == 4) {
    weight = earthw * 1.17;
  }

    else if (planet == 5) {
    weight = earthw * 1.05;
  }

    else if (planet == 6) {
    weight = earthw * 1.23;
  }
    std::cout << "Your weight is: " << weight << "Kg\n";

}
simo110
  • 15
  • 5
  • Show full error message please. – Alexander Ushakov Oct 13 '19 at 07:08
  • I can compile and run it. And this may not necessarily be mentioned, `return 0` from your `main` function. – ghchoi Oct 13 '19 at 07:10
  • Here it is: $ g++ space.cpp space.cpp: In fucntion 'int main ()': space.cpp:48.4: error: 'else' without a else { ^~~~ – simo110 Oct 13 '19 at 07:13
  • do you mean i should add return 0? – simo110 Oct 13 '19 at 07:14
  • I just compiled and ran it using Visual Studio Community. This could be a bug in the codecademy compiler. Or maybe they want me to write the code exactly their way. – simo110 Oct 13 '19 at 07:18
  • explicit return 0 from main makes no sense. It is clear in the standard that it is not needed! https://stackoverflow.com/questions/1188335/why-default-return-value-of-main-is-0-and-not-exit-success – Klaus Oct 13 '19 at 07:24
  • everything compiles fine for me. If you have to deal with such problems on some online site, think about using a good book and a free compiler and start developing your own projects on your local pc. – Klaus Oct 13 '19 at 07:28
  • This doesn't address the question, but writing essentially the same code in six different places is usually not a good idea. I'd create an array of multipliers, `double multiplier[] = { 0.78, 0.39, 2.65, 1.17, 1.05, 1.23 };` and use the input value as an index into the array: `if (1 <= planet && planet <= 6) weight = earthw * multiple[planet - 1];`. – Pete Becker Oct 13 '19 at 16:36
  • I am yet to learn about arrays and other concepts. As I mentioned, I am very new to this and if statements and switches is what I am reading about now. – simo110 Oct 13 '19 at 16:58

2 Answers2

0

There is no such problem in your code, and it compiles successfully in visual studio. Sometimes the online compiler can be really buggy. If it's required to complie it successfully in Codecademy, you may send a link to that Codecademy task to get a more-accurate answer.

0

Usually you get this error code when you put a semicolon (;) after your if statement. In this case the compiler should be the problem. In Visual Studio 2019 there is no problem with the program.

tibiv111
  • 105
  • 8