Switch case and enum help

This is a discussion on Switch case and enum help within the C++ Programming forums, part of the General Programming Boards category; I am starting to get used to the use of switch case and enum... Basically, all that I need to ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Structure and enum help

    I am starting to get used to the use of switch case and enum... Basically, all that I need to learn all the basics of what I need is how I'm supposed to personally record information without having to manually go into the code.

    I want to simply type in the information and have it recorded into the appopriate value... instead of simply having it instantly record a pre-made value.

    Code:
    #include <iostream.h>
    using namespace std;
    
    enum athel_names { Ed, Joe, Bill, Dan};
    enum class_level { Freshman, Sophomore, Junior, Senior, Drop_Out, Graduate };
    enum sport_teams { Football, Basketball, Baseball, None };
    enum grade_acept { Yes, No };
    
    struct students
    {
    athel_names an;
    class_level cl;
    sport_teams st;
    grade_acept ga;
    };
    
    int main()
    {
    students sport;
        
    sport.an = Ed;
    sport.cl = Senior;
    sport.st = Football;
    sport.ga = No;
    
    system("cls");
    
      switch ( sport.an ) {
      case Ed:
        cout << "Ed\n";    
        break;
      case Joe:         
        cout << "Joe\n";    
        break;
      case Bill:           
        cout << "Bill\n";    
        break;
      case Dan:        
        cout << "Dan\n";  
        break;
      default:     
        break;
      }
    
    system("pause");
    }
    Basically, this system records the information on 4 people (Either Ed, Joe, Bill, or Dan), which school class they are in (Freshman, Sophomore, Junior, Senior, Drop Out, or Graduate), what sport they would wish to take (Football, Basketball, Baseball, or None), and if their grades are/were acceptable to allow them to take a sport... Not a real life situation, but it works...

    The problem isn't in the coding (Though, it may be somewhat inefficiant due to me being a novice), because it works, the problem lies in my need to type in the information... Sort of typing in a normal integer with cin.

    Sorry for the lengthy post, and whether or not I get help, thanks for your time! n_n
    Last edited by SomeCrazyGuy; 04-17-2005 at 10:05 PM.

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    You probably want to approach this in a totally different way:

    Use strings.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
         std::string athlete_name;
         std::string class_level;
         std::string sports_teams;
         std::string grade_accept;
    
         std::cout << "Enter the name: " << std::flush;
         std::getline(std::cin, athlete_name);//get athlete name
    
         //................ (continue getting the information)
    
         std::cout << "The athlete's name is: " << athete_name << std::endl;
         //.............. (continue this)
    
    
         std::cin.get();
    }
    Last edited by Kybo_Ren; 04-17-2005 at 10:56 PM. Reason: Had a function I ended up not using. :-\

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I want to simply type in the information and have it recorded into the appopriate value
    Enums aren't variables, they are constants, so you can't read input into an enum. Enums just make your code more readable, they don't actually do anything logic wise. In fact, I suggest you forget about them completely since they seem to be a bright light blinding you from seeing what you need to do. After you work out the logic of your program, and finish writing it so that it works correctly, then if you have time, and you want to make your code more readable, you can go back and add some enums where appropriate. At this point in your C++ learning, don't dwell on enums--they are syntactic sugar.

    For instance, this might be your main() file:
    Code:
    #include <iostream>
    #include "fish.h" 
    
    using namespace std;
    
    void display_message(int fishType);
    
    int main()
    {
    	int input;
    	cout<<"Enter an int designating the the type of fish you caught\n";
    	cout<<"(bass=0, trout=1, carp=2): ";
    	cin>>input;
    
    	display_message(input);
    	
    	return 0;
    }
    and here is "fish.h":
    Code:
    //fish.h
    
    #ifndef __FISH_H__
    #define __FISH_H__
    
    #include <iostream>
    using namespace std;
    
    void display_message(int fishType)
    {
    	switch (fishType)
    	{
    	case 0:
    		cout<<"Last year there was a blight on those fish and 4,000 were killed."<<endl;
    		break;
    	case 1:
    		cout<<"According to statistics, that is the most popular fish to eat."<<endl;
    		break;
    
    	case 2:
    		cout<<"In 2004, 40,000 anglers ranked that fish as one of the\n";
    		cout<<"best fighters pound for pound."<<endl;
    		break;
    	default:
    			cout<<"I've never caught any of those."<<endl;
    	}
    }
    
    #endif //__FISH_H__
    In the file "fish.h", it's not exactly clear what 0, 1, and 2 mean. In fact, as you are programming that, you may even forget what they mean and get the messages mixed up. So, you might create an enum:
    Code:
    //fish.h
    
    #ifndef __FISH_H__
    #define __FISH_H__
    
    #include <iostream>
    using namespace std;
    
    void display_message(int fishType)
    {
    	enum {bass, trout, carp};  //anonymous enum
    	
    	switch (fishType)
    	{
    	case bass:
    		cout<<"Last year there was a blight on those fish and 4,000 were killed."<<endl;
    		break;
    	case trout:
    		cout<<"According to statistics, that is the most popular fish to eat."<<endl;
    		break;
    
    	case carp:
    		cout<<"In 2004, 40,000 anglers ranked that fish as one of the\n";
    		cout<<"best fighters pound for pound."<<endl;
    		break;
    	default:
    			cout<<"I've never caught any of those."<<endl;
    	}
    }
    
    #endif //__FISH_H__
    Now, it's clearer what's going on in "fish.h", and it may be easier for you as a programmer to keep from getting the messages in the wrong cases. Note, that the first "fish.h" worked perfectly fine in the program--it just wasn't that readable--and the only thing adding an enum did was change some numbers to words.
    Last edited by 7stud; 04-18-2005 at 12:04 AM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I would personally suggest using std::maps for this, it's alot easier than that approach in my personal opinion.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I would personally suggest using std::maps for this, it's alot easier than that approach in my personal opinion.
    Interesting suggestion for someone who is obviously a beginner. So, you think they should go from learning enums on page 45 of their beginning C++ book to learning the STL? It's not clear whether the op has even been introduced to for-loops yet, which generally come after if-statements and switch statements in a beginning C++ book.
    Last edited by 7stud; 04-18-2005 at 04:20 PM.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Talking

    Quote Originally Posted by 7stud
    Interesting suggestion for someone who is obviously a beginner. So, you think they should go from learning enums on page 45 of their beginning C++ book to learning the STL? It's not clear whether the op has even been introduced to for-loops yet, which generally come after if-statements and switch statements in a beginning C++ book.
    Basically, I came into learning C++ as a suggestion from my friend 2 weeks ago after seeing my immense bordom with creating complex programming using BASIC programming for the TI-83+. I have mastered the use of almost, if not all, concepts and uses of each possible option available... So, again, my friend thought C++ would be a good change of pace from the simplicity and inefficiency of a graphing calculator.

    So, I am basically in the process of translating common statements I would have used on the TI-83+... For, If, While, Do-While (Easily learned), Strings, Arrays, etc... They were all very much easy to learn with my previously earned skills. But, the major dissapointment came in with the realization that C++ didn't have a direct randomization process... Only a heart-breaking time-based system was what I could find, so (since I am a beginner) I am still looking for a good way to randomize (or should I say pseudo-randomize?) numbers... Because a time-based system would be very much terrible idea with my rapid and large quanitity need for random numbers in my usual programs...

    Yes... I do understand your reply, but don't worry, I will likely be able to catch on pretty quick to what they are trying to suggest... C++, at least from what I see, should be quite easy to use and learn once I gain some traction.

    (I was in a great hurry this morning and had only enough time to print your answer (fish), but since the page was partly cut off... your reply sounded incredibly blunt, rude, and, at times, almost nonsensical... But I had a good laugh after reading the full reply, seeing that you were, in fact, being quite kind and helpful...)

    So, thank you, everyone, for your help, yet again. n_n
    Last edited by SomeCrazyGuy; 04-18-2005 at 08:17 PM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    But, the major dissapointment came in with the realization that C++ didn't have a direct randomization process... Only a heart-breaking time-based system was what I could find, so (since I am a beginner) I am still looking for a good way to randomize (or should I say pseudo-randomize?) numbers... Because a time-based system would be very much terrible idea with my rapid and large quanitity need for random numbers in my usual programs...
    I'm not sure what you mean by a "time based system". C++ has a rand() function that will return all the random numbers you want. Initially, the rand() function is usually seeded with the time on the user's computer, but to characterize that as a "time based system" is misguided. Or, maybe you are talking about the internals of the rand() function(which I have no knowledge of)? This site has a short tutorial on rand() that you might want to read:

    http://www.cprogramming.com/tutorial/random.html

    I will likely be able to catch on pretty quick to what they are trying to suggest... C++, at least from what I see, should be quite easy to use and learn once I gain some traction.
    I'm sure most members on the forum will chuckle when they read that. C++ is complex language that includes much more than the for loops, while loops, arrays, and strings you enountered in BASIC. Those features are basic features in any programming language, and you can learn the new syntax for those features in a new programming language fairly quickly. But, the power of C++ has to do with pointers, functions, classes, inheritance, class templates, virtual functions, operator overloading, polymorphism, and linked lists and other containers in the STL. You have a lot to learn, and you can spend a lifetime at it.
    Last edited by 7stud; 04-18-2005 at 11:28 PM.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Quote Originally Posted by Kybo_Ren
    You probably want to approach this in a totally different way:

    Use strings.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
         std::string athlete_name;
         std::string class_level;
         std::string sports_teams;
         std::string grade_accept;
    
         std::cout << "Enter the name: " << std::flush;
         std::getline(std::cin, athlete_name);//get athlete name
    
         //................ (continue getting the information)
    
         std::cout << "The athlete's name is: " << athete_name << std::endl;
         //.............. (continue this)
    
    
         std::cin.get();
    }
    Would it be as efficient to use 'using namespace std'?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string a_n;
    string c_l;
    string s_t;
    string g_a;
    
    cout << "Enter students name: " << flush;
    getline(cin, a_n);
    cout << "Enter " << a_n << "'s class: " << flush;
    getline(cin, c_l);
    cout << "Enter " << a_n << "'s wanted sport: " << flush;
    getline(cin, s_t);
    cout << "Are " << a_n << "'s grades acceptable?: " << flush;
    getline(cin, g_a);
    
    cout << "The students name is: " << a_n << '\n';
    cout << "The students class is: " << c_l << '\n';
    cout << "The students wanted sport is: " << s_t << '\n';
    cout << "Are grades acceptable to allow into sports?: " << g_a << '\n';
    
    cin.get();
    }
    Furthermore, the point of my program was to allow for extra storage of information with a single set of code... If I am to keep a set of code with strings, then I will need a set of code that will permanently keep code based upon entered name and call it out at users will.

    ~~~

    Example:

    [1 = Student age input]
    [2 = Student age aquirement]

    1
    Enter name: Joe
    Age?: 17

    [Program closes]

    1
    Enter name: Bill
    Age?: 14

    [Program closes]

    2
    Enter name: Joe
    Joe's age is: 17

    [Program closes]

    ~~~

    I don't know how well my original way would work, but I am going to look for a way to permanently store information such as this... Thanks for the idea though, it really helped to re-establish my knowledge of strings and other such things. n_n

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I don't know how well my original way would work,
    It won't. Neither will the suggestions provided because you never stated that was your goal. All variables in a program are destroyed when your program ends along with any data they contain. If you want to save the data, you have to store it in a database(which is beyond your reach at this point), or write it to a text file. File input/output might still be out of reach for you, but at least now you know what your goal is.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I don't know how well my original way would work,
    It won't. Neither will the suggestions provided because you never stated that was your goal. All variables in a program are destroyed when your program ends along with any data they contain.

    but I am going to look for a way to permanently store information such as this...
    If you want to save the data, you have to store it in a database(which is beyond your reach at this point), or write it to a text file. File input/output is what you need to learn about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 10:40 PM
  2. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 11:03 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. enumeration with switch case?
    By Shadow12345 in forum C++ Programming
    Replies: 17
    Last Post: 09-26-2002, 05:57 PM
  5. What enum got to do with it?
    By correlcj in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21