Issue with program

So I'm learning about if and else structures right now and for some reason my program that I'm working on won't accept one of my strings whenever I try to use a cout statement. It's the cout statement "cout << "How many scoops would you like?" << endl;" cin >> scoops;cout << "Scoops: " << endl;" and I get an error under the << before I state flavor.
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
  #include <iostream>
#include <iomanip>

using namespace std;

int main() {
	int scoops, lollypops, gumdrops, blah;
	double totalCharge, lCost, gCost, accIceCreamCost, fullLollyCost, fullGumCost;
	char choice = ' ';
	double iceCreamCost = 0.0;
	string flavor, Vanilla, Strawberry, Chocolate;

	cout << fixed << showpoint << setprecision(2);

	cout << "Welcome to the  Ice Cream and Candy Store!"
		<< endl;
	cout << "Welcome to the Ice Cream and Candy Store! Chocolate ice cream is"
		"\n$3.05, Strawberry is $3.15, and Vanilla is $2.86.What flavor would"
		"\nyou like : (C)hocolate, (S)trawberry, or (V)anilla : V " << endl;

	cin >> choice;

	if ('V' == choice)
		flavor = Vanilla;
	iceCreamCost = 2.86;
	if ('C' == choice)
		flavor = Chocolate;
	iceCreamCost = 3.15;
	if ('S' == choice)
		flavor = Strawberry;
	iceCreamCost = 3.05;
	cout << "How many scoops would you like?" << endl;
	cin >> scoops;
	cout << "Scoops: " << endl;

	cout << "Lollypops are $0.79. How many lollypops would you like? " << endl;
	cin >> lollypops;
	cout << "Gumdrops are $0.65.How many packages would you like?" << endl;
	cin >> gumdrops;

	if (lollypops > 0)
		lCost = 0.79;
	if (gumdrops > 0)
		gCost = 0.65;

	fullLollyCost = lCost * lollypops;
	fullGumCost = gCost * gumdrops;
	accIceCreamCost = iceCreamCost * scoops;
	totalCharge = lollypops * lCost, gumdrops * gCost;
	totalCharge = totalCharge + (iceCreamCost * scoops);


	cout << "Thank you for your order: " << endl;
	cout << scoops << " scoops of " << flavor << "at " << iceCreamCost;
	cout << "charges you: " << accIceCreamCost << endl;

	if (lollypops > 0)
		cout << lollypops << " at, " << lCost << " charge: " << fullLollyCost << endl;
	if (gumdrops > 0)
		cout << gumdrops << " at, " << gCost << " charge: " << fullGumCost << endl;
	cout << "Total Charge: $" << totalCharge;


	cin >> blah;



	return 0;


}
It's the cout statement "cout << "How many scoops would you like?" << endl;" cin >> scoops;cout << "Scoops: " << endl;" and I get an error under the << before I state flavor.


You mean this bit?

1
2
3
cout << "How many scoops would you like?" << endl;
	cin >> scoops;
	cout << "Scoops: " << endl;


I get an error under the << before I state flavor.

That code doesn't state flavor anywhere.
Last edited on
<< flavor << "at " << iceCreamCost;
cout << "charges you: " << accIceCreamCost << endl; this one my bad
That code compiles. What error are you talking about? What does it do that you don't like?
Never mind I got it I realized I didn't include <string> into my program and that's what gave me the error that I got, it runs now though. Thanks!
The only other issue I have with this is whenever I run the program, this statement "cout << "Thank you for your order: " << endl;
cout << scoops << " scoops of " << flavor << "at " << iceCreamCost;
cout << "charges you: " << accIceCreamCost << endl;" it doesn't select my assigned string variable from the first if statements, if that makes sense. So if I were to run the code, and selected "V" for vanilla, how can I put the string "Vanilla" into that statement above when it runs. Or do I have to use static_cast<char> so that it takes "Vanilla" and puts it into the output statement.
You created a string named Vanilla. But you never gave it a value. It's an empty string.


Maybe showing you with an int would help.

1
2
int five; // no value set
five = 5;  // NOW five has a value set 


Just naming a variable doesn't give it a value.

string Vanilla; //an empty string

string Vanilla = "Vanilla"; //a string with an actual value.
Hello blahblah123,

You should initialize all your variables when you define them. I had a problem with "lCost" not being initialized. Moschops covered my point on the variables "Vanilla, Strawberry, Chocolate".

The other problem I noticed is:

1
2
3
4
5
6
7
8
9
if ('V' == choice)
		flavor = Vanilla;
	iceCreamCost = 2.86;
	if ('C' == choice)
		flavor = Chocolate;
	iceCreamCost = 3.15;
	if ('S' == choice)
		flavor = Strawberry;
	iceCreamCost = 3.05;


First what happens if choice is a lowercase letter? The if statements will not work. You could change the if statements to include a lowercase letter or before the if statements add the code choice = toupper(choice);. The next problem is that "flavor =" is part of the if statement, but "iceCreamCost =" is not part of the if statement. Those two line need to be inside a set of "{}" braces. Or another choice for that code could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	cin >> choice;
	choice = toupper(choice);

	if ('V' == choice)
	{
		flavor = Vanilla;
		iceCreamCost = 2.86;
	}
	else if ('C' == choice)
	{
		flavor = Chocolate;
		iceCreamCost = 3.15;
	}
	else if ('S' == choice)
	{
		flavor = Strawberry;
		iceCreamCost = 3.05;
	}
	else
	{
		std::cout << "\n Invalid choice." << std::endl;
	}


Although with this code a do/while loop would be useful to get the right input.

After that I did some minor changes to make the final output look better.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.