programming assignment help!

Hey everyone,

I'm trying to write a program that asks a user to enter the number of cookies they bought and then it is supposed to tell the user how many candybars/candyballs they can get for free. It won't run and I don't even know where to start correcting errors. Can I get some pointers?(3 cookies> 1 candybars and <3 cookies =1 candy ball)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream>
using namespace std;
int main()
{

	int cookies, freecandybar,freecandyball;


	cout << "How many cookies did you purchase?";
	cin >> cookies;
	if (cookies > 3) freecandybar = 1;

	else if (cookies < 3) freecandyball = 1;


	cout << "Candy bars earned " << freecandybar;


	system("pause");

	return 0;
}
Your problem is your not initializing your variable. When your program runs cookies will get a value from cin but freecandybar and freecandyball are both in an if statement so they may or may not get a value and yet your out putting their value. to initialize them simply int cookies, freecandybar = 0 ,freecandyball = 0;

other pointers

- what if the user enters 3?

-how will you output it if they get a candy ball and not a bar?
Last edited on
Also, don't use system("pause")
It breaks you code's portability.
Topic archived. No new replies allowed.