【问题标题】:Do-While to ask user to repeat entire int main programmeDo-While 要求用户重复整个 int 主程序
【发布时间】:2015-04-29 07:25:45
【问题描述】:

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

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

using namespace std;   

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

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

}

我想请用户重复该程序。如果输入 y,则重复 int main 直到再次提出相同的重复问题。 Else to cout

【问题讨论】:

  • 把你想重复的代码放在一个单独的函数中,当输入为'y'时运行函数

标签: c++ user-interface main repeat do-while


【解决方案1】:
#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();
    }
}

【讨论】:

  • 对我来说,这看起来像是我必须将已经包含在 int main () 中的整个代码放入 void repeat () 括号中?对不起,如果我弄错了,但这看起来还有很长的路要走
  • @MikeEricson 是的。 repeat 在循环内被调用,所以repeat定义 内的所有代码都将在循环内执行。
【解决方案2】:
#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;
}

【讨论】:

  • 对于类,你可以添加这个。
【解决方案3】:

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

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 &lt;stdlib.h&gt;。在 C++ 中,当使用 C 头文件时,您应该使用带有 c 前缀的头文件名:#include &lt;cstdlib&gt;#include &lt;ctime&gt;

【讨论】:

  • 它说错误:在')'令牌之前预期的不合格ID。在第一行... for (;;)
  • @user2970916 是的,这也是可能的,添加到答案中。
  • @MikeEricson 另外,添加了关于您的包含的旁注。
  • @zenith 非常感谢。我是编程新手,因此为什么我需要我能得到的所有帮助。 Do l 放置 bool repeat = true; while(重复)在 int main 之前或之后;
  • @MikeEricson 您的程序从int main() { 开始执行,所以在这种情况下,您希望它们 main 函数(在{} 之间) )。
猜你喜欢
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 2014-11-30
相关资源
最近更新 更多