hey ya'll i've made this code (simple, bc that's what i am) and i think it would work except for the tiny problem that i always get the error that the switch statement is illegal, but i don't know what to put instead (when i do run the program, it stops and says that it can't run anymore b/c the statement isn't defined). any helps would be great, thankies..here's the code....

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


int main ()
{
float inWeight;
float inPlanet;
int planet;



cout << "Enter you weight.\n";
cin >> inWeight;


cout << "Press 1 for Mercury\n";
cout << "2 for Venus\n";
cout << "3 for Earth\n";
cout << "4 for Moon\n";
cout << "5 for Mars\n";
cout << "6 for Jupiter\n";
cout << "7 for Saturn\n";
cout << "8 for Uranus\n";
cout << "9 for Neptune\n";
cout << "10 for Pluto\n";
cin >> inPlanet;


switch (planet)
{
case 1: cout << (inWeight * 0.4155);
break;
case 2: cout << (inWeight * 0.8975);
break;
case 3: cout << (inWeight * 1.0);
break;
case 4: cout << (inWeight * 0.166);
break;
case 5: cout << (inWeight * 0.3507);
break;
case 6: cout << (inWeight * 2.5374);
break;
case 7: cout << (inWeight * 1.0677);
break;
case 8: cout << (inWeight * 0.8947);
break;
case 9: cout << (inWeight * 1.1794);
break;
case 10: cout << (inWeight * 0.0899);
break;
default : cout << "Not a planet.";
}
return 0;


}

Dani AI

Generated

The immediate problem was the input/variable mismatch that and flagged: the value you read from the user must be the same integral value you switch on. Beyond that simple fix, a couple of practical tips make this kind of program more robust and easier to maintain.

Use input validation so non-numeric or out-of-range answers don’t blow up the program. Example pattern (input loop only):

#include <iostream>
#include <limits>

int planet;
std::cout << "Choose planet (1-10): ";
while (!(std::cin >> planet) || planet < 1 || planet > 10) {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Invalid selection. Enter a number 1..10: ";
}

Instead of a long switch, map the planet index to a factor array. That centralizes the numbers, avoids duplicated code, and makes it trivial to change formatting or add planets:

#include <iostream>
#include <iomanip>

double factors[] = { 0.0, 0.4155, 0.8975, 1.0, 0.166, 0.3507, 2.5374, 1.0677, 0.8947, 1.1794, 0.0899 };
// after validating `planet`:
double weight; std::cin >> weight;
double result = weight * factors[planet];
std::cout << std::fixed << std::setprecision(2) << result << '\n';

Other quick notes: prefer double for numeric accuracy, always check std::cin for failure, and consider using named constants or an enum if you want readable names instead of numbers. For a concise reference on what types switch accepts, see the C++ language summary on switches (cppreference switch). If keeping Pluto in a menu matters, note its IAU reclassification as a dwarf planet in 2006 ().

Recommended Answers

All 6 Replies

My guess is that it has something to do with this confusion:

float inPlanet;
	int planet;

	cin >> inPlanet;

	switch (planet)

rite, but i don't know how to fix it, regardless of the int

Well which do you want? planet is an uninitialized variable, so your results will be undefined. And inPlanet is actually obtained from user input, but it is not used, and cannot be used in the switch. Given that, I might try:

cin >> planet;

switch (planet)

didn't work, but that's ok
thanks anywho

didn't work, but that's ok
thanks anywho

::shakes head::

#include <iostream>
using namespace std;

int main ()
{
   float weight;
   int planet;

   cout << "Enter you weight.\n";
   cin  >> weight;

   cout << "Press 1 for Mercury\n";
   cout << "2 for Venus\n";
   cout << "3 for Earth\n";
   cout << "4 for Moon\n";
   cout << "5 for Mars\n";
   cout << "6 for Jupiter\n";
   cout << "7 for Saturn\n";
   cout << "8 for Uranus\n";
   cout << "9 for Neptune\n";
   cout << "10 for Pluto\n";
   cin  >> planet;

   switch ( planet )
   {
   case  1: cout << (weight * 0.4155);  break;
   case  2: cout << (weight * 0.8975);  break;
   case  3: cout << (weight * 1.0);     break;
   case  4: cout << (weight * 0.166);   break;
   case  5: cout << (weight * 0.3507);  break;
   case  6: cout << (weight * 2.5374);  break;
   case  7: cout << (weight * 1.0677);  break;
   case  8: cout << (weight * 0.8947);  break;
   case  9: cout << (weight * 1.1794);  break;
   case 10: cout << (weight * 0.0899);  break;
   default: cout << "Not a planet.";
   }
   return 0;

}

/* my output
Enter you weight.
150
Press 1 for Mercury
2 for Venus
3 for Earth
4 for Moon
5 for Mars
6 for Jupiter
7 for Saturn
8 for Uranus
9 for Neptune
10 for Pluto
6
380.61
*/

Swithc works with integral types, so inPlanet (float) will not work. Instead, try this

cin >> planet;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.