Unable to exit program properly

Hello I am wondering how I could have the user enter "exit" in the update read and have it end. I thought about creating another string variable for the exit command to test in the outer loop however the problem is that I am trying to have the user enter in another weight to continue the program or type in exit to end the program and I can't get them to input correctly.

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

using namespace std;

int main()
{
string planet[8] = { "MERCURY", "VENUS", "EARTH", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE" };
double gravity[8] = { 0.37, 0.78, 1.00, 0.38, 2.64, 1.16, 1.07, 1.21 };
double weight = 0.0;
string name = "";
string plan = "";
string leave = "";
double newweight = 0.0;
int planetnum = 8;

cout << "Use the keyboard to enter the required information." << endl;
cout << "HEllO! Would you like to learn what your weight is on another planet?" << endl;
cout << "Please enter your first name: ";
cin >> name;
cout << endl;
//priming read
cout << "Please enter your weight on the great planet Earth: ";
cin >> weight;
weight = toupper(weight);
while (weight >= 0 || leave != "EXIT")
{
cout << "Lets see now, which planet would you like to convert your weight to?" << endl;
cout << "Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune" << endl;
cout << "Enter planet name: ";
cin >> plan;
transform(plan.begin(), plan.end(), plan.begin(), ::toupper);
for (int num = 0; num < 8; num += 1)
{
if (planet[num] == plan)
{
newweight = gravity[num] * weight;
planetnum = num;
}//end of if statement
}//end of for loop
switch(planetnum)
{
case 0:
cout << name << " your weight on Mercury, the smallest planet in the solar system is: " << newweight << endl;
break;
case 1:
cout << name << " your weight on Venus, sometimes refered to as the 'Morning Star' is: " << newweight << endl;
break;
case 2:
cout << name << " your weight on Earth is the same you entered silly :) " << newweight << endl;
break;
case 3:
cout << name << " your weight on Mars, the closest planet to Earth is: " << newweight << endl;
break;
case 4:
cout << name << " your weight on Jupiter, the largest planet in our solar system is: " << newweight << endl;
break;
case 5:
cout << name << " your weight on Saturn, the second largest planet in our solar system is: " << newweight << endl;
break;
case 6:
cout << name << " your weight on Uranus, the coldest planet in our solar system is: " << newweight << endl;
break;
case 7:
cout << name << " your weight on Neptune, the eight planet in our solar system is: " << newweight << endl;
break;
default:
cout << "Oh no! It seems like the planet name you entered is incorrect. :( Please try again." << endl;
}//end of switch command
//update read
cout << endl;
cout << endl;
cout << "Please enter another weight (enter "exit" to end program): ";
cin >> weight >> leave; //This is what I am talking about inputing a double and string variable there has to be a way.
transform(leave.begin(), leave.end(), leave.end(), ::toupper);
}//end of while loop
system("pause");
return 0;
}//end of main function





Thank You

> weight = toupper(weight);
¿?

> enter in another weight to continue the program or type in exit to end the program
read the input as a string, then try to convert it to double.
http://en.cppreference.com/w/cpp/string/basic_string/stof


> Mars, the closest planet to Earth
I would have said Venus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iomanip>
#include<iostream>
#include<string>
#include<algorithm>

int main()
{
    const int N = 8 ;
    const std::string planet[N] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
    const double gravity[N] = { 0.37, 0.78, 1.00, 0.38, 2.64, 1.16, 1.07, 1.21 };
    const std::string descr[N] =
    {
        "the smallest planet in the solar system",
        "sometimes referred to as the 'Morning Star'",
        "... what ever for earth ...",
        "... what ever for mars ...",
        "... what ever 4 ...",
        "... what ever 5 ...",
        "... what ever 6 ...",
        "... what ever 7 ..."
    };

    double weight = 0.0;

    while( std::cout << "\nPlease enter your weight on the great planet Earth\n(enter a negative weight "
                        "or non-numeric input eg. \"exit\" to end program): " &&
           std::cin >> weight && weight >= 0 )
    {
        std::cout << "\nLets see now, which planet would you like to convert your weight to?\n" ;
        for( auto& name : planet ) std::cout << name << ' ' ;

        std::cout << "\nEnter planet name: ";
        std::string planet_name ;
        std::cin >> planet_name ;

        // make first letter upper case, everything else lower case
        for( char& c : planet_name ) c = std::tolower(c) ;
        planet_name.front() = std::toupper( planet_name.front() ) ;

        // locate the planet number
        const int planet_num = std::find( planet, planet+N, planet_name ) - planet ;
        if( planet_num == N )
            std::cout << "Oh no! It seems like the planet name you entered is incorrect :(\n" ;
        else
        {
            std::cout << "\nyour weight on " << planet_name << ", " << descr[planet_num]
                      << ", is " << gravity[planet_num] * weight << '\n' ;
        }
    }//end of while loop
}//end of main functio 
Topic archived. No new replies allowed.