473,383 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. int EXIT=1;
  4. int main()
  5. {
  6.      System system = {{getline}, {println}};
  7.  
  8.      while (EXIT)
  9.      { 
  10.           printf("# ");
  11.           char exit[] = "exit";
  12.           char help[] = "help";
  13.           char dataInput[80];
  14.           int choice;
  15.           gets(dataInput);
  16.  
  17.           if(strcmp (dataInput, exit ) == 0)
  18.           { 
  19.                choice = 1;
  20.           }
  21.           else if(strcmp (dataInput, help ) == 0)
  22.           {
  23.                choice=2; 
  24.           }
  25.           else
  26.           {
  27.                choice=0;
  28.           }
  29.           switch (choice)
  30.           { 
  31.                case 1: system.out.println("System Shutdown...");
  32.                EXIT=0;
  33.                break;
  34.  
  35.                case 2: system.out.println("No Help for NOOBS!");
  36.                break;
  37.  
  38.                default: printf("Command not recognized!\n");
  39.                break;
  40.           }
  41.  
  42.      }
  43.      return 0;
  44. }
  45.  
Mar 16 '08 #1
4 1974
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 16 '08 #2
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.
Mar 17 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 17 '08 #4
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)

Expand|Select|Wrap|Line Numbers
  1. // comparing apples with apples
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8.   string str1 ("green apple");
  9.   string str2 ("red apple");
  10.  
  11.   if (str1.compare(str2) != 0)
  12.     cout << str1 << " is not " << str2 << "\n";
  13.  
  14.   if (str1.compare(6,5,"apple") == 0)
  15.     cout << "still, " << str1 << " is an apple\n";
  16.  
  17.   if (str2.compare(str2.size()-5,5,"apple") == 0)
  18.     cout << "and " << str2 << " is also an apple\n";
  19.  
  20.   if (str1.compare(6,5,str2,4,5) == 0)
  21.     cout << "therefore, both are apples\n";
  22.  
  23.   return 0;
  24. }
  25.  
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.
Mar 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
8
by: brian.digipimp | last post by:
I turned this in for my programming fundamentals class for our second exam. I am a c++ newb, this is my first class I've taken. I got a good grade on this project I'm just wondering if there is a...
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
6
by: mike3 | last post by:
Hi. I did this in a C program, which involves searching for the occurence of a string in another string: --- posptr = strstr(MyString, StringToFind); if(posptr == NULL) return(-1); /*...
5
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have about a 40 or more lines in my switch statement and was just wondering if it was worth extracting out the individual case statement which would involve passing a Table object, 2...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.