Help fixing code

This is a discussion on Help fixing code within the C Programming forums, part of the General Programming Boards category; Can anyone tell me whats wrong with my code? It has some syntax error in line 22,24 and 26 and ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    27

    Help fixing code

    Can anyone tell me whats wrong with my code? It has some syntax error in line 22,24 and 26 and when theres no syntax error it says its the weekend no matter what i type.

    Code:
    #include <stdio.h>
    int main () {
    
    char S, M, T, W, R, F, A, day;
    int hour;
    
    printf("Enter day of Week (S M T W R F A): ");
    scanf("%c", &day);
    
    if (day == S || A)
    printf("It's the weekend!\n");
    
    else if (day != S || M || T || W || R || F || A)
    printf("Invalid day entered.\n");
    
    else if (day == M || T || W || R || F || A || S)
    printf("Enter hour (0-23):\n");
    scanf("%d", &hour);
    
    if (hour >=0 && <8)
    printf("Still asleep\n");
    else if (hour >=8 && <18)
    printf("Should be at work\n");
    else if (hour >=18 && <24)
    printf("Off work\n");
    else printf("Hour Invalid\n");
    return 0;
    }
    Last edited by laserlight; 10-23-2012 at 10:29 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I know this is going to break your heart, but you can't make comparisons like this, in C:
    if (day == S || A)

    Note the single quotes around the letter, and the repeat of day and ==
    if(day == 'S' || day == 'A')

    And Welcome to the forum, Return0.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    oh yeah i forgot about that thanks. i fixed that but i cant figure out the error in line its all the same error it says
    Last edited by return0; 10-23-2012 at 10:11 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need curly braces around this:
    Code:
      else if (day == M || T || W || R || F || A || S) {  //comparisons goofed, and curly brace needed
         printf("Enter hour (0-23):\n");
        scanf("%d", &hour);
     }
    And of course, you'll get errors or warnings until all the comparison code is made correct

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    yeah heres what i have with the correction and the added curly bracers.
    Code:
    #include <stdio.h>
    int main () {
    
    char S, M, T, W, R, F, A, day;
    int hour;
    
    
    printf("Enter day of Week (S M T W R F A): ");
    scanf("%c", &day);
    
    if (day == 'S' ||day == 'A')
    printf("It's the weekend!\n");
    
    else if (day != 'S' ||day != 'M' ||day != 'T'
    ||day != 'W' ||day != 'R' ||day != 'F' ||day != 'A')
    printf("Invalid day entered.\n");
    else if (day == 'M' ||day == 'T' ||day == 'W'
    ||day == 'R' ||day == 'F' ||day == 'A' ||day == 'S') {
    printf("Enter hour (0-23):\n");
    scanf("%d", &hour);
    }
    if (hour >=0 && <8)
    printf("Still asleep\n");
    else if (hour >=8 && <18)
    printf("Should be at work\n");
    else if (hour >=18 && <24)
    printf("Off work\n");
    else printf("Hour Invalid\n");
    return 0;
    }
    am i missing any brackets anywhere else because i keep getting the same error in those lines :$
    Last edited by laserlight; 10-23-2012 at 10:29 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, this is another illegal comparison - it MIGHT be used in another context, but it's not OK, here.
    if (hour >=0 && <8)

    add the second hour in there, and check your other comparisons.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    yeah heres what i have with the correction and the added curly bracers.
    Code:
    #include <stdio.h>
    int main () {
    
    char S, M, T, W, R, F, A, day;
    int hour;
    
    
    printf("Enter day of Week (S M T W R F A): ");
    scanf("%c", &day);
    
    if (day == 'S' ||day == 'A')
    printf("It's the weekend!\n");
    
    else if (day != 'S' ||day != 'M' ||day != 'T'
    ||day != 'W' ||day != 'R' ||day != 'F' ||day != 'A')
    printf("Invalid day entered.\n");
    else if (day == 'M' ||day == 'T' ||day == 'W'
    ||day == 'R' ||day == 'F' ||day == 'A' ||day == 'S') {
    printf("Enter hour (0-23):\n");
    scanf("%d", &hour);
    }
    if (hour >=0 && <8)
    printf("Still asleep\n");
    else if (hour >=8 && <18)
    printf("Should be at work\n");
    else if (hour >=18 && <24)
    printf("Off work\n");
    else printf("Hour Invalid\n");
    return 0;
    }
    am i missing any brackets anywhere else because i keep getting the same error in those lines :$
    Last edited by laserlight; 10-23-2012 at 10:30 PM.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    actually when i enter a weekday it says invalid day entered too for some reason i cant figure out how to fix it.

    it seems its not registering the else if statements after

    Code:
    if (day == 'S' ||day == 'A')
    printf("It's the weekend!\n");
    Last edited by laserlight; 10-23-2012 at 10:30 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your comparison of days,!= to, change the || to &&.

    Here's a big tip for new programmers in C. When you use scanf() or fscanf() and you are trying to store a char, put a space just before the %c:

    scanf(" %c", &mychar); //note the space before the %c

    You'll avoid troublesome newlines that way. (not needed for int's, or floats, by the way).
    Last edited by Adak; 10-23-2012 at 10:07 PM.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Whoops wrong thread

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    I have restored the "deleted" posts.

    Thread closed
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need some help fixing this code.
    By Mremokid1 in forum C Programming
    Replies: 4
    Last Post: 11-06-2011, 10:00 PM
  2. help in fixing this code..
    By transgalactic2 in forum C Programming
    Replies: 20
    Last Post: 12-13-2008, 05:38 AM
  3. help!!! code needs fixing
    By hockey123 in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2008, 06:02 PM
  4. Fixing old C-style code
    By tunafish in forum C Programming
    Replies: 6
    Last Post: 07-01-2005, 07:24 AM
  5. need help fixing up this code in allegro
    By Bobish in forum Game Programming
    Replies: 11
    Last Post: 04-06-2002, 01:50 PM

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