问题描述
在这里写下您的问题。
任何人都可以告诉我如何在出现ERROR时输入R来重启程序吗?
Write your question here.
can anyone please tell me how to restart the program when i type R if an ERROR appeared ?
#include <conio.h>
#include <iostream>
using namespace std;
void restart() {
/////????
}
int main()
{
float fat, cal = -1, gram = -1 ,R ;
float per;
cout << "Enter the total number of calories" <<" in the food" << endl;
cin >> cal;
cout << "Enter the number of fat grams" <<" in the food" << endl;
cin >> gram;
fat = 9 * gram;
per = (fat / cal) * 100;
if (per > 100 || cal < 0 || fat < 0) {
cout << " \n ( ERROR ,Either the calories or fat grams were incorrect ! ) \n \t To Repeat write [R] then Enter : ";
cin >> R;
restart(); ///// problem _i need to restart the program here <~~~~~~~
}
if (per >= 30)
{
cout << "The percentage of calories" << " from the fat is " << per << "% \n";
}
else
{
cout << "The percentage of calories from" << " the fat is" << fat << "% \n";
cout << "The fat is low in the food";
}
system("pause");
return 0;
}
我的尝试:
void restart(){
///// ????
}
if(per> 100 || cal< 0 || fat< 0){
cout<< \ n(错误,卡路里或脂肪克不正确!)\ n \t重复写[R]然后输入:;
cin>> R;
restart(); /////问题_i需要在这里重启程序< ~~~~~~~
}
不知道是什么要做,我还是新尝试学习c ++
What I have tried:
void restart() {
/////????
}
if (per > 100 || cal < 0 || fat < 0) {
cout << " \n ( ERROR ,Either the calories or fat grams were incorrect ! ) \n \t To Repeat write [R] then Enter : ";
cin >> R;
restart(); ///// problem _i need to restart the program here <~~~~~~~
}
No idea what to do ,im still new on trying to learn c++
推荐答案
per = (fat / cal) * 100;
将抛出除零异常。
对于你的问题来说,写一个递归函数就可以了。
will throw divide by zero exception if the user inputs zero.
A for your problem, writing a recursive function will do the trick.
int main()
{
Calculate();
}
private void Calculate()
{
float fat, cal = -1, gram = -1;
char R;
float per;
cout << "Enter the total number of calories" <<" in the food" << endl;
cin >> cal;
cout << "Enter the number of fat grams" <<" in the food" << endl;
cin >> gram;
fat = 9 * gram;
if(cal!= 0)
{
per = (fat / cal) * 100;
}
if (per > 100 || cal < 0 || fat < 0)
{
cout << " \n ( ERROR ,Either the calories or fat grams were incorrect ! ) \n \t To Repeat write [R] then Enter : ";
cin >> R;
if(R == 'R' || R == 'r')
{
Calculate(); //Calls the method all over again if the user inputs R.
}
}
if (per >= 30)
{
cout << "The percentage of calories" << " from the fat is " << per << "% \n";
}
else
{
cout << "The percentage of calories from" << " the fat is" << fat << "% \n";
cout << "The fat is low in the food";
}
}
bool bRedo = false;
do
{
cout << "Enter the total number of calories" <<" in the food" << endl;
cin >> cal;
cout << "Enter the number of fat grams" <<" in the food" << endl;
cin >> gram;
fat = 9 * gram;
// Zero cal is an error too and would result in a division by zero
// Therefore it must be checked before performing the division
if (cal <= 0 || fat < 0 || fat / cal > 1)
{
cout << " \n ( ERROR ,Either the calories or fat grams were incorrect ! ) \n \t To Repeat write [R] then Enter : ";
string strRestart;
getline(cin, strRestart);
if (0 == strRestart.compare("R"))
bRedo = true;
else
return 1;
}
else
per = (fat / cal) * 100;
}
while (bRedo);
这篇关于如何重启程序C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!