2015-10-16 122 views
0
#include <iostream> 
#include <iomanip> 

using namespace std; 

void far_cels(); 
void cels_far(); 
void quit(); 
void answerF(double); 
void answerC(double); 
double getTemp(); 



int main() 
{ 
cout << "--MENU--\n" << "PLease make a selection\n" 
    << "1: Farenheit to Celsius conversion\n" 
    << "2: Celsius to Farenheit conversion\n" 
    << "3: Quit Program\n" 
    << "MAKE SELECTION : "; 

int num = 0; 
cin >> num; 
switch (num) { 
    case 1:far_cels(); 
    case 2:cels_far(); 
    case 3: quit(); 
    default: return main(); 
} 

} 



double getTemp() 
{ 
    double temp; 
    cout << "Enter the tempature you want to convert\n"; 
    cin >> temp; 
    return temp; 
} 


void far_cels() 
{ 
double farenheit; 
double celsius; 
farenheit = getTemp(); 
celsius = farenheit - 32/1.8; 
answerC(celsius); 

} 
void cels_far() 
{ 
double farenheit; 
double celsius; 
celsius = getTemp(); 
farenheit = celsius * 1.8 + 32; 
answerF(farenheit); 

} 
void answerF(double farenheit) 
{ 
cout << "The Tempature is: " << endl; 
cout << setprecision (4); 
cout << farenheit <<" degrees farenheit " << endl; 

} 
void answerC(double celsius) 
{ 
cout << "Your temperature is: " << endl; 
cout << setprecision (4); 
cout << celsius <<" degrees celsius " << endl; 

} 
void quit(){ 

} 

我似乎無法弄清楚如何清除屏幕並返回main(),如果沒有激活switch語句。我已經想出瞭如何返回到主菜單,但菜單仍然存在。任何幫助將不勝感激我剛剛開始,需要我可以得到的所有建議。代碼的其他部分似乎運作良好,但如果我有人可以看到我在做什麼的問題,也將不勝感激。我也在Mac上使用Xcode。C++程序清除屏幕

+2

1:您需要在'switch'塊中添加一些'break'。 2:可能不會調用'main'遞歸。 3:你認爲哪條線應該「清除屏幕」? – MooseBoys

+0

如果沒有進行菜單選擇,我想清除屏幕。但我剛剛決定使用一個函數來關閉多行新行。 – chris101010

回答

0

使用while循環顯示菜單,直到選中quit並將一些中斷放入開關盒中。

while(true){ 
    cout << "--MENU--\n" << "PLease make a selection\n" 
    << "1: Farenheit to Celsius conversion\n" 
    << "2: Celsius to Farenheit conversion\n" 
    << "3: Quit Program\n" 
    << "MAKE SELECTION : "; 

    int num = 0; 
    cin >> num; 
    switch (num) { 
     case 1:far_cels();break; 
     case 2:cels_far();break; 
     case 3: quit(); 
     default: return 0; 
    } 
} 

參考here清除屏幕。