c++ - Why is my code not exiting when Entering 3 in the Command prompt? -
i beginner , taking first c++ programming class. using ide named codeblocks , stumped. have searched forums answer cannot figure out on own.
i trying figure out why code not exiting when entering 3 in menu on command prompt. trying figure out why when trying convert fahrenheit celsius formula not working, when believe correct formula.
the code below:
#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int main () { float celsius; float fahrenheit; char x; //menu user choose option //preform. looping in case type in incorrect // response , choose again. { cout << "please choose option. please press enter. \n"; cout << "1. convert celsius fahrenheit.\n"; cout << "2. convert fahrenheit celsius. \n"; cout << "3. exit program \n"; cin >> x; if (x == '1') system ("cls"); { cout << "please enter degrees in celsius. \n"; cin >> celsius; system ("cls"); fahrenheit = 9.0 / 5 * celsius + 32; //calculate formula converting celsius fahrenheit. cout << fixed << showpoint << setprecision(2); cout << fixed << "the degrees in fahrenheit \n" << fahrenheit; cout << static_cast<char>(248) << endl; cout << "thank have great day!"; (x = '3'); } // user not want convert celsius fahrenheit // since user not want convert, display thank message. if (x == '2') { cout << "please enter degrees in fahrenheit. \n"; cin >> fahrenheit; celsius = (fahrenheit - 32) * 5.0 / 9 ; //calculate formula converting fahrenheit celsius. cout << fixed << showpoint << setprecision(2); cout << fixed << "the degrees in celsius \n" << celsius; cout << static_cast<char>(248) << endl; cout << "thank have great day!"; (x = '3'); } while (x != '3') return 0; } }
your code formating not good. don't do-while loop, when you're ending program after conversion anyway.
here version:
#include <iostream> using namespace std; int main() { float celsius; float fahrenheit; char x; cout << "please choose option. please press enter. \n"; cout << "1. convert celsius fahrenheit.\n"; cout << "2. convert fahrenheit celsius. \n"; cout << "3. exit program \n"; cin >> x; if(x == '1') { cout << "please enter degrees in celsius. \n"; cin >> celsius; system("cls"); fahrenheit = 9.0 / 5 * celsius + 32; printf("the degrees in fahrenheit %0.2f%c\nthank have great day!", fahrenheit, (char)248); } else if(x == '2') { cout << "please enter degrees in fahrenheit. \n"; cin >> fahrenheit; system("cls"); celsius = (fahrenheit - 32) * 5.0 / 9; printf("the degrees in celsius %0.2f%c\nthank have great day!", celsius, (char)248); } else { return 0; } getchar(); getchar(); return 0; }
Comments
Post a Comment