String Compare

This is a discussion on String Compare within the C Programming forums, part of the General Programming Boards category; Hi I'm Writing A simple program and im almost done but im stumped! I have my program set as follows....The ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Exclamation String Compare

    Hi I'm Writing A simple program and im almost done but im stumped! I have my program set as follows....The user enters 10 names into a character array using a for loop. The user picks these names from a list where there are 20 choices. (by the way i have the array declared as "char names[10][20];") Now here is the problem. I cant think of an EFFICENT way to get the results i want. I want it so that the program recognizes what name was typed in and, depending on the name that the user entered, have another integer variable changed. That might be confusing so heres an example. (Ex: name[0] = "john" so therefore "money = money+10") How can i do this? There are 20 different options, so i dont want to have like 200 "strcmp" (string compare) functions. Help is SOOO greatly appreciated. Thanks.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Is the variable always the same? If so, you could just create an array of a struct which has the name and the amount to increment:
    Code:
    struct foo {
        char name[20];
        int inc;
    };
    
    struct foo bar[] = {
        {"john", 10}, 
        {"fred", 20},
        /* ... */
    };
    Then you just iterate through the array, comparing each enter with the entered string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 06:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM

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