Welcome! Log In Create A New Profile

Advanced

Switch statement

Posted by casper001 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Switch statement
March 20, 2011 08:20PM
I am busy with question 2 of Assignment 2 and am running into problems with the switch statement. Any advice will be appreciated. Here is a copy of my switch statement, it looks 100% right to me:

//begin switch statement

switch (numSalesmen)
{
case 1 : cout << "Enter total sales for Grade A:" << endl;
commission = gradeACommission;
break;
case 2 : cout << "Enter total sales for Grade B:" << endl;
commission = gradeBCommission;
break;
case 3 : cout << "Enter total sales for Grade C:" << endl;
commission = gradeCCommission;
break;
case 4 : cout << "Enter total sales for Grade D:" << endl;
commission = gradeDCommission;
break;
case 5 : cout << "Enter total sales for Grade E:" << endl;
commission = gradeECommission;
break;

default:
cout << "Wrong grade!!!";

}

//end switch statement
avatar Re: Switch statement
March 20, 2011 08:44PM
You forgot to mention what problems you're experiencing. The statement looks fine to me on it's own but nobody can help you unless you post the error messages.
Re: Switch statement
March 20, 2011 09:03PM
Thanks for the reply.

LOL.

Let me try. The program compiles fine and there are no error messages.

After compiling, the programs runs and runs just repeating the following information before coming to a stop:

Enter the staff number for salesman 1001 : Enter the medical aid contribution for staff number 1002 : Enter total sales for Grade A:
Enter total sales for Grade B:
Enter total sales for Grade C:
Enter total sales for Grade D:
Enter total sales for Grade E:
Enter the staff number for salesman 1002 : Enter the medical aid contribution for staff number 1002 : Enter total sales for Grade A:
Enter total sales for Grade B:
Enter total sales for Grade C:
Enter total sales for Grade D:
Enter total sales for Grade E:
Press any key to continue . . .



Here is an extract from part of my code:

#include <iostream>
using namespace std;
int main()
{
int numSalesmen;
int staffNumber;
float commission;
float tax;
float netSalary;
float medicalAid;
float gradeSales;
float totalSales = 0;
float grossSalary = 0;
const float superIncomeTaxRate = 0.5;
const float highIncomeTaxRate = 0.4;
const float midIncomeTaxRate = 0.3;
const float lowIncomeTaxRate = 0.2;
const float lowestIncomeTaxRate = 0.1;
const float gradeACommission = 0.2;
const float gradeBCommission = 0.18;
const float gradeCCommission = 0.15;
const float gradeDCommission = 0.1;
const float gradeECommission = 0.05;

cout << "Enter the number of salesmen for Company ABC: ";//***prompt for number of salesman
cin >> numSalesmen;
//for loop iterate over number of salesmen
for(int numSalesmen = 1; numSalesmen <= staffNumber; numSalesmen++)
{
cout<< "Enter the staff number for salesman " << numSalesmen << " : ";
cin >> staffNumber;
//validate staff number
while (staffNumber >= 1500 && staffNumber <= 9999)
{
cout<< "Enter the Staff number " << staffNumber << " : ";//***prompt for staff number
cin >> staffNumber;
}
cout<< "Enter the medical aid contribution for staff number " << staffNumber << " : ";
cin >> medicalAid;
//for loop iterating over the grades
for (int numSalesmen = 1; numSalesmen <= 5; numSalesmen++)
{

//begin switch statement

switch (numSalesmen)
{
case 1 : cout << "Enter total sales for Grade A:" << endl;
commission = gradeACommission;
break;
case 2 : cout << "Enter total sales for Grade B:" << endl;
commission = gradeBCommission;
break;
case 3 : cout << "Enter total sales for Grade C:" << endl;
commission = gradeCCommission;
break;
case 4 : cout << "Enter total sales for Grade D:" << endl;
commission = gradeDCommission;
break;
case 5 : cout << "Enter total sales for Grade E:" << endl;
commission = gradeECommission;
break;

default:
cout << "Wrong grade!!!";

}

//end switch statement
}
}
return 0;
}


avatar Re: Switch statement
March 20, 2011 10:41PM
Language: C++
cout << "Enter the number of salesmen for Company ABC: ";//***prompt for number of salesman cin >> numSalesmen; //for loop iterate over number of salesmen for(int numSalesmen = 1; numSalesmen <= staffNumber; numSalesmen++)

For starters, I think you'll have a bit of a problem with this?

Having pointed out the possible problem, I'll switch over to cryptic mode. Stand back a few paces, consider what you're doing at each step, and see if you can see it for yourself. (Don't worry. If this turns out to be one of those things that blind each and every one of us mortals, simply by being so obvious that they become invisible, I hereby undertake to be less cryptic if requested to be so on eg. Tuesday.)
avatar Re: Switch statement
March 20, 2011 10:44PM
Oh one other thing. Look at the line directly above the edit box. There are buttons for things like bold and italics etc. just like a word processor. Hover over the boxy thing to the right of the quotation mark. That gives you the tags I used to make your code so pretty in the previous mysterious whisper. Nice toy. Maybe y'all should play with it?
Re: Switch statement
March 21, 2011 04:27PM
Hi slow_eddy

Thank you for the response.

Yes, I can see that part of the problem most probably lies with "numSalesmen <= staffNumber" and I changed it to for (int numSalesmen = 1; numSalesmen <= 5; numSalesmen++).

The program compiles fine, but I still think there is a problem with the switch statement:



switch (numSalesmen)
{
case 1 : cout << "Enter total sales for Grade A:" << endl;
commission = gradeACommission;
break;
case 2 : cout << "Enter total sales for Grade B:" << endl;
commission = gradeBCommission;
break;
case 3 : cout << "Enter total sales for Grade C:" << endl;
commission = gradeCCommission;
break;
case 4 : cout << "Enter total sales for Grade D:" << endl;
commission = gradeDCommission;
break;
case 5 : cout << "Enter total sales for Grade E:" << endl;
commission = gradeECommission;
break;

}

//end switch statement



After compiling, I get the result:
Enter the number of salesmen for Company ABC: 2
Enter the staff number for salesman 1 : 1002
Enter the medical aid contribution for staff number 1002 : R200.00
Enter total sales for Grade A:
Enter total sales for Grade B:
Enter total sales for Grade C:
Enter total sales for Grade D:
Enter total sales for Grade E:
Enter the staff number for salesman 2 : Enter the medical aid contribution for s
taff number 1002 : Enter total sales for Grade A:
Enter total sales for Grade B:
Enter total sales for Grade C:
Enter total sales for Grade D:
Enter total sales for Grade E:
Enter the staff number for salesman 3 : Enter the medical aid contribution for s
taff number 1002 : Enter total sales for Grade A:
Enter total sales for Grade B:
Enter total sales for Grade C:
Enter total sales for Grade D:
Enter total sales for Grade E:
Enter the staff number for salesman 4 : Enter the medical aid contribution for s
taff number 1002 : Enter total sales for Grade A:
Enter total sales for Grade B:
Enter total sales for Grade C:
Enter total sales for Grade D:
Enter total sales for Grade E:
Enter the staff number for salesman 5 : Enter the medical aid contribution for s
taff number 1002 : Enter total sales for Grade A:
Enter total sales for Grade B:
Enter total sales for Grade C:
Enter total sales for Grade D:
Enter total sales for Grade E:
Press any key to continue . . .

I say there is a problem with the switch statement, because cin >> gradeSales; is not happening!

Any clue?
avatar Re: Switch statement
March 21, 2011 04:42PM
Er, I think I'll just be lazy and stick with the problem I think you have, above, and leave someone else your switch problem for the time being.

When you do the cin, you're assigning some value, right?

OK.

Now what are you doing with the first "parameter" (yelp! yelp! Right name for that, please, somebody ...) of your for loop? Think about that. What does each "line of code" at the start of the for loop do? Go through them one at a time. Only go on when you're clear about the one you're now on.

In other words the first question you need to ask yourself is "What does int numSalesmen = 1 mean?"

PS. Looks like you didn't find that button that gives you the box for the beautified code. Count four buttons left from the smiley?
avatar Re: Switch statement
March 21, 2011 07:56PM
Here's a copy of the code you pasted above, formatted and with line numbers turned on:
Language: C++
  1. include <iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5. int numSalesmen;
  6. int staffNumber;
  7. float commission;
  8. float tax;
  9. float netSalary;
  10. float medicalAid;
  11. float gradeSales;
  12. float totalSales = 0;
  13. float grossSalary = 0;
  14. const float superIncomeTaxRate = 0.5;
  15. const float highIncomeTaxRate = 0.4;
  16. const float midIncomeTaxRate = 0.3;
  17. const float lowIncomeTaxRate = 0.2;
  18. const float lowestIncomeTaxRate = 0.1;
  19. const float gradeACommission = 0.2;
  20. const float gradeBCommission = 0.18;
  21. const float gradeCCommission = 0.15;
  22. const float gradeDCommission = 0.1;
  23. const float gradeECommission = 0.05;
  24.  
  25. cout << "Enter the number of salesmen for Company ABC: ";//***prompt for number of salesman
  26. cin >> numSalesmen;
  27. //for loop iterate over number of salesmen
  28. for(int numSalesmen = 1; numSalesmen <= staffNumber; numSalesmen++){
  29. cout<< "Enter the staff number for salesman " << numSalesmen << " : ";
  30. cin >> staffNumber;
  31. //validate staff number
  32. while (staffNumber >= 1500 && staffNumber <= 9999){
  33. cout<< "Enter the Staff number " << staffNumber << " : ";//***prompt for staff number
  34. cin >> staffNumber;
  35. }
  36. cout<< "Enter the medical aid contribution for staff number " << staffNumber << " : ";
  37. cin >> medicalAid;
  38. //for loop iterating over the grades
  39. for (int numSalesmen = 1; numSalesmen <= 5; numSalesmen++){
  40.  
  41. //begin switch statement
  42.  
  43. switch (numSalesmen){
  44. case 1 : cout << "Enter total sales for Grade A:" << endl;
  45. commission = gradeACommission;
  46. break;
  47. case 2 : cout << "Enter total sales for Grade B:" << endl;
  48. commission = gradeBCommission;
  49. break;
  50. case 3 : cout << "Enter total sales for Grade C:" << endl;
  51. commission = gradeCCommission;
  52. break;
  53. case 4 : cout << "Enter total sales for Grade D:" << endl;
  54. commission = gradeDCommission;
  55. break;
  56. case 5 : cout << "Enter total sales for Grade E:" << endl;
  57. commission = gradeECommission;
  58. break;
  59.  
  60. default:
  61. cout << "Wrong grade!!!";
  62.  
  63. }
  64.  
  65. //end switch statement
  66. }
  67. }
  68. return 0;
  69. }

The problem you mentioned :
Quote
casper001
I say there is a problem with the switch statement, because cin >> gradeSales; is not happening!

Where exactly is that statement? You didn't include it in the code you pasted.

In the same line as Eddy, what exactly are you using numSalesmen for? You seem to be using it for at least three separate reasons.
1) You define one version on line 1 and initialise it on line 26.
2) On line 28, you create a second version with the same name for some unknown purpose. I don't understand what you're trying to do with this one.
3) On line 39, you create a third version with the same name for yet again, another unknown purpose.
4) On line 43, you use the one created on line 39 for yet again, an unknown purpose.

It looks to me like you haven't quite gotten the problem solved in your head before translating it to code. This is truely the hardest part of learning to program. Learning the syntax of any programming language is the easy part. Breaking the problem up and solving it in pieces is the hard part.

Not being registered for this module and thus not having the assignment questions, I'm just going to guess what you need to do.

I'm think what you are required to do is to get a the following information from a variable number of salespeople:
Employee code, medical contribution, sales total, calculate commission based on sales and then keep a running total for all salespeople.

I assume the loop you start on line 28 is designed to loop through each of the salespeople you discovered in line 26. Why did you choose to define your looping variable with the same name? Are you aware that, because the for loop creates a new local scope that it creates a new local variable that in fact hides the one in the outer scope? In essence, you have two variable with the same name but you can only access the one from closest scope (ie the one from line 28).

If the purpose of the loop is to iterate through all the salespeople, why are you making termination condition dependent on the staff number? how will the staff number tell you when to end the loop? In fact, there's no way to tell, initially how many times the loop will run. This is because you define the staffNumber on line 6 without initialising it, and then use it as a terminating condition in line 26. Since it's not initialised, it could contain any value. If it had a negative value, your loop wouldn't even execute once. You then proceed to get a number from the user and then range-bind it to a value from 1500 to 9999. This means that your loop will run at least 1500 times and at most 9999 times.

In your while loop (lines 32 through 35), you attempt to make sure that the user has input a value from 1500 to 9999. By forcing the repitition, you imply that any number that is not in this range is invalid. Why then do you quote the number back to the user in line 33? You can replace the code from 29 through 35 in a single do{ ... } while () loop. If you don't understand why I've suggested this, go back to your notes and read up the differences between the two forms of the while loop.

What you're think you're trying to do in the third loop (lines 39 through 66) is not what's actually happening. I'm assuming that this is where you hope to calculate the commission for each salesperson. This is far from what you're actually doing. Try rethinking about this and only this. How would you actually calculate the commission for one person?
avatar Re: Switch statement
March 21, 2011 09:16PM
@Eddy

What you're referring to as a parameter in the for loop is not a parameter at all. The for loop is a bit of a funny statement as far as c++ goes. The parenthesised part has three components, all of which are surprisingly optional (yes, for ( ; ; ) is allowed)[1]

The first part is the initialisation part, this is executed only once at the beginning of the loop. There is no real limitation on what you can put in here but conventionally, you would initialise one or more variables(separated by a comma) here to control the loop.

The second part is executed after the first part and before each loop iteration and can also have any code you wish that will return a boolean value but is conventionally used to determine when the loop should terminate. Like the first part, you can separate multiple statements by the comma operator but only the boolean result of the first statement is used. Note that it is extremely rare to find more than one statement useful in the second part of a for statement. If you find yourself needing to use a second or third statement, there's usually a better way of solving your problem.

The third part is executed at the end of each iteration and can also have any code in it but usually manipulated control variables defined in the first part. Like the first two parts, multiple statements can be separated by the comma operator. This is occasionally useful if you need to update more than one variable in each iteration but again, if you find yourself needing to do this, try designing a simpler loop.

An important thing to note is that the for loop defines a new local scope and any variables defined in the first part are destroyed once the loop terminates.

It's common to have something along the lines of

Language: C++
for (int i = 0; i < 15; ++i){ // some code }

but I quite often use the following when iterating through lists of a fixed size:
Language: C++
for (int i = 0, count = list.size(); i < count; ++i){ // some code }
I do this because the common alternative would be
Language: C++
for(int i = 0; i < list.size(); ++i){ // some code }
but since I'm not expecting the list's size to change throughout the iterations, and the second part gets executed before each iteration, I can cut down on function calls by storing the list's size in a temporary variable.

To give you a better visualisation of how a for loop works, the second for loop above is identical to the code from lines 5 through 14 of the following:

Language: C++
  1. int main(int argc, char **argv){
  2.  
  3. std::vector<int> list;
  4.  
  5. {
  6. int i = 0, count = list.size();
  7. while (i < count) {
  8.  
  9. // Some code
  10.  
  11. ++i;
  12. }
  13.  
  14. }
  15.  
  16. return 0;
  17. }

Lines:
3) External list of some size. This is just for this example. Don't worry too much about what a vector is at this point. Just note that it's a container of other variables
5) Creates new local scope, any variables defined inside this scope only live inside the scope and are destroyed when execution leaves the scope
6) This is the same as the first part of the for loop
7) This is the same as the second part of the for loop
11) This is the same as the the third part of the for loop
14) End of local scope. i and count are destroyed here


[1]
Getting back to the for( ; ; ) statement. As I said, this is allowed since all of the parts are optional. A missing second part implies true thus the empty second part implies an infinite loop. The only way out of an infinite for loop is by using the break statement.
avatar Re: Switch statement
March 22, 2011 12:35PM
Thanks Rob! Very informative.

I now have a different "picture" of a for loop. It would be based on the meaning of ";" in C++:

for( each n, maybe something else;
while a < n < b is true; // Giving statements their own lines just like elsewhere.
make n step along)

Obviously that's a much cruder picture than you've provided. All it does is save me from the error of seeing "parameters" where there's no function.

(Naturally, I now move all those statement lines to the correct places you've placed them in, so the short little mnemonic of mine makes heaps more sense than it would appear to at face value.)

I'll remember the trick of multiple initialisations in the initialisation section. Very nifty!

@ casper100. I think it may help to answer the question: "What is the effect of the statement numSalesmen ++ ?"

Let's hear if any of this helped, please.
Re: Switch statement
March 26, 2011 12:47PM
Thank you all for the replies. I finally have time today to look into this assignment again. Will post (hopefully positive) comments soon.
Sorry, only registered users may post in this forum.

Click here to login