c++ - Making program so that if statement is assigned proper number -


i'm trying making if planet name not found in array, set code variable -1, seems make variable -1 regardless. if don't have else statement, program works fine, switch's default statement doesn't work. how make code variable gets assign proper value if planet name entered incorrectly.

#include "stdafx.h" #include <iostream> #include <string> #include <algorithm> using namespace std;   int main() { //declare arrays string planetnames[8] = { "mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune" }; double gravity[8] = { 0.37, 0.78, 1, 0.38, 2.64, 1.16, 1.07, 1.21 };  //declare variable string name = " "; double weight = 0.0; string planet = " "; double finalweight = 0.0; int code = 0;  //explain program user cout << "in program enter first , last name, weight, , planet want know weight on" << endl << endl;  //get input cout << "what first , last name: "; getline(cin, name); cout << "how weigh: "; cin >> weight; cout << "which planet know how weigh on: "; cin >> planet; //capitalize planet name transform(planet.begin(), planet.end(), planet.begin(), toupper);  while (weight != -1) {     (int x = 0; x < 8; x++)     {         if (planet == planetnames[x])             code = x; //this problem is!!!!!!!!!         else             code = -1;          //end if     }//end      //calculate , display weights     switch (code)     {     case 0:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     case 1:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     case 2:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     case 3:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     case 4:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     case 5:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     case 6:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     case 7:         finalweight = weight * gravity[code];         cout << "the weight of " << name << " on " << planetnames[code] << " " << finalweight << "lbs" << endl;         break;     default:         cout << "invalid planet name" << endl;     }//end switch      cout << endl;     cout << "how weigh(-1 end program): ";     cin >> weight;     cout << "which planet know how weigh on: ";     cin >> planet;     transform(planet.begin(), planet.end(), planet.begin(), toupper); }//end while    system("pause"); return 0; 

}

you need break loop after have found matching planet name, otherwise loop keeps searching, , next name won't match code set -1.

also: efficiency , clarity: set code = -1 before loop , set x on match:

code = -1; (int x = 0; x < 8; x++) {     if (planet == planetnames[x])     {         code = x; //this problem is!!!!!!!!!         break; // , it's gone ;-)     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -