hey .. please i need help with this code when i write it and i start in case 3 .. i've error massage .. illegal case .. why is that ?

[LIST=1]
[*]#include <iostream>
[*]#include <string>
[*]using namespace std ;
[*]class employee
[*]{
[*]private:
[*]	string name;
[*]	int salary;
[*]	char degree;
[*]	int id;
[*]	long nr;
[*]public:
[*]	employee(){
[*]		name=" ";
[*]                                 degree=' ';
[*]		salary=0;
[*]		id=0;
[*]		nr=0;}

[*]	void AddNewEmployee(string n,int s,char d,int id1,long nr1){
[*]	  name=n;
[*]	  salary=s;
[*]	  degree=d;
[*]	  id=id1;
[*]	  nr=nr1;}

[*]	string getname(){ return name;}
[*]	char getde(){ return degree;}
[*]	int getsa(){ return salary;}
[*]	int getidnu(){ return id;}
[*]	long getnrnum(){ return nr;}

[*]};
[*]int main(){  
[*]string n;
[*]char d;
[*]int s;
[*]int id1;
[*]long nr1;
[*]int choise;
[*]const int size = 1;
[*]employee em[size];
[*]int i,j;
[*]do{
[*]cout<<"\n*********************************************"<<endl;
[*]cout<<"------------------Welcome To ABC Company --------------"<<endl;
[*]cout<<"Main Menu:"<<endl;
[*]cout<<"1.	Add a new employee."<<endl;
[*]cout<<"2.	Edit employee information."<<endl;
[*]cout<<"3.	Find employee."<<endl;
[*]cout<<"4.	Remove employee."<<endl;
[*]cout<<"5.	Print employee(s) information."<<endl;
[*]cout<<"6.	Sort company list"<<endl;
[*]cout<<"7.	Exit"<<endl;
[*]cout<<"*********************************************"<<endl;
[*]cin>>choise;
[*]switch(choise)
[*]{
[*]case 1:{
[*]	string key = "y";
[*]	while( key == "y" || key == "Y"){
[*]	cout<<"\n*********************************************"<<endl;
[*]    cout<<"----------- ABC Company >> Add a new employee.."<<endl;
[*]	for(i=0; i<size;i++){
[*]	cout<<"\nID: ";
[*]	cin>>id1;
[*]	cout<<"\nNR: ";
[*]	cin>>nr1;
[*]	cout<<"\nName: ";
[*]	cin>>n;
[*]	cout<<"\nDegree:m for Manager - s for Secretary - l for Labor : ";
[*]	cin>>d;
[*]	switch(d)	
[*]	{
[*]	case 'm' :
[*]		s=10000;
[*]		break;
[*]	case 's' :
[*]		s=5000;
[*]		break;
[*]	case 'l' :
[*]		s=4200;
[*]		break;
[*]	default: cout<<"wrong degree "; 
[*]		break;}
[*]	cout<<"\nSalary: "<<s <<"\n";
[*]	em[i].AddNewEmployee(n,s,d,id1,nr1);}
[*]	cout << "\n*********************************************";
[*]	cout << "\nDo you want to add more (y/n)? ";
[*]	do{getline( cin, key);}while( key != "y" && key != "Y" && key != "n" && key != "N");
[*]	cout << "*********************************************";
[*]    }}
[*]	break;
[*]case 2:
[*]int inf;
[*]int edit;
[*]int empid;
[*]string n_ed;
[*]char d_ed;
[*]string key = "y";
[*]while( key == "y" || key == "Y"){
[*]cout<<"------ ABC Company >> Edit an employee information.."<<endl;
[*]cout<<"Please enter employee ID: ";
[*]cin>>empid;
[*]for ( j=0 ; j<size ; j++){
[*]	if ( em[j].getidnu() == empid ){
[*]      inf=j;
[*]	  cout<<"\nWhat do you want to edit? "<<endl;
[*]cout<<"\n1.	Name : "<< em[inf].getname()<<endl;
[*]cout<<"\n2.	Degree: "<< em[inf].getde()<<endl;
[*]cin>>edit;
[*]switch(edit)
[*]{
[*]case 1 :
[*]cout<<"Name:";
[*]cin>>n_ed;
[*]em[inf].AddNewEmployee(n_ed,s,d,id1,nr1);
[*]break;
[*]case 2 :
[*]cout<<"Degree: ";
[*]cin>>d_ed;
[*]em[inf].AddNewEmployee(n,s,d_ed,id1,nr1);
[*]break;
[*]default: cout<<" wrong choise "; break;}
[*]break;}}

[*]cout << "\n*********************************************";
[*]	cout << "\nDo you want to edit another one information (y/n)?  ";
[*]	do{getline( cin, key);}while( key != "y" && key != "Y" && key != "n" && key != "N");
[*]	cout << "*********************************************";}}

[*]case 3 :

[*]	cout<<"i've got error here .. why?";  //error

[*]}while (choise != 7);

[*]	return 0;
[*]}
[/LIST]

thanx

Recommended Answers

All 5 Replies

Probably (ie, due to your crappy indentation) because the case is OUTSIDE the switch.

This is good. All the cases and braces line up, and you can instantly tell the structure of the code simply from following the indentation.

switch ( foo ) {
  case 1:
  switch ( bar ) {
    case 1:
  }
}

This is bad. Trying to save a couple of lines of source by compressing multiple braces leads to all sorts of trouble.

switch ( foo ) {
  case 1:
  switch ( bar ) {
    case 1:}}

Top tip:
Write all the control structures from the outside in.
Eg.

for ( ; ; ) {
}

Having got the structure of the for loop correct, then you can move the cursor and fill in the detail. Sure it's a few extra key presses, but it sure saves hours of playing "hunt the missing brace, then post on a message board".

So in your example, you would start with

switch ( choice ) {
  case 1: {
  }
  break;
  case 2: {
  }
  break;
  case 3: {
  }
  break;
  default: {
  }
  break;
}

Also in your case, you need to break out the functionality of your cases into separate functions, so your main() doesn't grow to such monstrous proportions.


It works for all the other bits of syntax which need to be balanced, such as "" () and /**/

Your code formatting is absolutely horrible! Completly unreadable. And that is probably why you don't understand the error. Reformat it, indent it properly, and you will probably see the problem. And don't be afraid to put { and } on their own separatelins, unlike the horrible code in line 122.

i'm sorry .. i know it's horrible but because i have a very long question and more less time .. i just want to solve it then clean it and make it more readable .. with using functions .. sorry .. :(

thanx ..

Don't be sorry -- clean it up first and fixing it will be a lot simpler. If you are concerned about time then you have already wasted too much time trying to figure out that crappy code. Here are some suggestions.

thanx .. i've clean it and fix it .. it become not horrible know :) .. i just have one houre know to finish it .. i'll put it here after i finish it .. thanx :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.