C++ 某种';转到';用于C++; #包括 使用名称空间std; int main() { 浮动a、b、结果; 字符操作、响应; cin>>a>>操作>>b; 开关(操作) { 格“+”: 结果=a+b; 打破 案例'-': 结果=a-b; 打破 案例“*”: 结果=a*b; 打破 案例“/”: 结果=a/b; 打破 违约: cout

C++ 某种';转到';用于C++; #包括 使用名称空间std; int main() { 浮动a、b、结果; 字符操作、响应; cin>>a>>操作>>b; 开关(操作) { 格“+”: 结果=a+b; 打破 案例'-': 结果=a-b; 打破 案例“*”: 结果=a*b; 打破 案例“/”: 结果=a/b; 打破 违约: cout,c++,goto,C++,Goto,您可以使用循环do…while(条件) 比如: #include <iostream> using namespace std; int main() { float a, b, result; char operation, response; cin >> a >> operation >> b; switch(operation) { case '+': result = a + b;

您可以使用循环
do…while(条件)

比如:

#include <iostream>
using namespace std;

int main()
{
   float a, b, result;
   char operation, response;
   cin >> a >> operation >> b;
   switch(operation)
   {
   case '+':
         result = a + b;
         break;

   case '-':
         result = a - b;
         break;

   case '*':
         result = a * b;
         break;

   case '/':
         result = a / b;
         break;

   default:
         cout << "Invalid operation. Program terminated." << endl;
         return -1;
   }

   // Output result
   cout << "The result is " << result << endl;
   system("sleep 200");
   cout << "Do you want to make another opertaion? (Y/N)" << endl;
   cin >> response;
   if(response=='Y')
   {
    here
   }
   else if(response=='N')
   {
    cout << "The program will now close" << endl;
    system("sleep 500");
    return -1;
   }
   return 0;
}
do{
cin>>a>>操作>>b;
开关(操作)
{
...
}
//输出结果

Cuth

虽然 Goto 在C++中高度不赞成,但确实存在。 与汇编非常相似,您可以在代码中放置一个标签,然后使用

goto
命令它“跳转”到那里。然而,跳转在汇编中工作的方式与跳转相同,这可能会将您从所有循环中分离到跳转到的点

do{
    cin >> a >> operation >> b;
    switch(operation)
    {
...

    }

    // Output result
    cout << "The result is " << result << endl;
    system("sleep 200");
    cout << "Do you want to make another opertaion? (Y/N)" << endl;
    cin >> response;
}while(response=='Y');
如果这是从另一个位置调用的,我强烈建议使用do while而不是goto,因为它可能会导致问题


goto唯一真正的问题是它不优雅,让代码阅读起来很混乱。你应该尽可能使用循环和返回,并且只有在你认为没有其他方法可以使它达到最佳效果时才使用goto。

猜你在寻找一个循环,只要很好地组织你的程序控制流。没有必要使用
goto
或类似的方法。好吧C++确实有一个<代码> Goto 语句,虽然很多人会告诉你不要使用它。实际上,尽管确实有一些合理用法用于<代码> Goto ,但是你的情况不是其中之一。
#include <iostream>
using namespace std;

int main()
{
   label: 
   float a, b, result;
   char operation, response;
   cin >> a >> operation >> b;
//condensed for neatness

   // Output result
   cout << "The result is " << result << endl;
   system("sleep 200");
   cout << "Do you want to make another opertaion? (Y/N)" << endl;
   cin >> response;
   if(response=='Y')
   {
    goto label;
   }
   else if(response=='N')
   {
    cout << "The program will no`enter code here`w close" << endl;
    system("sleep 500");
    return -1;
   }
   return 0;
}
#include <iostream>
using namespace std;

int main()
{
   do{
   float a, b, result;
   char operation, response;
   cin >> a >> operation >> b;
   //condensed for neatness 


// Output result


   cout << "The result is " << result << endl;
   system("sleep 200");
   cout << "Do you want to make another opertaion? (Y/N)" << endl;
   }while(cin.get() == 'Y');


    cout << "The program will no`enter code here`w close" << endl;
    system("sleep 500");
    return -1;

   return 0;
}