如何重启程序C ++ [英] How to restart the program C++

查看:97
本文介绍了如何重启程序C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里写下您的问题。

任何人都可以告诉我如何在出现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++

推荐答案

你好,



你需要了解在尝试编写甚至正确编译的代码之前,语言的基础知识。



据我所知,如果用户需要再调用cal和gram的方法按下'R'键。我建议编写一个不同的方法,然后在main函数中调用它。



我注意到的一些问题R不应该是一个浮点变量将它定义为字符串或字符。如果用户输入零,那么行
Hello,

You need to understand the basics of the language before trying to write code that even compiles correctly.

From what I understand you need to call the method that takes cal and gram again if the user presses the key 'R'. I would suggest writing a different method that does it and call it in the main function.

Some problems I noticed R should not be a floating point variable define it as a string or char. Also the line
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"; 
   }
}


无需重启应用程序。只需使用while循环:

There is no need to restart the application. Just use a while loop:
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); 


非常感谢! ,感谢你的帮助。
Thanks a lot guys ! ,appreciate your help all .


这篇关于如何重启程序C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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