-
July 26th, 2012, 05:04 AM
#1
Switch Output Error
Greeting Codeguru World.
I have a question related to switch statement, which the switch in class employee2 gives me error at output. Program compiled well. I am using Code::Block Compiler V10.5. I have created object of class employee2 in main() function to get data from user, store it and display it. At the output, Compiler doesn't show actual output what i am expecting. The fun thing is my compiler printed emocion like (), where (hourly/monthly/weekly) was supposed to print.
Is there any mistake in my Coding..
Note : I am beginner in (OOP C++ Programming) (I guess i might made some common error, which i can't find myself)
Code:
#include <iostream>
using namespace std;
enum period {hourly,weekly,monthly};
class employee2
{
private :
char ch;
period x;
double compensation;
public :
void getdata()
{
cout <<"\nEnter Compensation : "; cin >> compensation;
cout <<"\nEnter Period :";
cout <<"\nPress 'h' for hourly"
<<"\nPress 'w' for weekly"
<<"\nPress 'm' for Monthly";
cin >> ch;
switch(ch)
{
case 'h' : x = hourly; break;
case 'w' : x = weekly; break;
case 'm' : x = monthly; break;
}
switch(x)
{
case 0 : ch = hourly; break;
case 1 : ch = weekly; break;
case 2 : ch = monthly; break;
default : cout << "Unknown Key!";
}
}
void putdata() const
{
cout << "\nPayment Period is " << ch << '\n';
}
};
Expecting some suggestion..
Regards
Basanta
-
July 26th, 2012, 05:24 AM
#2
Re: Switch Output Error
Is this the whole program? If so when I copied it into my code::blocks it didn't even compile correctly. I got an error that said something like "undefined reference to WinMain@16".
-
July 26th, 2012, 06:12 AM
#3
Re: Switch Output Error
Originally Posted by Rafen
Is this the whole program? If so when I copied it into my code::blocks it didn't even compile correctly. I got an error that said something like "undefined reference to WinMain@16".
It is only the class of my program. The Whole Program is
Code:
#include <iostream>
using namespace std;
enum period {hourly,weekly,monthly};
const int LEN = 80;
class employee
{
private :
char name[LEN];
unsigned long number;
public :
void getdata()
{
cout << "\nEnter Last name : "; cin >> name;
cout << "\nEnter number : "; cin >> number;
}
void putdata() const
{
cout << "\nName : " << name;
cout << "\nNumber : " << number;
}
};
class employee2
{
private :
char ch;
period x;
double compensation;
public :
void getdata()
{
cout <<"\nEnter Compensation : "; cin >> compensation;
cout <<"\nEnter Period :";
cout <<"\nPress 'h' for hourly"
<<"\nPress 'w' for weekly"
<<"\nPress 'm' for Monthly";
cin >> ch;
switch(ch)
{
case 'h' : x = hourly; break;
case 'w' : x = weekly; break;
case 'm' : x = monthly; break;
}
switch(x)
{
case 0 : ch = hourly; break;
case 1 : ch = weekly; break;
case 2 : ch = monthly; break;
default : cout << "Unknown Key!";
}
}
void putdata() const
{
cout << "\nPayment Period is " << ch << '\n';
}
};
class manager : public employee, public employee2
{
private :
char title[LEN];
double dues;
public :
void getdata()
{
employee::getdata();
employee2::getdata();
cout << "Enter title :"; cin >> title;
cout << "Enter Gold Club's due amount : "; cin >> dues;
}
void putdata() const
{
employee::putdata();
employee2::putdata();
cout << "\nTitle : " << title;
cout << "\nGolf Club Dues : " << dues;
}
};
class scientist : public employee, public employee2
{
private :
int pubs;
public :
void getdata()
{
employee :: getdata();
employee2 :: getdata();
cout << "\nEnter number of pubs :"; cin >> pubs;
}
void putdata() const
{
employee :: putdata();
employee2 :: putdata();
cout << "\nNumber of Publication :" << pubs;
}
};
class laborer : public employee
{
};
int main()
{
manager m1;
scientist s1;
laborer l1;
cout <<"\nEnter Data for Manager 1";
m1.getdata();
cout <<"\nEnter Data for Scientist 1";
s1.getdata();
cout <<"\nEnter Data for Laborer 1";
l1.getdata();
cout << '\n';
cout <<"\nData for Manager 1";
m1.putdata();
cout <<"\n\nData for Scientist 1";
s1.putdata();
cout <<"\n\nData for Laborer 1";
l1.putdata();
return 0;
}
Check Output Once..
-
July 26th, 2012, 07:23 AM
#4
Re: Switch Output Error
Seemed to work okay for me with Visual C++, but I'm more concerned with your class hierarchy. I don't see the need for two employee classes and multiple inheritance. That's going to add a lot of confusion unnecessarily. Why not a single employee class?
-
July 26th, 2012, 07:24 AM
#5
Re: Switch Output Error
In putdata, you are writing ch which is a char. That is the reason for the funny output.
Try
Code:
cout << "\nPayment Period is " << static_cast<int>(ch) << '\n';
-
July 26th, 2012, 08:04 AM
#6
Re: Switch Output Error
Originally Posted by basanta
The fun thing is my compiler printed emocion like ( ), where (hourly/monthly/weekly) was supposed to print.
Nope, you won't get the program to print one of the strings "hourly", "monthly" or "weekly" unless you tell it to.
An enum will internally be an integer, so everytime you write in your code hourly it will be replaced by 0, weekly by 1, etc. To print out the key, you need to switch again:
Code:
void print_period(period p) {
switch(p) {
case hourly: cout << "hourly"; break;
case weekly: cout << "weekly"; break;
case monthly: cout << "monthly"; break;
default: cout << "unknown";
}
}
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
July 26th, 2012, 08:37 AM
#7
Re: Switch Output Error
You can also define an operator<< for the enum:
Code:
std::ostream& operator<<( std::ostream& os, period p )
{
switch(p)
{
case hourly: os<< "hourly"; break;
case weekly: os<< "weekly"; break;
case monthly: os<< "monthly"; break;
default: os<< "unknown";
}
return os;
}
-
July 26th, 2012, 09:16 AM
#8
Re: Switch Output Error
Originally Posted by GCDEF
Seemed to work okay for me with Visual C++, but I'm more concerned with your class hierarchy. I don't see the need for two employee classes and multiple inheritance. That's going to add a lot of confusion unnecessarily. Why not a single employee class?
I did tried with a single class 'employee' but, it seems creating more complexity in single class. So, i created new class called 'employee2' to get compensation from user and their period type using switch..
-
July 26th, 2012, 09:17 AM
#9
Re: Switch Output Error
Originally Posted by GCDEF
Seemed to work okay for me with Visual C++, but I'm more concerned with your class hierarchy. I don't see the need for two employee classes and multiple inheritance. That's going to add a lot of confusion unnecessarily. Why not a single employee class?
I did tried with a single class 'employee' but, it seems creating more complexity in single class. So, i created new class called 'employee2' to get compensation from user and their period type using switch..
-
July 26th, 2012, 11:12 AM
#10
Re: Switch Output Error
Originally Posted by basanta
I did tried with a single class 'employee' but, it seems creating more complexity in single class. So, i created new class called 'employee2' to get compensation from user and their period type using switch..
Don't all employees get compensated? Why not just move the employee2 stuff to employee? I would avoid multiple inheritance unless you absolutely need it.
-
July 26th, 2012, 11:44 AM
#11
Re: Switch Output Error
Originally Posted by basanta
I did tried with a single class 'employee' but, it seems creating more complexity in single class. So, i created new class called 'employee2' to get compensation from user and their period type using switch..
The flaw in your classes is that you're making input and output routines decide how you're setting up your classes. That's why you're getting confusion and complexity.
You shouldn't place I/O routines in the class code. What if I want to build an employee from data in a file? How would I do that? Add file input to my classes? What if I don't like the words you used in the cout's? What if I want to create employees some other way, and not by prompting?
The employee class should only have information pertaining to a single employee. It shouldn't have input and output routines embedded into them. The way this should be done is that the I/O should be seperated from the class itself.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|