2014-02-09 71 views
-2

这是我的程序,亚姆很抱歉,因为我只是一个noobie所以我想问我怎么能返回到菜单,因为亚姆使用switch语句,我想问他后,他/她完成转换。请参阅程序中的评论。如何返回到我的菜单C++

#include <iostream> 
#include <stdlib.h> 
#include <conio.h> 

using namespace std; 

int main() 
{ 
int option; 
float C,F,result; 
char rpt; 

cout<<"Temperature Converter" 
    <<"\n1.Celsius to Fahrenheit" 
    <<"\n2.Fahrenheit to Celsius" 
    <<"\n3.Exit" 
    <<"\n\nEnter a number from 1 to 3: "; 
cin>>option; 

switch (option) 
{ 
    case 1: 
    system ("CLS"); 
    cout<<"Celsius to Fahrenheit!"<<endl; 
    cout<<"\nEnter Celsius: "; 
    cin>>C; 

    result=C*9/5+32; 

    cout<<C<<" Celsius is equivalent to "<<result<<" Fahrenheit."<<endl<<endl; 


    cout<<"Would you like to convert again? Type Y to try again OR N to exit. : "; 
    cin>>rpt; 

    if (rpt='Y') 

    //What Code should i put here? 


    break; 
+2

该计划甚至不是语法上有效... –

回答

0

你应该使用循环。像这样:

bool ended = false; 
while (!ended) 
{ 
    //some code 
} 

如果答案是N变化结束为true。

或者,正如有些人喜欢:

bool ended; 
do 
{ 
    //some code 
} while(!ended); 
+2

因为我们会想他绝不会之前,第一次通过循环中止,我喜欢的是'做{.../*也许设置结束=真; */...} while(!ended);' – mah

0

把代码放到一个do while循环

int main() 
{ 
    do { 
    ..... 
    } while(rpt);// instead of if 

} 
-1

你应该做一个功能,即显示菜单和返回所选择的选项 例:

 int showMenu() { 
     //Show menu 
     //Read option 
    //Return option 
     }  
1

询问用户是否要重复的代码属于切换块。你也应该使用==运算符进行比较。因此,像

do { 

    // Get menu choice 
    ... 

    switch (option) 
    { 
     case 1: 
      ... 
      break; 

     case 2: 
      ... 
      break; 

     ... 
    } 

    cout<<"Would you like to convert again? Type Y to try again OR N to exit. : "; 
    cin>>rpt; 
} while (rpt == 'Y'); 
-1
// put the label Start below the declaration // 

if (rpt == 'Y') 

system ("CLS"); 
goto Start; 

else 

exit (0); 
+0

而不是仅仅给出代码,请解释逻辑。这使得OP和其他读者能够学习解决方案背后的原理,并将其应用于他们的具体问题。 – CodeMouse92

+0

请尝试添加一些代码的解释。这将有所帮助 – silwar