labHelp_Beginner

Hey guys my name is Coty i am a brand new programmer taking a c++ class in college and i am having a problem with this lab not putting out a 0% when i input 0's in for the variables. I tried to talk to a programmer friend about it but his explanation wasn't great since he couldn't see the program or anything while we were at work. If you guys could suggest anything that would be great.

The check my teacher wants us to runs is 150 then 6 and you should get out 54 and 36%.But when i check with 0's i get a o and -1%

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
 //Lab5-2.cpp - displays a food's fat
//calories and fat percentage
//Created/revised by Coty on 10/23/2014

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//declare variables
	int totalCals    =0;
	int fatGrams     =0;
	int fatCals      =0;
	double fatPercent=0.0;

	//enter input items
	cout << "Total calories: ";
	cin >> totalCals;
	cout << "Grams of fat: ";
	cin >> fatGrams;

	//determain whether the data is valid
	if (totalCals >= 0 && fatGrams >= 0)
	{
		//calculate and display the output
		fatCals = fatGrams * 9;
		fatPercent = static_cast<double>(fatCals)
			/ (totalCals) * 100;

		cout << "Fat calories: " << fatCals << endl;
		cout << fixed << setprecision(0);
		cout << "Fat percentage: " << fatPercent
			 << "%" << endl;
	}
	else
		cout << "Input error" << endl;
	//end if

	system("pause");
	return 0;
  } //end of main function 
Last edited on
One of the problems you will have if all your inputs are zero is divide by zero errors resulting in a program crash (exception). Perhaps you should think about insuring your entries are greater than zero.

I had tried that but i am not sure if she will take of points for the program not displaying 0's
You could also put a condition on the calculation if you need to allow zeroes to be entered. If the denominator is 0, don't do the calculation which would result in error, but set fatPercent to 0.
I know this might not look so pretty but i got the program to do what i needed,so looks like i got this one solved.

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
//Lab5-2.cpp - displays a food's fat
//calories and fat percentage
//Created/revised by Coty Peeler on 10/23/2014

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//declare variables
	int totalCals    =0;
	int fatGrams     =0;
	int fatCals      =0;
	double fatPercent=0.0;

	//enter input items
	cout << "Total calories: ";
	cin >> totalCals;
	cout << "Grams of fat: ";
	cin >> fatGrams;

	//determain whether the data is valid
	if (totalCals > 0 && fatGrams > 0)
	{
		//calculate and display the output
		fatCals = fatGrams * 9;
		fatPercent = static_cast<double>(fatCals)
			/ static_cast<double>(totalCals) * 100;

		cout << "Fat calories: " << fatCals << endl;
		cout << fixed << setprecision(0);
		cout << "Fat percentage: " << fatPercent
			 << "%" << endl;
	}
	else
		// If to display input error for negatives
		if ( totalCals < 0 || fatGrams < 0 )
		{
		cout << "Input error" << endl;
		}
	//end if
        
		// If to display with 0's
		if ( totalCals == 0 && fatGrams == 0)
		{
		cout << "Fat calories: " << 0 << endl;
		cout << fixed << setprecision(0);
		cout << "Fat percentage: "<< 0 << '%'<< endl;
		}
	//end if
		
	system("pause");
	return 0;
  } //end of main function 
Topic archived. No new replies allowed.