/* ** ENGR 131, Monday 3:30 Recitation (Amanda Rohn) ** Homework #3 - Unit Converter ** ** Solution */ #include using std::cout; using std::cin; using std::endl; int main() { cout << "Welcome to the Unit Converter!\n"; int choice; //holds user's choice from menus char yn; //holds user's choice of whether to quit ('y','Y','n', or 'N') double length; //holds value in original unit do //main program loop { for(;;) //input loop { cout << "\nWhat unit would you like to convert?\n" << "1. Meters (m)\n" << "2. Miles (mi)\n" << "3. Kilometers (km)\n" << "4. Feet (ft)\n" << "Enter choice: "; cin >> choice; if(choice >= 1 && choice <= 4) //if choice is valid, go on with program break; else cout << "\nThat was an invalid choice. Try again.\n"; } //end input loop switch(choice) //main (choice-to-x) conversion switch statement { case 1: //meters cout << "\nEnter length in meters: "; cin >> length; //assume user enters a number for(;;) //input loop { cout << "\nWhat unit would you like to convert meters to?\n" << "1. Miles (mi)\n" << "2. Kilometers (km)\n" << "3. Feet (ft)\n" << "Enter choice: "; cin >> choice; //recycle variable if(choice >= 1 && choice <= 3) break; else cout << "\nInvalid choice. Try again.\n"; } //end input loop switch(choice) //meters-to-choice conversion { case 1: cout << endl << length << " meters = " << length * .000621 //1 m = .000621 mi << " miles\n\n"; break; case 2: cout << endl << length << " meters = " << length * .001 //1 m = .001 km << " kilometers\n\n"; break; case 3: cout << endl << length << " meters = " << length * 3.28 //1 m = 3.28 ft << " feet\n\n"; break; default: cout << "Can't happen!"; //error-checking should've caught this case } //end meters-to-choice conversion break; //end meters case case 2: //miles cout << "\nEnter length in miles: "; cin >> length; //assume user enters a number for(;;) //input loop { cout << "\nWhat unit would you like to convert miles to?\n" << "1. Meters (m)\n" << "2. Kilometers (km)\n" << "3. Feet (ft)\n" << "Enter choice: "; cin >> choice; //recycle variable if(choice >= 1 && choice <= 3) break; else cout << "\nInvalid choice. Try again.\n"; } //end input loop switch(choice) //miles-to-choice conversion { case 1: cout << endl << length << " miles = " << length * 1609 // 1 mi = 1609 m << " meters\n\n"; break; case 2: cout << endl << length << " miles = " << length * 1.61 // 1 mi = 1.61 km << " kilometers\n\n"; break; case 3: cout << endl << length << " miles = " << length * 5280 // 1 mi = 5280 ft << " feet\n\n"; break; default: cout << "Can't happen!"; //error-checking should've caught this case } //end miles-to-choice conversion break; //end miles case case 3: //kilometers cout << "\nEnter length in kilometers: "; cin >> length; //assume user enters a number for(;;) //input loop { cout << "\nWhat unit would you like to convert kilometers to?\n" << "1. Meters (in)\n" << "2. Miles (m)\n" << "3. Feet (ft)\n" << "Enter choice: "; cin >> choice; //recycle variable if(choice >= 1 && choice <= 3) break; else cout << "\nInvalid choice. Try again.\n"; } //end input loop switch(choice) //kilometers-to-choice conversion { case 1: cout << endl << length << " kilometers = " << length * 1000 // 1 km = 1000 m << " meters\n\n"; break; case 2: cout << endl << length << " kilometers = " << length * .62 // 1 km = .62 mi << " miles\n\n"; break; case 3: cout << endl << length << " kilometers = " << length * 3280 // 1 km = 3280 ft << " feet\n\n"; break; default: cout << "Can't happen!"; //error-checking should've caught this case } //end kilometers-to-choice conversion break; //end kilometers case case 4: //feet cout << "\nEnter length in feet: "; cin >> length; //assume user enters a number for(;;) //input loop { cout << "\nWhat unit would you like to convert feet to?\n" << "1. Meters (in)\n" << "2. Miles (m)\n" << "3. Kilometers (km)\n" << "Enter choice: "; cin >> choice; //recycle variable if(choice >= 1 && choice <= 3) break; else cout << "\nInvalid choice. Try again.\n"; } //end input loop switch(choice) //feet-to-choice conversion { case 1: cout << endl << length << " feet = " << length / 3.28 // 1 ft = 1/3.28 m << " meters\n\n"; break; case 2: cout << endl << length << " feet = " << length / 5280 // 1 ft = 1/5280 mi << " miles\n\n"; break; case 3: cout << endl << length << " feet = " << length / 3280 // 1 ft = 1/3280 km << " kilometers\n\n"; break; default: cout << "Can't happen!"; //error-checking should've caught this case } //end feet-to-choice conversion break; //end feet case default: //can't happen cout << "Can't happen"; } //end choice-to-x conversion switch statement for(;;) { cout << "Would you like to do another conversion? (Y/N): "; cin >> yn; if(yn == 'y' || yn == 'Y' || yn == 'n' || yn == 'N') //valid input break; else cout << "That wasn't a valid answer.\n"; //ask again } }while(yn == 'y' || yn == 'Y'); //end main loop cout << "\nThank you for using the Unit Converter\n"; return 0; } //end program