Thread: Help with Excercise

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    Help with Excercise

    Hi
    I am doing a number of excercises from a book. I just finished a chapter on structures and enumerated daya types, but this one question i'm failing to understand. Here it is:

    We said earlier that C++ I/O statements don't automatically understand the values of enumerated data types. Instead, the >> and << operators think of such variables simply as integers. You can overcome this limitation by using switch statements to translate between the user's way of expressing an enumerated variable and the acutal values of the enumerated variable. For example, imagine an enumerated type with values that indicate an employee type within an organization:

    enum etype { laborer, secretary, manager, accountant, executive, researcher };

    Write a program that first allows the user to specify a type by entering its first letter (l, s, m and so on), then stores the type chosen as a value of a variable of type enum etype, and finally displays the comlete word for this type.

    Enter employee type (first letter only) laborer, secretary, manager, accountant, executive, researcher): a
    Employee type is accountant.

    You'll Probably need two switch statements: one for input and oe for output. END OF EXCERCISE Q:

    I don't really understand what he wants me to do, especially with the last sentance. Would someone explain to me plz thanks
    #include <Jesus.h>
    It will save your life

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Basically what the book is explaining to you is that you can create enumerated data types using the enum keyword and that if you want to do IO with variables of such a type, the << and >> operators will not work the way you would want it to. For example, lets say you do the following:
    Code:
    enum etype { laborer, secretary, manager, accountant, executive, researcher };
    
    etype employee = laborer;
    cout << employee << endl;
    Instead of printing something like "employee" it will print the numeric value associated with the value of the variable. The author is asking you to use a switch statement to print what value the variable contains instead. Think of it this way:
    Code:
    if (employee == laborer)
      cout << "laborer\n";
    else if (employee == secretary)
      cout << "secretary\n";
    ...
    Doing this, you are determining what the value is and printing some string accordingly as opposed to asking the compiler to figure out a string that might be associated with the enumerated guy.

    hth

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    Ok, thanks. But what about the last sentance.
    you probably need two switch statements: one for input and one for output

    why would you need a switch statement for both?
    #include <Jesus.h>
    It will save your life

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
    // output
    switch (blah) {
      case one:  cout << "one\n"; break;
      // ...
    }
    
    // input
    cin >> choice;
    switch (choice) {
      case 1: blah = one; break;
      // ...
    }

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    ok i tried doing it. Here it is. It comiles good and runs fine, but do you think iit's what the author intended it to be?

    Code:
    #include <iostream.h>
    
    enum employee {laborer, secretary, manager, accountant, executive, researcher};
    
    void main()
    	{
    	char letter;
    	employee etype;
    
    	cout << "Enter employee type (first letter only) " << endl
    		  << "\t(laborer, secretary, manager, " << endl
    		  << "\taccountant, executive, researcher): ";
    	cin >> letter;
    
    	switch(letter)
    		{
    		case 'l': etype = laborer; break;
    		case 's': etype = secretary; break;
    		case 'm': etype = manager; break;
    		case 'a': etype = accountant; break;
    		case 'e': etype = executive; break;
    		case 'r': etype = researcher;
    		}
    	switch(etype)
    		{
    		case 0:
    			cout << "\nEmployee type is laborer";
    			break;
    		case 1:
    			cout << "\nEmployee type is secretary";
    			break;
    		case 2:
    			cout << "\nEmployee type is manager";
    			break;
    		case 3:
    			cout << "\nEmployee type is accountant";
    			break;
    		case 4:
    			cout << "\nEmployee type is executive";
             break;
    		case 5:
    			cout << "\nEmployee type is researcher";
    		}
    	}
    #include <Jesus.h>
    It will save your life

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    looks fine. i haven't tested it but it looks okay. as long as you understand enumerated types and switch statements, you'll be okay; and should be able to move on.

    you should start using iostream instead of iostream.h (standard), and int main.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The author probably wants
    Code:
        cout << "\nEmployee type is ";
        switch(etype) 
            {
            case laborer :
                cout << "laborer";
            break;  
            case secretary :
                cout << "secretary"; 
            break;  
            case manager :
                cout << "manager";
            break;  
            case accountant :
                cout << "accountant";
            break;  
            case executive :
                cout << "executive";
            break;  
            case researcher :
                cout << "researcher";
            break;
            }
    This is slightly easer to maintain, as you can change the order of manager or accountants in the enum, or assign them specific values. You are still out of luck if you add a new value. What you have is fine.

    Still these are the rules for a language called C++, I have no idea what the rules are in your vile heretical "void main()" language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 2
    Last Post: 10-16-2008, 09:23 PM
  2. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 8
    Last Post: 10-15-2008, 07:10 PM
  3. excercise
    By luigi40 in forum C# Programming
    Replies: 9
    Last Post: 11-22-2005, 03:25 AM
  4. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 10:18 AM