Thread: problems with the logic of if statment

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    problems with the logic of if statment

    The way I am understanding my code fragment:
    Code:
    case 2:
    
                fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
                fclose(filePointer);
                printf("Enter Dept Name: ");
                scanf("%s", caseTwoDept);
                printf("Enter Course Number: ");
                scanf("%d", &caseTwoCourse);
                qsort(data, MAX_RECORD, sizeof(sched_record), sortFunction);
                for (i=0; i < MAX_RECORD; i++){
                    if ((strcmp(caseTwoDept, data[i].Dept)==0) && caseTwoCourse == data[i].course){
                        printf("\n%d%s", data[i].start.hour, data[i].Dept);
                    }
                }
                break;
    particularly the if statement, I am comparing the string input with a department name AND comparing the int input with a course number. The way I have it set up it should be true and true. So if these conditions are both true then print the hour and Dept. If there is the same course, the condition is still true and should print another line with hour and Dept. Is my logic correct? My output is nothing. But when I change this
    Code:
    ((strcmp(caseTwoDept, data[i].Dept)==0) && caseTwoCourse == data[i].course
    to this:
    Code:
    ((strcmp(caseTwoDept, data[i].Dept)==0) && caseTwoCourse != data[i].course
    My output is hour and Dept for the given input and an input not given.
    What pray tell am I doing wrong here.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What pray tell am I doing wrong here.
    Besides the horrific inefficiency of reading in and sorting the whole file just to look up a single record in it, your explanation seems to indicate that the asked-for course number does not exist for the asked-for department name.

    If you cannot verify that yourself, then you'll have to post the entire data file and entire program. But try printing the entire record (or at least the Dept and course fields) just inside the for loop so you can verify that the asked-for record exists.

    BTW, caseTwoDept and caseTwoCourse are indicative of bad variable names.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Data file in text format even though the actualy file is binary.
    Code:
    Math 102 10 M 0800 0850 Schulte
    Eng  033  1 T 0930 1050 Shakespeare
    Art  308  2 M 0800 1150 VanGogh
    Anth 055 13 T 1200 1325 Kroeber
    CS   125  2 T 0800 0850 Hoare
    Eng  202 10 M 1000 1050 Chaucer
    Chem 100  5 T 1100 1250 Pauling
    Phys 395  2 T 1200 1250 Einstein
    CS   125  4 M 1030 1120 Knuth
    Math 420  2 T 0800 0950 al-Khowarizmi
    main.c
    Code:
    #include "classes.h"
    
    int main(void)
    {
        int switchInput;
        int i = 0;
        int tempCourse = 0;
        char caseOneDept[5];
        char caseTwoDept[5];
        int caseTwoCourse = 0   ;
        FILE *filePointer;
        sched_record data[MAX_RECORD];
    
        filePointer = fopen (BINFILE, "rb");
        if (filePointer == NULL) {
            printf("**Can't open file**");
            printf("**Check file permissions of file path**\n");
            exit(1);
        }
    
        printf("Enter your choice: ");
        scanf("%d", &switchInput);
    
        switch(switchInput)
        {
            case 1:
    
                fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
                fclose(filePointer);
                printf("Enter Dept Name: ");
                scanf("%s", caseOneDept);
                for (i=0; i<MAX_RECORD; i++){
                    if (strcmp(caseOneDept, data[i].Dept)==0){
                        if(tempCourse != data[i].course){
                        printf("\n%s %d", data[i].Dept, data[i].course);
                        tempCourse = data[i].course;
                        }
    
                    }
                }
                break;
    
            case 2:
    
                fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
                fclose(filePointer);
                printf("Enter Dept Name: ");
                scanf("%s", caseTwoDept);
                printf("Enter Course Number: ");
                scanf("%d", &caseTwoCourse);
                qsort(data, MAX_RECORD, sizeof(sched_record), sortFunction);
                for (i=0; i < MAX_RECORD; i++){
                    if ((strcmp(caseTwoDept, data[i].Dept)==0) && caseTwoCourse == data[i].course){
                        printf("\n%d%s", data[i].start.hour, data[i].Dept);
                    }
                }
                break;
        }
        return 0;
    }
    classes.h
    Code:
    #ifndef CLASSES_H_INCLUDED
    #define CLASSES_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BINFILE "classes.db"
    #define MAX_RECORD 10
    
    typedef enum {MW, TR} days;
    
    typedef struct {
        int hour, min;
    } Time;
    
    typedef struct {
        char Dept[5];
        int course, sect;
        days meet_days;
        Time start, end;
        char instr[20];
    } sched_record;
    
    int sortFunction(const void *p, const void *q);
    #endif // CLASSES_H_INCLUDED
    sortFunction.c
    Code:
    #include "classes.h"
    
    int sortFunction(const void *p, const void *q) {
            return ((sched_record *) p)->start.hour -
                           ((sched_record *) q)->start.hour;
    }
    I can verify the Departments and courses are there.
    Should not variable names represent what they are, not criptic names no one can understand when reading code?

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    I figured it out. It does what it is supposed to do.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Glad you figured it out -- anything worth sharing? I couldn't see anything wrong with the 'if'

    Quote Originally Posted by csharp100
    Should not variable names represent what they are, not criptic names no one can understand when reading code?
    For sure they should -- but in this case you don't need special temporaries for each case -- you could just have 1 tempCourse and 1 tempDept for both cases. Seeing the rest of your code, I guess the variable names you've used are fair enough.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    The problem was the binary file. For some reason when I ftp'ed the binary file off unix onto my windows machine, the course numbers, Sections, etc are all different ints. When I actually compiled and ran on unix, it worked just fine. The above code is good to go. Oh, I did take out the qsort call for the time being. I do have to sort later on.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Ahh, fair enough! That's to be expected: sometimes it works, but generally you shouldn't expect to be able to read in a binary file written on a different OS/architecture. For sure if you wrote it on Windows then read it back it'd be fine. Thanks for sharing, I was quite curious!

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Should not variable names represent what they are, not criptic names no one can understand when reading code?
    That's a strange statement. Did I say "your variables should be more cryptic"? I don't think so. I'm just saying that it's utterly pointless to have a caseOneDept and a caseTwoDept when a single dept will do. I've never seen variable names refer to the number of the case they are used in. It's very very strange, that's all.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by oogabooga View Post
    That's a strange statement. Did I say "your variables should be more cryptic"? I don't think so. I'm just saying that it's utterly pointless to have a caseOneDept and a caseTwoDept when a single dept will do. I've never seen variable names refer to the number of the case they are used in. It's very very strange, that's all.
    Okie Dokie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  2. Logic Problems
    By mike_g in forum C Programming
    Replies: 3
    Last Post: 08-06-2008, 10:19 AM
  3. C++ If statment question
    By TehClutchKiller in forum C++ Programming
    Replies: 17
    Last Post: 05-28-2008, 07:33 AM
  4. Logic problems
    By Beowolf in forum C++ Programming
    Replies: 29
    Last Post: 10-31-2007, 08:36 AM
  5. if statment
    By rkjd2 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 11:48 AM