// // ------ scale mutator --- // void Temperature::Scale(scales inScale) { if (scale_ == inScale) { return; } else switch (inScale) { case CELSIUS: switch(scale_) { case FARRENHEIT: temp_ = FtoC( temp_); scale_ = CELSIUS; break; case KELVIN: temp_ = KtoC( temp_); scale_ = CELSIUS; break; } break; case FARRENHEIT: switch(scale_) { case KELVIN: temp_ = KtoC( temp_); case CELSIUS: temp_ = CtoF( temp_); scale_ = FARRENHEIT; break; } break; case KELVIN: switch(scale_) { case FARRENHEIT: temp_ = FtoC( temp_); case CELSIUS: temp_ = CtoK( temp_); scale_ = KELVIN; break; } break; default: std::cerr << "Invalid SCALE. SCALE must be C, F or K"; } }
/* convert F to C using function */ main() { int fahr; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%3d %6.1f\n", fahr, FtoC(fahr)); }
int main(void) { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; printf("F C\n\n"); fahr = lower; while(fahr <= upper) { celsius = FtoC(fahr); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } return 0; }
int main(){ // instructions std::cout << "Enter a rounded temperature value followed by its type (103F, 2C, 453K)\n"; std::cout << "Exit code: e\n"; // variables int length; bool valid; std::string temp; std::string line; // will loop until given exit code of e; do{ std::cout << "> "; // create separation stream std::stringstream ss; ss.clear(); // get input from user getline(std::cin, line); // calculate length length = line.length(); // check input if(length==1){ // if exit code given if(!line.compare("e")){ break; } // display error and restart a loop std::cout <<"Invalid Input\n"; continue; } // gather assumed value from input for(int i = 0; i <=line.length()-2; i++){ ss << line[i]; } // check if assumed input is valud temp = ss.str(); if(!is_number(temp)){ std::cout << "Input not valid\n"; continue; } // convert string temp to an integer int target = atoi(temp.c_str()); //switch for type and display conversions char type = line[line.length()-1]; switch(type){ case 'f': case 'F': std::cout << "Fahrenheit: " << target << std::endl; FtoC(target); FtoK(target); break; case 'c': case 'C': std::cout << "Celsius: " << target << std::endl; CtoF(target); CtoK(target); break; case 'k': case 'K': std::cout << "Kelvin: " << target << std::endl; KtoF(target); KtoC(target); break; default: std::cout << "Invalid type\n"; } }while(1); return 0; }