A better way to write the switch/case statement.......

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaemonCoder
    New Member
    • Mar 2008
    • 43

    A better way to write the switch/case statement.......

    I need some suggestions on how to betterer write the following code. I tried the array of arguments, and san through for a match then strcmp but i get massive errors. The code below is the only way i could get it to work.. if some one has any sugestions on hjow to make this better please let me know... The definiton of the class was left out intentionally. if you need this please let me know

    Code:
     
     
    int EXIT=1;
    int main()
    {
    	 System system = {{getline}, {println}};
     
    	 while (EXIT)
    	 { 
    		  printf("# ");
    		  char exit[] = "exit";
    		  char help[] = "help";
    		  char dataInput[80];
    		  int choice;
    		  gets(dataInput);
     
    		  if(strcmp (dataInput, exit ) == 0)
    		  { 
    			   choice = 1;
    		  }
    		  else if(strcmp (dataInput, help ) == 0)
    		  {
    			   choice=2; 
    		  }
    		  else
    		  {
    			   choice=0;
    		  }
    		  switch (choice)
    		  { 
    			   case 1: system.out.println("System Shutdown...");
    			   EXIT=0;
    			   break;
     
    			   case 2: system.out.println("No Help for NOOBS!");
    			   break;
     
    			   default: printf("Command not recognized!\n");
    			   break;
    		  }
     
    	 }
    	 return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Why not display a menu:

    1 - Exit
    2 - Help

    and have the user enter a number? Then just switch on that number.

    An invalid number would go to default where you could ask for another selection.

    Comment

    • DaemonCoder
      New Member
      • Mar 2008
      • 43

      #3
      This is being designed for a backend commandline interface for an os.. i dont want a numbered system because the commands are always gonna be a word. ie exit or help... Otherwise that would be a perfect solution.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        That is precisely why you want a number.

        The application makes its decision based on the number.

        Your user interface to the command line converts the users choice to the correct number.

        That way you can have different command lines without channging the application code.

        Comment

        • dwurmfeld
          New Member
          • Mar 2008
          • 9

          #5
          I would use the "string" class in C++, it has a powerful compare method:

          Here is an example of how it can be used: (from cplusplus.com)

          Code:
          // comparing apples with apples
          #include <iostream>
          #include <string>
          using namespace std;
          
          int main ()
          {
            string str1 ("green apple");
            string str2 ("red apple");
          
            if (str1.compare(str2) != 0)
              cout << str1 << " is not " << str2 << "\n";
          
            if (str1.compare(6,5,"apple") == 0)
              cout << "still, " << str1 << " is an apple\n";
          
            if (str2.compare(str2.size()-5,5,"apple") == 0)
              cout << "and " << str2 << " is also an apple\n";
          
            if (str1.compare(6,5,str2,4,5) == 0)
              cout << "therefore, both are apples\n";
          
            return 0;
          }
          Since you only have a minimal number of answers, like "exit", "Exit", "EXIT", a simple if-else tree should suffice, after all, you would need the if-else tree to assign an enumeration to a string in the first place.

          Comment

          Working...