Help with sentinel value and loop.
Nov 23, 2013 at 3:44am UTC
Hello everyone. I am writing a program to convert various temperature types with user input. My program compiles fine, but the values are stored after each loop. How do I reset them for a clean conversion? Also, my program should execute after hitting -999, it doesn't work and I'm not sure why. Any help is appreciated.
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
double C = 0;
double F = 0;
double R = 0;
double K = 0;
double temperature = 0;
int temp;
cout << "Enter F for a celcius to fahrenheit conversion.\nEnter K for a celcius to kelvin conversion." << "\nEnter R for fahrenheit to rankine conversion.\nHit -999 to end program.\nEnter C for a celsius temp." << endl;
while ((temp = cin.get()) != '-999' )
{
cout << "Enter a temperature to be converted: " << endl;
switch (temp)
{
case 'C' :
case 'c' :
cin >> temperature;
C += temperature;
cout << "Celsius" << setw(17) << "Kelvin" << setw(17) << "Fahrenheit" <<
setw(17) << "Rankine" << endl;
cout << "----------" << setw(17) << "---------" << setw(17) << "-------- ----" << setw(17) << "--------" << endl;
cout << C << setw(17) << K << setw(17) << F << setw(17) << R << endl;
break ;
case 'F' :
case 'f' :
cin >> temperature;
F = temperature * 9/5 + 32;
cout << "Celsius" << setw(17) << "Kelvin" << setw(17) << "Fahrenheit" <<
setw(17) << "Rankine" << endl;
cout << "----------" << setw(17) << "---------" << setw(17) << "------------" << setw(17) << "--------" << endl;
cout << C << setw(17) << K << setw(17) << F << setw(17) << R << endl;
break ;
case 'K' :
case 'k' :
cin >> temperature;
K = temperature + 273;
cout << "Celsius" << setw(17) << "Kelvin" << setw(17) << "Fahrenheit" <<
setw(17) << "Rankine" << endl;
cout << "----------" << setw(17) << "---------" << setw(17) << "-------------" << setw(17) << "--------" << endl;
cout << C << setw(17) << K << setw(17) << F << setw(17) << R << endl;
break ;
case 'R' :
case 'r' :
cin >> temperature;
R = (F-32) + 491.67;
cout << "Celsius" << setw(17) << "Kelvin" << setw(17) << "Fahrenheit" <<
setw(17) << "Rankine" << endl;
cout << "----------" << setw(17) << "---------" << setw(17) << "-------------" << setw(17) << "--------" << endl;
cout << C << setw(17) << K << setw(17) << F << setw(17) << R << endl;
break ;
case '\n' :
case '\t' :
case ' ' :
break ;
default :
cout << "Incorrect temperature entered." << "Enter a new one" << endl;
break ;
}
}
}
Nov 23, 2013 at 4:13am UTC
((temp = cin.get()) != '-999' )
should be
temp equals cin.get()
then
if temp !equals -999
Topic archived. No new replies allowed.