I cant figure out how to write the code where if the user inputs anything else than (a to h), it gives an output of Invalid Selection. Or if the user enters q, the application Quits. Here is my code so far. Please Help

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

int main ()
{
   double distance;
   int weight;
   double gravity;
   char planet;
   int travelSpeed;
   string planetName;

   cout << "                            INTERPLANETARY TRAVEL" << endl;
   cout << "                            ---------------------" << endl << endl;
   cout << " This program will generate information for a user about traveling easily to other planets.";
   cout << " This program will perform calculations concerning weight on various planets as well as travel time between planets." << endl << endl;
   cout << "Interplanetary Travel Menu\n";
   cout << "--------------------------" << endl << endl;
   cout << "a) Mercury\n";
   cout << "b) Venus\n";
   cout << "c) Earth\n";
   cout << "d) Mars\n";
   cout << "e) Jupiter\n";
   cout << "f) Saturn\n";
   cout << "g) Uranus\n";
   cout << "h) Neptune\n";
   cout << "q) Quit\n" << endl << endl;
   cout << "Select a planet to travel to or enter q to quit the program: ";
   cin  >> planet;

   if ((planet >= 'a') && (planet <= 'h'))

     if (planet == 'a')

      {
       planetName = "Mercury";
       gravity = 0.27;
       distance = 36000000.00;
       }
     else 

           if (planet == 'b')
           {
            planetName = "Venus";
            gravity = 0.86;
            distance = 67000000.00;
            }
          else 

                if (planet == 'c')
                {
                 planetName = "Earth";
                 gravity = 1.00;
                 distance = 93000000.00;
                 }
               else 

                     if (planet == 'd')
                    {
                      planetName = "Mars";
                      gravity = 0.37;
                      distance = 141000000.00;
                      }
                    else 

                     if (planet == 'e')
                     {
                         planetName = "Jupiter";
                         gravity = 2.64;
                         distance = 483000000.00;
                     }
                     else

                         if (planet == 'f')
                         {
                             planetName = "Saturn";
                             gravity = 1.17;
                             distance = 886000000.00;
                         }
                         else

                         if (planet == 'g')
                         {
                             planetName = "Uranus";
                             gravity = 0.92;
                             distance = 1782000000.00;
                         }
                         else

                             if (planet == 'h')
                             {
                                 planetName = "Neptune";
                                 gravity = 1.44;
                                 distance = 2793000000.00;
                             }


   cout << "\n\nPlease enter your weight in pounds: ";
   cin >> weight;

   cout << "\n\nEnter the speed in which you would like to travel (MPH): ";
   cin >> travelSpeed; 

   cout << "\n\nINTERPLANETARY TRAVEL:  Earth to " << planetName << endl;
   cout << "--------------------------------------------" << endl << endl;
   cout << "Your weight on " << planetName << ": " << weight * gravity << endl;
   cout << "\nYour travel time to " << planetName << ":\n" << endl;
   cout << fixed << setprecision(0) << setw(5) << "In Hours: " << (distance / travelSpeed) << " hours" << endl;
   cout << setw(5) << "In Days: " << (distance / travelSpeed / 24) << " days" << endl;
   cout << setw(5) << "In Years: " << (distance / travelSpeed / 365) << " years\n\n" << endl;

   system ("PAUSE");
   return 0;
}

Hmm,

if (key_pressed == 'q')
    return 0;

That should do it. Also, try using a switch instead of the above. Makes code a little neat.

it worked thanks. how about for invalid selections?

i thought you had the code for it already:

>if ((planet >= 'a') && (planet <= 'h'))

Just handle the invalid selection in the else part.

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.