-
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;
}
-
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. :cool:
-
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
-
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
-
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 :$
-
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.
-
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 :$
-
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");
-
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).
-
-
I have restored the "deleted" posts.
Thread closed