Thread: Overlooking something in a comparison

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

    Overlooking something in a comparison

    I am trying to compare the input from the user to whether they entered something or not. If they did not, move onto the next input. I know I am missing something. I tried several ways of comparing:
    strcmp, this =="NULL" this = "NULL", etc. What am I missing. Here is my code:
    Code:
    case 3:
                fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
                fclose(filePointer);
                printf("Enter Dept Name: ");
                  scanf("%s", tempDept);
                printf("Enter Course Number of 0 for any course in department: ");
                  scanf("%d", &tempCourse);
                printf("Enter Days of Class, M for MW, T for TTH or D for any: ");
                  scanf("%s", tempDay);
                printf("Enter A for mornings, P for afternoons of D for any: ");
                  scanf("%s", tempTime);
                for (i=0; i < MAX_RECORD; i++) {
                    if (tempDept == "NULL"){
                        if (strcmp(tempDept, data[i].Dept)==0) {
                            printf("\n%s", data[i].Dept);
                        }
                    }
    
                    else if (tempCourse != '0'){
                        if (tempCourse = data[i].course){
                        printf("\n%d", data[i].course);
                        }
                    }
                }
    
            break;

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The easiest thing to do is record the value returned by scanf call.
    scanf - C++ Reference

    Next easiest is shown in code segment below

    Code:
    /* set to known value before scanf */
    tempDept[0]='\0'  /* set tempDept value to a C Nul string */
    
    /* after scanf test for known value */
    if(tempDept[0]=='\0')

    Tim S.
    Last edited by stahta01; 04-29-2012 at 04:52 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by stahta01 View Post
    The easiest thing to do is record the value returned by scanf call.
    scanf - C++ Reference

    Next easiest is shown in code segment below

    Code:
    /* set to known value before scanf */
    tempDept[0]='\0'  /* set tempDept value to a C Nul string */
    
    /* after scanf test for known value */
    if(tempDept[0]=='\0')

    Tim S.
    I tried this:
    Code:
    printf("Enter Dept Name: ");
                tempDept[0]='\0';
                scanf("%s", tempDept);
                if (tempDept[0]=='\0');
                printf("Enter Course Number of 0 for any course in department: ");
                scanf("%d", &tempCourse);
    and does not seem to work. Even tried it like:
    Code:
    printf("Enter Dept Name: ");
                tempDept[0]='\0';
                scanf("%s", tempDept);
                if (tempDept[0]=='\0') {
                printf("Enter Course Number of 0 for any course in department: ");
                }
                scanf("%d", &tempCourse);

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest posting the smallest amount of compilable/testable code the demonstrates the problem.

    NOTE: You might try adding a space before the %s to skip newlines. I no idea if this will make a difference; normally it will not.
    Code:
    scanf(" %s", tempDept);
    Tim S.
    Last edited by stahta01; 04-29-2012 at 05:50 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I realized I never stated this line of code DOES NOT work in C.
    Code:
    if (tempDept == "NULL"){
    The use of double quotes around NULL means it is a C-string instead of a C NULL. That means strcmp needs to compare it.

    Note: I have no idea why you are comparing a array name with the value of "NULL" or NULL. I see no reason either makes sense.

    So, is tempDept the name of an array of chars or a pointer?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    This is a scheduling program with many constraints, hence if no department is chosen, move onto course.
    tempDept is an array, not a pointer.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You'll have to use a completely different scanf I think. Something like this...
    Code:
    #include <stdio.h>
    int main()
    {
       int i;
       char text[BUFSIZ];
       char* inputs[] = { "stuff\n", "", "\n", "more stuff\n" };
       for (i = 0; i < sizeof inputs / sizeof *inputs; i++) {
          int result = sscanf(inputs[i], "%[^\n]", text);
          if (result == 1) {
             printf("good: text=\"%s\"\n", text);
          }
          else {
             printf("bad: input=\"%s\"\n", inputs[i]);
          }
       }
       return 0;
    }
    
    /* output:
    good: text="stuff"
    bad: input=""
    bad: input="
    "
    good: text="more stuff"
    */
    C code - 17 lines - codepad

    If you actually use sscanf like I did though, %s will probably work. scanf does a blocking read from the keyboard, which means it will wait as long as it can to read something. Unlike something defined as a string, if scanf stopped reading input just because the user hit return too soon, it could miss whatever input is coming. Compare that to a string: if you run out of string before the whole format string is used, the input is obviously wrong.

    Quote Originally Posted by csharp100
    This is a scheduling program with many constraints, hence if no department is chosen, move onto course.
    Other idea... You could also instruct the user to send EOF in this situation. But you would have to use the return result of scanf to know what happened. But then, return values from input functions are always important!
    Last edited by whiteflags; 04-29-2012 at 07:30 PM.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by whiteflags View Post
    You'll have to use a completely different scanf I think. Something like this...
    Code:
    #include <stdio.h>
    int main()
    {
       int i;
       char text[BUFSIZ];
       char* inputs[] = { "stuff\n", "", "\n", "more stuff\n" };
       for (i = 0; i < sizeof inputs / sizeof *inputs; i++) {
          int result = sscanf(inputs[i], "%[^\n]", text);
          if (result == 1) {
             printf("good: text=\"%s\"\n", text);
          }
          else {
             printf("bad: input=\"%s\"\n", inputs[i]);
          }
       }
       return 0;
    }
    
    /* output:
    good: text="stuff"
    bad: input=""
    bad: input="
    "
    good: text="more stuff"
    */
    C code - 17 lines - codepad

    If you actually use sscanf like I did though, %s will probably work. scanf does a blocking read from the keyboard, which means it will wait as long as it can to read something. Unlike something defined as a string, if scanf stopped reading input just because the user hit return too soon, it could miss whatever input is coming. Compare that to a string: if you run out of string before the whole format string is used, the input is obviously wrong.



    Other idea... You could also instruct the user to send EOF in this situation. But you would have to use the return result of scanf to know what happened. But then, return values from input functions are always important!
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time comparison
    By anaer0bic in forum C Programming
    Replies: 4
    Last Post: 09-18-2010, 06:45 PM
  2. what the comparison does?
    By dpp in forum C Programming
    Replies: 2
    Last Post: 12-12-2009, 04:20 PM
  3. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  4. probably something simple I'm overlooking
    By tomasc in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2008, 11:07 PM
  5. By comparison
    By kahli_mist in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2005, 02:19 PM