#include <conio.h>
#include <iostream>
usingnamespace 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;
}
can anyone please tell me how to restart the program when i type R if an ERROR appeared ?
#include <iostream>
usingnamespace std;
int main()
{
start:
float cal { 0 };
float gram { 0 };
float fat { 0 };
float per { 0 };
char r { 0 };
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;
if (r == 'R')
{
system("cls");
goto start;
}
else
{
system("pause");
return 0;
}
}
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\n";
}
system("pause");
return 0;
}
// #include <conio.h> <-- really?
#include <iostream>
#include <limits>
void waitForEnter();
int main()
{
double per = 101.0, cal = -1.0, fat = -1.0;
while(per > 100 || cal < 0.0 || fat < 0.0) {
std::cout << "Enter the total number of calories in the food: ";
std::cin >> cal;
std::cout << "Enter the number of fat grams in the food: ";
double gram = 0.0;
std::cin >> gram;
fat = 9 * gram;
per = (fat / cal) * 100;
char R = '\0';
if (per > 100 || cal < 0 || fat < 0) {
std::cout << "\nERROR: either the calories or fat grams were ""incorrect!\n\tTo repeat write [R] then press Enter: ";
std::cin >> R;
if(R != 'R') { return 0; } // if the user doesn't enter 'R', exit program
}
}
if (per >= 30) {
std::cout << "The percentage of calories from the fat is " << per << "% \n";
} else {
std::cout << "The percentage of calories from the fat is" << fat
<< "%\nThe fat is low in the food";
}
// system("pause"); http://www.cplusplus.com/forum/beginner/1988/
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}