如何在循环后重新开始 [英] How do make a fresh start after the loop

查看:315
本文介绍了如何在循环后重新开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 int main()
{//在循环过程中,我的程序将跳过此部分,直接从第二个 段开始。
cout<< 嘿那里,我该怎么称呼你? << ENDL;
getline(cin,name);
system(cls);

cout<< 嘿<<名称<< 今天我会帮你计算一次往返需要支付的汽油费。
system(暂停);
system(cls);


Info();
Graphics();
system(暂停);

bool input = 1;
while(输入)

{
char choice;
cout<< 你想再试一次吗?(y / n)<< ENDL;
cin>>选择;



开关(选择)
{
案例'y':
输入= 0;
main();
返回0;
休息;

case'n':
cout<< huehuehuehuehue bye!;
输入= 0;
休息;

默认值:
cout<< 输入无效。 << ENDL;
输入= 1;
休息;
}
}

}





我尝试过:



我什么都没试过。

解决方案

你需要了解软件设计和构建软件的架构。



另一个外循环是 obvoius 最简单的解决方案。但是你应该考虑一下acore功能并将其写入一个额外的功能。调用主递归是非常糟糕的风格。



你的内部函数应该有一个返回值来表示是否中止或进一步运行。



你的启动代码也应该有自己的功能。所以它看起来像这样:

  int  main()
{
Init(); // startup

int loop = 1 ;

while (loop)
{
loop = StartComputation();
}

return 0 ;
}

BTW:

系统(暂停); 

也是不好的风格(但我不知道更好的方法)


int main()
{       //During loop, my program will skip this section and start straight from second  paragraph.
	cout << "Hey there, what shall i call you?" << endl;
	getline(cin,name);
	system("cls");
	
	cout << "Hey " << name << ".\nToday I will help you on calculating the cost of petrol you need to pay for a round trip.\n"; 
	system("pause");
	system("cls");	
	

	Info();
	Graphics();
	system("pause");
	
		bool input = 1;
		while(input)
		
		{	
		char choice;
		cout << "Do you want to try again? (y/n)" << endl;
		cin >> choice;
	
	
	
			switch(choice)
			{
			case 'y':
			 input = 0;
			 main();
			 return 0;
			 break;
		
			case 'n':
			 cout << "huehuehuehuehue bye!";
			 input = 0;
			break;
		
			default :
			 cout << "invalid input." << endl;
			 input = 1;
			break;
			}
		}
	
}



What I have tried:

I have tried nothing.

解决方案

You need to laern about software design and architecture for structuring your software.

Another outer loop is obvoius the simplest solution. But you should think about acore function and write it in an extra function. Calling main recursive is very bad style.

Your inner function should have a return value to signal whether to abort or run further.

Your startup code should also get an own function. So it look somehow like this:

int main()
{
  Init();//startup

  int loop = 1;
  
  while( loop )
  {
     loop = StartComputation();
  }

  return 0; 
}

BTW:

system("pause");

is also bad style (but I got no idea about a better way)


这篇关于如何在循环后重新开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆