control structures

Oct 17, 2011 at 1:12am
can someone please help; its telling me that case 2 not within a switch case?
how do i keep count of the code the user selected
for example how many students calculated under case 1


#include<iostream.h>
#include<conio.h>

main()
{
char name[25];
int code;
int credits;
int tuition,sum=0;
int student_count=0;
int PT_count=0;
int FT_count=0;


cout<<"Enter Student Name"<<endl;
cin>>name;
cout<<"Enter 1 if student is a resident or\n[ Enter 2 if a student is not a resident"<<endl;
cin>>code;
while (code!=0)
{
switch(code)
case 1:
cout<<"Enter the credit amount"<<endl;
cin>>credits;
if(credits>=12)
tuition=600;
else
tuition=credits*50;
break;
case 2:
cout<<"Enter the credit amount"<<endl;
cin>>credits;
if (credits>=12)
tuition=1320;
else
tuition=credits*110;
break;
default:
cout<<"Invalid Code"<<endl;


student_count=student_count+1;
PT_count=PT_count+1;
FT_count=FT_count+1;
sum=sum+tuition;

cout<<"Student credit amount is "<<credits;
cout<<"\nStudent tuition is $"<<tuition;
}
cout<<"\nTotal number of student is "<<student_count;

cout<<"\nTotal tuition amount is "<<sum;


getch();
}
Last edited on Oct 17, 2011 at 11:54pm
Oct 17, 2011 at 2:05am
Well first of all, you have a problem with the employees name. You're trying to put the whole name into a single char variable, but a char variable can only hold one letter. You can fix this by using a char array rather than just a single char. All you have to change is instead of
1
2
3
char name;
//and
void printvalues(char name,float net_pay)

put
1
2
3
char name[20];
//and
void printvalues(char name[],float net_pay)


Also, you are passing in some unnecessary parameters to your functions. For instance, rather than
1
2
3
4
5
float calc_tax(int pay, float tax)
{
    tax=pay*0.25;
    return tax;
}

It would be better to do
1
2
3
4
5
float calc_tax(int pay)
{
    float tax=pay*0.25;
    return tax;
}

Or, even better
1
2
3
4
float calc_tax(int pay)
{
    return pay*0.25;
}


While you don't HAVE to do it this way, the way you're doing it could cause some problems. For instance, you're passing tax to the calc_tax function before you assign any value to tax, which could cause some problems (it did with my compiler).
Last edited on Oct 17, 2011 at 2:06am
Oct 17, 2011 at 11:55pm
thanks alot
Oct 18, 2011 at 12:17am
closed account (zb0S216C)
Omitting the braces that surround the cases of a switch statement will mean that the switch can only handle a single case. Add braces around the case statements and you're good to go.

Please don't duplicate posts[1, Link].

References:
[1]http://www.cplusplus.com/forum/beginner/52730/


Wazzak
Last edited on Oct 18, 2011 at 12:19am
Topic archived. No new replies allowed.