Is this a if while loop error?

Hello, I’m working on a project to write a program that lets you convert different temperatures.
I figured I had to use while loops, in the if statements in case you enter a wrong number.

However, when I try to compile it nothing seems to happen. Is this a if while loop error or am I just missing something. :smiley: :smiley:

#include <iostream>


int main() {

double celsius;
double fahrenheit;
double temperature;
double rankine;
double kelvin;
int scalein;
int scaleout;

std::cout << "Pick which temperature scale you want to use.\n";
std::cout << "1 Fahrenheit    2 Celsius    \n";
std::cout << "3 Rankine       4 Kelvin     \n";
std::cout << "Enter here:"; std::cin >> scalein;

  while (scalein >= 4 || scalein > 0) {
    std::cout << "\nPlease enter a proper number for the temperature scale.\n\n";
    std::cout << "1 Fahrenheit    2 Celsius    \n";
    std::cout << "3 Rankine       4 Kelvin     \n";
    std::cout << "Enter here:"; std::cin >> scalein;
  }

std::cout << "Enter temperature: ";
std::cout << "Enter here:"; std::cin >> temperature;



// Fahrenheit to X
if (scalein == 1) {
  std::cout << "Pick which scale you want to convert to.\n";
  std::cout << "2 Celsius    3 Rankine    4 Kelvin\n";
  std::cout << "Enter here:"; std::cin >> scaleout;
  while (scaleout < 2 || scaleout > 4) {
    std::cout << "Please choose one of the temperature scales.\n";
    std::cout << "2 Celsius    3 Rankine    4 Kelvin\n";
    std::cout << "Enter here:"; std::cin >> scaleout;
  }
    if (scaleout == 2) {
    celsius = (temperature - 32) * (5/9);
    std::cout << temperature << " in Fahrenheit is equal to " << celsius << " in Celsius\n";
  } else   if (scaleout == 3) {
    rankine = temperature + 459.67;
    std::cout << temperature << " in Fahrenheit is equal to " << rankine << " in Rankine\n";
  } else if (scaleout == 4) {
    kelvin = (((temperature - 32) * 5) / 9) + 273.15;
    std::cout << temperature << " in Fahrenheit is equal to " << kelvin << " in Kelvin\n";
  } 





// Celsius to X
  } else if (scalein == 2) {
  std::cout << "Pick which scale you want to convert to.\n";
  std::cout << "1 Fahrenheit    3 Rankine    4 Kelvin\n";
  std::cout << "Enter here:"; std::cin >> scaleout; 
   while (scaleout < 1 || scaleout == 2 || scaleout > 4) {
    std::cout << "Please choose one of the temperature scales.\n";
    std::cout << "1 Fahrenheit    3 Rankine    4 Kelvin\n";
    std::cout << "Enter here:"; std::cin >> scaleout;
  }
    if (scaleout == 1) {
    fahrenheit = (temperature * 1.8) + 32;
    std::cout << temperature << " in Celsius is equal to " << fahrenheit << " in Fahrenheit\n";
  } else   if (scaleout == 3) {
    rankine = (temperature * 1.8 + 491.67);
    std::cout << temperature << " in Celsius is equal to " << rankine << " in Rankine\n";
  } else if (scaleout == 4) {
    kelvin = temperature + 273.15;
    std::cout << temperature << " in Celsius is equal to " << kelvin << " in Kelvin\n";
  } 





// Rankine to X
  } else if (scalein == 3) {
  std::cout << "Pick which scale you want to convert to.\n";
  std::cout << "1 Fahrenheit    2 Celsius    4 Kelvin\n";
  std::cout << "Enter here:"; std::cin >> scaleout;
    while (scaleout < 1 || scaleout == 3 || scaleout > 4) {
    std::cout << "Please choose one of the temperature scales.\n";
    std::cout << "1 Fahrenheit    2 Celsius    4 Kelvin\n";
    std::cout << "Enter here:"; std::cin >> scaleout;
  }
    if (scaleout == 1) {
    fahrenheit = temperature + 359.67;
    std::cout << temperature << " in Rankine is equal to " << fahrenheit << " in Fahrenheit\n";
  } else   if (scaleout == 2) {
    celsius = (temperature - 491.67) * 5/9;
    std::cout << temperature << " in Rankine is equal to " << celsius << " in Celsius\n";
  } else if (scaleout == 4) {
    kelvin = temperature / 1.8;
    std::cout << temperature << " in Rankine is equal to " << kelvin << " in Kelvin\n";
  } 





// Kelvin to X
  } else (scalein == 4) {
  std::cout << "Pick which scale you want to convert to.\n";
  std::cout << "1 Fahrenheit    2 Celsius    3 Rankine\n";
  std::cout << "Enter here:"; std::cin >> scaleout;
      while (scaleout < 1 || scaleout > 3) {
    std::cout << "Please choose one of the temperature scales.\n";
    std::cout << "1 Fahrenheit    2 Celsius    3 Rankine\n";
    std::cout << "Enter here:"; std::cin >> scaleout;
  }
    if (scaleout == 1) {
    fahrenheit = (((temperature - 273.15) *9) /5) + 32;
    std::cout << temperature << " in Kelvin is equal to " << fahrenheit << " in Fahrenheit\n";
  } else   if (scaleout == 2) {
    celsius = temperature - 273.15;
    std::cout << temperature << " in Kelvin is equal to " << celsius << " in Celsius\n";
  } else if (scaleout == 3) {
    rankine = temperature * 1.8;
    std::cout << temperature << " in Kelvin is equal to " << rankine << " in Rankine\n";
  } 
  }


}

I think the condition for the while loop is wrong:
while (scalein >= 4 || scalein > 0) {
should be
while (scalein >= 4 || scalein < 0) {
because you want the loop to run if scalein is not 1,2,3, or 4.

Also else can’t have a condition.

Change that else to else if
} else if(scalein == 4) {

#include <iostream> int main() { double celsius = 20; double fahrenheit = 100; double temperature = 50; double rankine = 50; double kelvin = 300; int scalein = 1; int scaleout = 2; std::cout << "Pick which temperature scale you want to use.\n"; std::cout << "1 Fahrenheit 2 Celsius \n"; std::cout << "3 Rankine 4 Kelvin \n"; std::cout << "Enter here:"; std::cin >> scalein; while (scalein >= 4 || scalein < 0) { std::cout << "\nPlease enter a proper number for the temperature scale.\n\n"; std::cout << "1 Fahrenheit 2 Celsius \n"; std::cout << "3 Rankine 4 Kelvin \n"; std::cout << "Enter here:"; std::cin >> scalein; } std::cout << "Enter temperature: "; std::cout << "Enter here:"; std::cin >> temperature; // Fahrenheit to X if (scalein == 1) { std::cout << "Pick which scale you want to convert to.\n"; std::cout << "2 Celsius 3 Rankine 4 Kelvin\n"; std::cout << "Enter here:"; std::cin >> scaleout; while (scaleout < 2 || scaleout > 4) { std::cout << "Please choose one of the temperature scales.\n"; std::cout << "2 Celsius 3 Rankine 4 Kelvin\n"; std::cout << "Enter here:"; std::cin >> scaleout; } if (scaleout == 2) { celsius = (temperature - 32) * (5/9); std::cout << temperature << " in Fahrenheit is equal to " << celsius << " in Celsius\n"; } else if (scaleout == 3) { rankine = temperature + 459.67; std::cout << temperature << " in Fahrenheit is equal to " << rankine << " in Rankine\n"; } else if (scaleout == 4) { kelvin = (((temperature - 32) * 5) / 9) + 273.15; std::cout << temperature << " in Fahrenheit is equal to " << kelvin << " in Kelvin\n"; } // Celsius to X } else if (scalein == 2) { std::cout << "Pick which scale you want to convert to.\n"; std::cout << "1 Fahrenheit 3 Rankine 4 Kelvin\n"; std::cout << "Enter here:"; std::cin >> scaleout; while (scaleout < 1 || scaleout == 2 || scaleout > 4) { std::cout << "Please choose one of the temperature scales.\n"; std::cout << "1 Fahrenheit 3 Rankine 4 Kelvin\n"; std::cout << "Enter here:"; std::cin >> scaleout; } if (scaleout == 1) { fahrenheit = (temperature * 1.8) + 32; std::cout << temperature << " in Celsius is equal to " << fahrenheit << " in Fahrenheit\n"; } else if (scaleout == 3) { rankine = (temperature * 1.8 + 491.67); std::cout << temperature << " in Celsius is equal to " << rankine << " in Rankine\n"; } else if (scaleout == 4) { kelvin = temperature + 273.15; std::cout << temperature << " in Celsius is equal to " << kelvin << " in Kelvin\n"; } // Rankine to X } else if (scalein == 3) { std::cout << "Pick which scale you want to convert to.\n"; std::cout << "1 Fahrenheit 2 Celsius 4 Kelvin\n"; std::cout << "Enter here:"; std::cin >> scaleout; while (scaleout < 1 || scaleout == 3 || scaleout > 4) { std::cout << "Please choose one of the temperature scales.\n"; std::cout << "1 Fahrenheit 2 Celsius 4 Kelvin\n"; std::cout << "Enter here:"; std::cin >> scaleout; } if (scaleout == 1) { fahrenheit = temperature + 359.67; std::cout << temperature << " in Rankine is equal to " << fahrenheit << " in Fahrenheit\n"; } else if (scaleout == 2) { celsius = (temperature - 491.67) * 5/9; std::cout << temperature << " in Rankine is equal to " << celsius << " in Celsius\n"; } else if (scaleout == 4) { kelvin = temperature / 1.8; std::cout << temperature << " in Rankine is equal to " << kelvin << " in Kelvin\n"; } // Kelvin to X } else if (scalein == 4) { std::cout << "Pick which scale you want to convert to.\n"; std::cout << "1 Fahrenheit 2 Celsius 3 Rankine\n"; std::cout << "Enter here:"; std::cin >> scaleout; while (scaleout < 1 || scaleout > 3) { std::cout << "Please choose one of the temperature scales.\n"; std::cout << "1 Fahrenheit 2 Celsius 3 Rankine\n"; std::cout << "Enter here:"; std::cin >> scaleout; } if (scaleout == 1) { fahrenheit = (((temperature - 273.15) *9) /5) + 32; std::cout << temperature << " in Kelvin is equal to " << fahrenheit << " in Fahrenheit\n"; } else if (scaleout == 2) { celsius = temperature - 273.15; std::cout << temperature << " in Kelvin is equal to " << celsius << " in Celsius\n"; } else if (scaleout == 3) { rankine = temperature * 1.8; std::cout << temperature << " in Kelvin is equal to " << rankine << " in Rankine\n"; } } return 0; }

Note that Codebytes and most of the Codecademy stuff can’t do user input.

1 Like

Wow, can’t believe I missed that! It works now thanks so much!