Problem with homework.

Hello,

I am new to the forum. I am also new to C++. I have a homework question that consist of a user entering a number of coupons that they have won and the output is candy bars and gumballs. The candy bars can be redeemed for 10 coupons and the gumballs can be redeemed for 3 coupons. The user prefers candy bars over gumballs so the gumball is the remainder of the total candy bars.

So this is what I have:

#include <iostream>
using namespace std;

int main(void)
{
int numberOfCandyBars = 1;
int numberOfGumballs = 0;
int numberOfCoupons = 0;

cout << "Please enter number of coupons won : " << endl;
cin >> numberOfCoupons;

if (numberOfCoupons == 10)

cout << "Total of candy bars is: " << numberOfCandyBars << endl;

else (numberOfCoupons <= 9)

numberOfCoupons % 3 = numberOfGumballs;

cout << "Total of gumballs: " << numberOfGumballs;

return 0;
}


I have probably totally butchered this simple problem but I need a better way for this to make sense. I know what I want to do. I just can't seem to put it in the right order. I may be over thinking this, but if someone could please help I would appreciate it. Thanks.
1
2
3
4
5
6
7
8
int numberOfCandyBars = 10;
int numberOfGumballs = 3;
int numberOfCoupons = 0;

cout << "Please enter number of coupons won : " << endl;
cin >> numberOfCoupons;
cout << "Total of candy bars is: " << numberOfCoupons/numberOfCandyBars << endl;
cout << "Total of gumballs: " << (numberOfCoupons%numberOfCandyBars)/numberOfGumballs << endl;
closed account (3hM2Nwbp)
Hi, in the future you may consider wrapping your code in [ code ] blocks, like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(void)
{
	int numberOfCandyBars = 1;
	int numberOfGumballs = 0;
	int numberOfCoupons = 0;
	cout << "Please enter number of coupons won : " << endl;
	cin >> numberOfCoupons;
	if (numberOfCoupons == 10)
		cout << "Total of candy bars is: " << numberOfCandyBars << endl;
	else if(numberOfCoupons <= 9) //You needed to add in an 'if' on this line to make it 'else if(condition)'
		numberOfGumballs = numberOfCoupons % 3; //You had your LValue and RValues mixed up on this line
//This code only works if there are between 0 and 10 coupons
	cout << "Total of gumballs: " << numberOfGumballs;
	return 0;
}


Let me know if you have any questions.

Best regards,
Luc
Last edited on
Thanks guys I tested the program with the input of 20 coupons and 21. I got an output of 0. The code did run without any errors but I need the calculation to output a number of candy bars when 10 coupons are redeemed and if their is a remainder it outputs the gumballs also. Gumballs are redeemed at 3 coupons. I guess my main problem is the math. So my if statement is wrong I am guessing.
closed account (3hM2Nwbp)
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
#include <iostream>
using namespace std;

int main(void)
{
	int numberOfCandyBars, numberOfGumballs, numberOfCoupons;

	cout << "Please enter number of coupons won : " << endl;
	cin >> numberOfCoupons;
	/*
	A) Find the number of candy bars
		Coupons divided by 10 is the number of candy bars
		Now we have the number of candy bars - so we know how many coupons were used.
		We can subtract the number of coupons used from the total to find out how many coupons are left
	B) Find the number of gumballs
		After we subtract the number of coupons used from the total, we can find the number of gumballs
		Coupons divided by 3 is the number of gumballs
		Now we know the number of gumballs - so we know how many coupons were used.
		We can subtract the number of coupons used from the total to find out how many coupons are left
	C) Leftover Coupons
		After we find the # of candy bars and # of gumballs, we have the # of leftover coupons

		Now it's up to you to translate that into C++ :)
	*/
	return 0;
}


There is more than one way to solve this problem, this is just another way of looking at it.

Best regards,
Luc
Last edited on
Thank you :-)
Topic archived. No new replies allowed.