If statments for a struct w/ strings

This is a discussion on If statments for a struct w/ strings within the C Programming forums, part of the General Programming Boards category; Hello all! Trying to finish up my last programming assignment for the semester. In the beginning of the program I ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    If statments for a struct w/ strings

    Hello all! Trying to finish up my last programming assignment for the semester. In the beginning of the program I created a struct, and one of the individual elements of the struct is a character string called "action". What I want to do is IF action is ____, then do this. I'm getting all sorts of compiler errors. I'm only going to post the code from the function I wrote it in, but if you want to see the struct or main, let me know.

    Code:
    int readfile(FILE *ifp, struct class student[]){
       int num, index;
    
       //Read in number of records.
       fscanf(ifp, "%d", &num);
    
       //Read in each record one by one.
       for (index=0; index<num; index++) {
         fscanf(ifp, "%s", student[index].last);
         fscanf(ifp, "%s", student[index].first);
         fscanf(ifp, "%s", student[index].action);
    
            //If they took a class or are doing grade forgiveness,
            // only scan in hours and grade.
    
           if(student[index].action == 'TOOK_CLASS' ||
              student[index].action == 'GRADE_FORGIVE'){
                   fscanf(ifp, "%d", &student[index].hours);
                   fscanf(ifp, "%s", student[index].grade);
                                                       }
    
            //If they are changing their grade, scan in hours, old grade, and new grade.
           if(student[index].action[] == 'CHANGE_GRADE'){
                   fscanf(ifp, "%d", &student[index].hours);
                   fscanf(ifp, "%s", student[index].grade);
                   fscanf(ifp, "%s", student[index].newgrade);
                                                      }
       }
    
     return num;
    
    }
    Thanks!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Things like this:
    Code:
    'TOOK_CLASS'
    ...seem to be your problem. Are they a global variable or a string literal?

    If they're a string use double quotes:
    Code:
    "TOOK_CLASS"
    If they're a variable, no quotes:
    Code:
    TOOK_CLASS
    Another thing worth mentioning is a bit of a logical error. No matter what "action" the student has, the first two things you read in are hours and grade. So there is no need for that first if statement.

    Code:
    fscanf(ifp, "%d", &student[index].hours);
    fscanf(ifp, "%s", student[index].grade);
    
    if(student[index].action[] == 'CHANGE_GRADE')
          fscanf(ifp, "%s", student[index].newgrade);
    Last edited by SlyMaelstrom; 12-01-2005 at 09:33 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    In C language you don't test equality of strings using the == operator like you do in c++. you have to use strcmp() for case-sensitive comparisons. I don't think there is a standard C function for case-insensive comparisons, some compilers have stricmp() while others have something like comparenocase().

    So an if statement might look like this (assuming action is a char array).
    Code:
    if( strcmp(student[index].action, "CHANGE_GRADE") == 0)
    {
       // do something when true
    }

  4. #4
    Registered User KidA's Avatar
    Join Date
    Nov 2005
    Location
    Ohio, USA
    Posts
    26
    If only string comparisons were that easy in C...

    Most string operations will require that you call some kind of string function. The if-else arrangement you have shown would require the use of strcmp(), such as:
    Code:
    if ( strcmp(student[index].action, "CHANGE_GRADE") == 0 )
    {
        // take CHANGE_GRADE action here
    }
    While this will work, using a string for this type of logic is inefficient both in terms of RAM requirements and performance. A better alternative is to use a simple enumeration and branch on its value, such as:
    Code:
    typedef enum
    {
         CHANGE_GRADE = 0
        ,TOOK_CLASS
        ,GRADE_FORGIVE
    } ACTIONS;
    ...declare the action member of your structure to be of this type and then write your comparisons like this:
    Code:
    if ( student[index].action == CHANGE_GRADE )
    {
        // take CHANGE_GRADE action here
    }
    Hope this helps...

    Edit: Or what Dragon said...slipped in there while I was typing this

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Thank you!

    Thank you for your help.... the strcmp worked perfect. I knew that I had to use strcmp somewhere, but I was not sure where or how.

    I understand the logical error I have, but in the second if statement (for CHANGE_GRADE), technically student[index].grade is really the old grade for the class, (in University terms) so, I did it the way I did for code readability, and compatibility w/ the problem and my comments throughout the code.

  6. #6
    Registered User KidA's Avatar
    Join Date
    Nov 2005
    Location
    Ohio, USA
    Posts
    26
    Well as long as you have a reason!

    I wasn't intending to call what you've done a "logical error", because it's not - it's a valid way to approach the problem. I just tend to restrict my use of strings in C whenever possible because they are awkward to work with...

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by KidA
    I wasn't intending to call what you've done a "logical error",
    He was talking about what I said, with the extra if statement.

    It's fine to have the two if statements. It does make it clearer for people that don't understand code too well. As you become more experienced, you'll find that you don't have to sacrifice "performance" (though unnoticable in this case) for readability.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. 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
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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