Do-While 要求用户重复整个 int 主程序

Do-While to ask user to repeat entire int main programme

对编程非常陌生,我找不到任何基本的在线解释或代码可以很好地满足我的需要。我有一段相当长的程序(大约 300 行),一切正常。这是给出思路的结构:

#include <iostream>   
#include <stdlib.h>    
#include <time.h>      
#include <vector>      
#include <algorithm>   

using namespace std;   

int main() 
{
      //code....

    { 
         //code... etc...
    }

}

我想请用户重播节目。如果输入 y,则重复 int main 直至再次询问相同的重复问题。否则计算<< "e.g. Thank you, goodbye";

这是一个简单的解决方案示例:

int main()
{
    for (;;) // "infinite" loop (while (true) is also possible)
    {
        // stuff to be repeated here

        cout << "Repeat? [y/n]" << endl;
        char answer;
        cin >> answer;
        if (answer == 'n')
            break; // exit loop
    }              // else repeat
    cout << "Thank you, goodbye" << endl;
}

这是另一个:

int main()
{
    bool repeat = true;
    while (repeat)
    {
        // stuff to be repeated here

        cout << "Repeat? [y/n]" << endl;
        char answer;
        cin >> answer;
        repeat = answer == 'y';
    }
    cout << "Thank you, goodbye" << endl;
}

附带说明一下,不要这样做:#include <stdlib.h>。在 C++ 中,使用 C 头文件时应使用 c 前缀头文件名:#include <cstdlib>#include <ctime>.

#include <iostream>   
#include <stdlib.h>    
#include <time.h>      
#include <vector>      
#include <algorithm>   

//using namespace std;   <--- Don't use using namespace std, it pollutes the namespace

void repeat()
{
   //... code to repeat
}

int main() 
{
      //code....
    char answer;
    while((std::cin >> answer) != 'y')
    { 
        repeat();
    }
}
#include <iostream>
#include <conio.h>

using namespace std;

//Class
class DollarToRs {
  public:

      int Dollar;
      int Rs;
      int ToRs;
      ConversionToRs() {
      cout << "Enter the amount of Dollar: ";
      cin >> Dollar;
      ToRs = Dollar * 154;
      cout << "This is the total amount in PKR: " << ToRs <<endl;
      }

};

int main()
{
  //Dollar Convertion Function
  DollarToRs convert;
  convert.ConversionToRs();


  //For Repeating Program

  int repeat;
  int exit;

  cout << "To repeat program enter 1" <<endl;
  cin >> repeat;

  while (repeat == 1) {
    convert.ConversionToRs();
    cout << "To repeat program enter 1" <<endl;
    cin >> repeat;
  }
  exit =0;

  if (exit == 0) {

  }

  getch();
  return 0;
}