Thread: Need Help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Need Help

    I am trying to write a program that will tell whether or not to go to work based on the day of the week entered by the user. Here is what I have so far.


    Code:
    #include <stdio.h>
    #define TRUE 1
    #define FALSE 0
    
    void dayofweek();
    
    int main()
    {	
    	char today[100];
    	int i;
    	
    	enum week {Monday = 0, Tuesday = 1, Wednesday = 2, Thursday = 3, Friday = 4, Saturday = 5, Sunday = 6};
    	
    	printf ("Enter the day of the week (eg. Wednesday) :\n");
    	scanf ("%c", today[]);
    	
    	dayofweek (today);
    	
    	return 0;
    }
    	void dayofweek(char today[])
    {
    	if (today = 0; today < 5; today++)
    	printf ("GO TO WORK!!");
    	
    		else
    		printf("You can rest today");
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    so.... what's the problem? I realized that you're trying to input a string as a char though
    Code:
    scanf ("&#37;c", today[]);
    Even if you wanted to correct that to read a string, you'd need to take the two square brackets away.
    Also, your if statement's syntax is totally wrong. You're obviously mixed up between if and for.
    Assuming you want to input the day of the week as a number (that's what I understand from your enum), you'd need your if to look like this:
    Code:
    if (today >=0 && today <= 5)
        printf("GO TO WORK!!!");
    else
        printf("You can rest today");
    I might not be a pro, but I'm usually right

  3. #3
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    Quote Originally Posted by Abda92 View Post
    Code:
    if (today >=0 && today <= 5)
    work on Saturday?
    yuk :/
    straight off the heap

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    sorry, I just based it on his logic.
    it should be <= 4
    I might not be a pro, but I'm usually right

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Okay. Here is what I have now. I am getting a parse error before ";" token on line 36.

    I am trying to enter the day of the week ie. Monday and get the results to return as GO TO WORK or you can stay home.



    Code:
    #include <stdio.h>
    
    void dayofweek();
    
    int main()
    {	
    	
    	char today[100];
    	int i;
    	
    	enum week {Monday = 0, Tuesday = 1, Wednesday = 2, Thursday = 3, Friday = 4, Saturday = 5, Sunday = 6};
    	
    	printf ("Enter the day of the week (eg. Wednesday) :\n");
    	
    	scanf ("%s", today);
    	
    	dayofweek (today);
    	
    	return 0;
    	
    }
    
    	void dayofweek(strng)
    	
    {
    	int i;
    	
    	if (i >= 0; i < 5)
    	printf ("GO TO WORK!!");
    	
    	else
    	printf("You can rest today");
    	
    }

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Now I get a successful build but the results always return as You can rest today.

    Code:
    #include <stdio.h>
    
    void dayofweek();
    
    int main()
    {	
    	
    	char today[100];
    	int i;
    	
    	enum week {Monday = 0, Tuesday = 1, Wednesday = 2, Thursday = 3, Friday = 4, Saturday = 5, Sunday = 6};
    	
    	printf ("Enter the day of the week (eg. Wednesday) :\n");
    	
    	scanf ("%s", today);
    	
    	dayofweek (today);
    	
    	return 0;
    	
    }
    
    	void dayofweek(strng)
    	
    {
    	char today;
    	
    	if (today >= 0 && today <= 4)
    	printf("GO TO WORK!!");
    	
    	else
    	printf("You can rest today");
    	
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two questions:

    1) Are enumerated values char's or int's? You have today as a char in days of week()

    2) When you print the value of "today" in days of week(), what do you see?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    I thought enum values could be char. I could be wrong, I am new to c prog.

    Also, no matter what I put is as the user response, I get the same results "You can rest today"

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Now this is were I am at. Still getting the same results regardless of user entry.

    Code:
    #include <stdio.h>
    #include <string.h>
    void dayofweek();
    
    int main()
    {	
    	char today[15];
    	
    	printf ("Enter the day of the week (eg. Wednesday) :\n");
    	
    	scanf ("&#37;c", &today);
    	
    	enum week {Saturday = 0, Sunday = 0, Monday = 1, Tuesday, Wednesday, Thursday, Friday};
    	
    	int i = *today;
    	
    	dayofweek (*today);
    
    	return 0;
    	
    }
    
    	void dayofweek(int *today)
    	
    {
    	int i;
    	
    	if (i != 0)
    	printf("GO TO WORK!!");
    	
    	if (i = 0)
    	printf("You can rest today");
    	
    }

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by jhp1025 View Post
    Now this is were I am at. Still getting the same results regardless of user entry.

    Code:
    #include <stdio.h>
    #include <string.h>
    void dayofweek();
    
    int main()
    {	
    	char today[15];
    	
    	printf ("Enter the day of the week (eg. Wednesday) :\n");
    	
    	scanf ("%c", &today);
    	
    	enum week {Saturday = 0, Sunday = 0, Monday = 1, Tuesday, Wednesday, Thursday, Friday};
    	
    	int i = *today;
    	
    	dayofweek (*today);
    
    	return 0;
    	
    }
    
    	void dayofweek(int *today)
    	
    {
    	int i;
    	
    	if (i != 0)
    	printf("GO TO WORK!!");
    	
    	if (i = 0)
    	printf("You can rest today");
    	
    }
    Tell me - how do you logically justify passing a value to a function, but not using it in the function?

    You need to pass the value to the function, and then check that value.

  11. #11
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Well, I haven't tested this, but from what you just posted I can give you a few pointers.

    Your function prototype at the top does not match the actual function that you have made. You need to put the arguments in the prototype that you also have in your function.

    In your main function, you call your dayofweek() function with *today, but that datatype doesn't match what you have declared for your function argument, int *. You most likely will get a warning for this.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    This is where im at now. Same results for anything entered as user. Stil GO TO WORK for anythinmg entered.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LSIZE 15
    void week();
    
    int main()
    {	
    	char today[LSIZE];
    
    	enum week {Saturday = 0, Sunday = 0, Monday = 1, Tuesday, Wednesday, Thursday, Friday};
    
    	printf ("Enter the day of the week (eg. Wednesday) :\n");
    
    	week (today);
    	
    	sscanf ("&#37;s", today);
    
    	if (today == 0)
    	printf("You can rest today");
    	
    	if (today != 0)
    	printf("GO TO WORK!!");
    	
    	return 0;
    	
    }
    
    	void week(char strng[])
    	
    {	 
    	int i =0;
    	char c;
    	
    	while (i < (LSIZE-1) && (c = getchar()) !='\n')
    	
    	{
    		strng[i] = c;
    		i++;
    	}
    	
    	strng[i] = '\0';
    		
    }

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You do realize you compare a pointer to a string (that's what today is) with 0? You need to compare the string that is input with the strings "Monday", "Tuesday", etc.

    And that sscanf shouldn't compile.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Im not following

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just defining the enum doesn't do any of the work in comparing what's typed in to the names of the days. You'll have to do it yourself.

Popular pages Recent additions subscribe to a feed