Am I doing this right?

Boy scout chocolate sales. The output should be arranged depending on the amount of chocolates sold. Example; the lowest amount should always be at third place. Getting a lot of errors.

Need the output to be a table. Should look something like this:


Rank Sales Commission

Third $43.75 $2.18

Second $75.00 $5.25

First $156.25 $15.63


List of errors are as follows:

Error 1 error C2059: syntax error : '<' f:\cois 270\projects\examen final\examen final\examenf.cpp 37 Examen Final

Error 2 error C2143: syntax error : missing ';' before '{' f:\cois 270\projects\examen final\examen final\examenf.cpp 38 Examen Final

Error 3 error C2143: syntax error : missing ';' before '==' f:\cois 270\projects\examen final\examen final\examenf.cpp 39 Examen Final

Error 4 error C2059: syntax error : '<' f:\cois 270\projects\examen final\examen final\examenf.cpp 41 Examen Final

Error 5 error C2143: syntax error : missing ';' before '{' f:\cois 270\projects\examen final\examen final\examenf.cpp 42 Examen Final

Error 6 error C2143: syntax error : missing ';' before '==' f:\cois 270\projects\examen final\examen final\examenf.cpp 43 Examen Final

Error 7 error C2059: syntax error : '<' f:\cois 270\projects\examen final\examen final\examenf.cpp 45 Examen Final

Error 8 error C2143: syntax error : missing ';' before '{' f:\cois 270\projects\examen final\examen final\examenf.cpp 46 Examen Final

Error 9 error C2143: syntax error : missing ';' before '==' f:\cois 270\projects\examen final\examen final\examenf.cpp 47 Examen Final

Error 10 error C2450: switch expression of type 'std::string' is illegal f:\cois 270\projects\examen final\examen final\examenf.cpp 55 Examen Final

Error 11 error C2051: case expression not constant f:\cois 270\projects\examen final\examen final\examenf.cpp 57 Examen Final

Error 12 error C2051: case expression not constant f:\cois 270\projects\examen final\examen final\examenf.cpp 60 Examen Final

Error 13 error C2051: case expression not constant f:\cois 270\projects\examen final\examen final\examenf.cpp 63 Examen Final

Warning 14 warning C4060: switch statement contains no 'case' or 'default' labels f:\cois 270\projects\examen final\examen final\examenf.cpp 66 Examen Final


Here's the code:

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
#include <iostream>
#include <iomanip>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::setprecision;
using std::ios;
using std::setiosflags;

int main()

{
		int chocolates            =  0;
		double sales              =  0;
		double commission          =  0;
		string rank               = "";
		

		cout << "Enter the first amount of chocolates: ";
		cin >> chocolates;
		cout << "Enter the second amount of chocolates: ";
		cin >> chocolates;
		cout << "Enter the third amount of chocolates: ";
		cin >> chocolates;
		cout << endl;

		sales = chocolates * 1.25;


		if (sales =< 50)
		 {
			 string rank == "Third";
		 }
		if (sales =< 100)
		   {
			 string rank == "Second";
		   }
		if (sales =< 150)
		   {
			 string rank == "First";
	           }

			
			cout << setiosflags(ios::fixed) << setprecision(2);

			cout << "      Rank" << "    Sales" << "   Commission";
		
			switch (rank) 
			{
			case "Third": commission = sales * .05;
				                 cout << "        " << sales << "       " << commission << endl;
								 break;
			case "Second": commission = sales * .07;
				                 cout << "        " << sales << "       " << commission << endl;
								 break;
			case "First": commission = sales * .010;
				                 cout << "        " << sales << "       " << commission << endl;
								 break;
			}

            return 0;

}


Any advice or criticism is welcome. I'm fairly stumped on this one.
Last edited on
Well, there are a couple obvious problems I can see:

1. You cin chocolates three times but don't instantiate it to a new variable. Effectively you just have the user input three numbers and the third number is the only one stored.
2. You should swap your =< to <= if you mean less than or equal to
3. Don't put extra spaces on << operators, it just makes it hard to read
4. You're using a "double equals" when you should be using an equals on your string variables, and the string variables are only being instantiated in the if statement - you need to declare them at the top.

Try that and then see what you get. :)
closed account (3pj6b7Xj)
You made a booboo,

string rank == "Third";

Perhaps you meant string rank = "Third";

== is used for comparisons = alone is used to asign a value to something.

I also belive you can't initialize a string this way,

string rank = "";

the string class implementation has a constructor that automatically empties the string objet, there is no need to add "" just,

string rank;

also you can shortern your code...

if (sales =< 50)
{
string rank == "Third";
}
if (sales =< 100)
{
string rank == "Second";
}
if (sales =< 150)
{
string rank == "First";
}


to...

1
2
3
if (sales =< 50) string rank = "Third";
if (sales =< 100) string rank = "Second";
if (sales =< 150) string rank = "First";


By the way these if statements cancel each other out, the second condition; sales =< 100 will be overwritten by the first and the third sales =< 150 will be overwritten by the rest, unless that is what you are aiming for, it seems rather confusing.

Also, you have a syntax error in...

1
2
3
4
case "Third": commission = sales * .05;
				                 cout << "        " << sales << "       " << commission << endl;
								 break;


You can't use switch statements like that, you have to correct it to this...

1
2
3
4
5
6
case "Third": 
{
     commission = sales * .05;
     cout << "        " << sales << "       " << commission;
     break;
}


Lastly, you cannot use switch with strings, as far as I know...that is why I stay away from switch most of the time, I would have done this instead...

1
2
3
if (rank == "First") { commission = sales * .010; cout << "        " << sales << "       " << commission; }
if (rank == "Second") { commission = sales * .07; cout << "        " << sales << "       " << commission; }
if (rank == "Third") { commission = sales * .05; cout << "        " << sales << "       " << commission; }


I also see repetitive code, my programming habits instruct me to create a function that will display the values of sales and commission....

1
2
3
4
5
void ShowData(double compute, double sales, double commission)
{
     commission = sales * compute;
     cout << "        " << sales << "       " << commission;
}


Then the previous code woould simply look like this...

1
2
3
if (rank == "First")  ShowData(.010, commission, sales); 
if (rank == "Second") ShowData(.07, commission, sales);
if (rank == "Third") ShowData(.05, commission, sales);


I hope I didn't critic too much and that this was all very helpfull to you, hopefully someone else can also help.
Last edited on
"By the way these if statements cancel each other out, the second condition; sales =< 100 will be overwritten by the first and the third sales =< 150 will be overwritten by the rest, unless that is what you are aiming for, it seems rather confusing." - mrfaosfx.

I seem to remember another program I made where I had 3 if statements in a row much like this one and it worked so I'm kinda confused.


Also, is there another way this can be done any other way without using switch statements?

I want the results to place themselves onto the proper spaces in the table. Otherwise I get the lowest amount of chocolates sold will end up in the "First place" section on the table, and I don't want that. I hope you see what I'm getting at.

Finally, based on what I have now. Is it even possible to carry out what I'm trying to make the program do?
Topic archived. No new replies allowed.