954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Enum, Char, and String Question

Hello all,

I am enrolled in an intro to C++ class, and I need some help on our final program. Any suggestions are greatly appreciated, as I am quite new to this please don't criticize.

Our program requirements require us to receive a command (I.E. delete) input as an array of characters, null-byte terminated. This input must be converted to all caps because the function should accept text of any case.

The function must then return the matching pre-defined enum command value.

We are not allowed to use strings, and should instead use strlen, strcpy, and strcmp.

Any ideas? Here is the code of what I've got so far. This is only the portion I am having trouble with.

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

enum CommandType { ADD, DELETE, PRINT_TRACK, PRINT_WEIGHT,
                   PRINT_CONSOLIDATE, SORT_BY_TRACKNO,
                   SORT_BY_TOTAL_WEIGHT, QUIT };
                   
//Prototypes
CommandType ReadAndReturnCommandType( );
        
// Global Constants
const int MAX_DESC_NAME_LEN = 30;
const int MAX_RECIPEINT_LEN = 30;
const int MAX_COMMAND_LEN = 30;
const int MAX_SHIPMENTS = 20;
const int MAX_ITEMS_PER_SHIPMENT = 20;
const int LARGER_THAN_NINE = 10;
const int NOT_FOUND = -1;


// Custom Alias Declarations
typedef char DescriptionType[MAX_DESC_NAME_LEN + 1];
typedef char RecipientType[MAX_RECIPEINT_LEN + 1];
typedef char CommandCStrngType[MAX_COMMAND_LEN + 1];


int main( )
{       
   CommandType command = ReadAndReturnCommandType();
   while ( command != QUIT )
   {
      cout << "Processing command ";
        PrintCommandType( command );
        cout << endl;
        HandleCommand ( shipList, command );
        cout << endl;
        command = ReadAndReturnCommandType();
   }
   return 0;
}

//Purpose: Reads a command string and returns
//         the appropriate CommandType.
//         The default return type is QUIT.
// Params: ( none )
CommandType ReadAndReturnCommandType()
{
   CommandCStrngType command;
   cin >> command;
   for( int i = 0; i < strlen(command); i++ )
      command[i] = toupper( command[i] );
   switch( command )          // <-------Here is where the error occurs.
      case "ADD":
         return ADD;
         break;
      case "DELETE":
         return DELETE;
         break;
// ......
      case default:
         return QUIT;
}
roemhildtg
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

As far as I know you can only 'switch' on int types. CommandCStrngType is definitely not convertible to an int! You should just replace your switch statement with a big if/else if/else.

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: