How to restart my program ,Help .

Write your question here.
can anyone please tell me how to restart the program when i type R if an ERROR appeared ?

the problem is on these lines

38
39
40


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
#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;

}



can anyone please tell me how to restart the program when i type R if an ERROR appeared ?

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

using namespace 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;
}
Last edited on
In case you don’t appreciate ‘goto’:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// #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');
}

Topic archived. No new replies allowed.