While loop malfunction, also center formatting is jacked up
Sep 18, 2013 at 1:15am UTC
I have this program that is almost complete, but for some reason that while loop is messing me up, I have it set so that if the 'choice' is not 1, 2 , or 3, then it should have the user re input which choice.
If a number is not (such as 0, f, 43, etc) then my cout simply runs and runs and runs.
Also, I dont underestand why "End program..." is not centered.
Thanks alot anybody!!
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
// A simple C++ program
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void center(string display);
void showMenu();
void celsiusToFarenheit();
void farenheitToCelsius();
int choice;
int main()
{
char choice;
//Constants for the menu choice
cout << fixed << showpoint << setprecision (2);
do
{
flush(stdin);
//Display the menu and get the user's choice.
showMenu();
cin >> choice;
//Validate the menu selection.
while (!(choice == '1' || choice == '2' || choice == '3' ))
{
center( "Are you drunk?? thats invalid order." );
cout<< endl;
cin.clear();
fflush (stdin);
center ("Re-Enter your choice : " );
}
switch (choice)
{
case '1' :
cout<< endl;
center("Enter Fahrenheit temperature for conversion to Celsius : " );
farenheitToCelsius();
break ;
case '2' :
cout << endl;
center("Enter Celsius temperature for conversion to Fahrenheit : " );
celsiusToFarenheit();
break ;
case '3' :
center ("Programmer: Andrew Holder" );
cout << endl;
center ("BYE BYE!!! Press <Enter> key to END the program..." );
fflush(stdin);
cin.get();
break ;
}
system("CLS" );
}
while (choice!= '3' );
cout << endl << endl;
fflush(stdin);
return 0;
}
void showMenu()
{
cout << endl;
center("C O N V E R S I O N" );
cout<< endl ;
center("=~=~=~=~=~=~=~=~=~=" );
cout<<endl << endl;
center("1 Farenheit to Celsius" );
cout<<endl;
center("2 Celsius to Farenheit" );
cout<<endl;
center("3 End program." );
cout << endl <<endl;
center("Enter Your Choice: " );
}
void farenheitToCelsius()
{
double Farenheit, Celsius;
cin >> Farenheit;
Celsius = (Farenheit-32) * (5.0/9.0);
cout << "\t\t Here you go: " << Farenheit << char (248)<<" F" << " = " << Celsius<< char (248)<< " C" ;
cout << endl << endl<< endl;
center("Press <Enter> key to continue..." );
fflush stdin;
cin.get();
}
void celsiusToFarenheit()
{
double Farenheit, Celsius;
cin >> Celsius;
Farenheit = ((9.0/5.0) * Celsius) + 32.0;
cout << "\t\t Here you go: " << Celsius << char (248)<<" C" << " = " << Farenheit<< char (248)<< " F" ;
cout << endl << endl << endl;
center("Press <Enter> key to continue..." );
fflush stdin;
cin.get();
}
void center(string display)
{
int number = ((80 - display.length()))/ 2;
for (int i=1; i<=number; i++)
cout << " " ;
cout << display;
}
Topic archived. No new replies allowed.