Thread: C Programming Assignment Problem

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    C Programming Assignment Problem

    I have a programming assignment where the user selects from pre-defined choices for a day of the week (S-Sunday, R-Thursday, A-Saturday, etc.) and then if it is a weekend, it prints back it's the weekend, if a week day and between 8-17 (8AM - 5PM) it says at work, and if before 8AM, it says asleep, then anything else is off work. It will also display the total number of times for errors, weekends, sleep, etc, but I haven't added that in yet as I'm having problems with the primary function. It works fine for the first two selections (Max of 5 correct selections before it terminates) then after that it goes to the default else of invalid day, even if I go back and type in one that worked previously. I've tried numerous reworkings so far, but if anyone sees a error, and can fix this, I would greatly appreciate it.

    Code:
    // Program Two CSE 1030 Alexander Hollis email csp01
    
    
    
    
    
    
    
    
    
    #define OUTPUT printf
    
    
    
    
    #include <stdio.h>
    
    
    
    
    int main() {
    
    
        
        char choice[2+1];
        int STOP=0;
        int count=0,hour,weekends=0,
        weekdays=0,invalidD=0,atwork=0,
        asleep=0,offwork=0,invalidH=0;
        
        OUTPUT("Program Two CSE 1030 Alexander Hollis email csp01\n\n");
        
        while(count <5) {
    
    
            OUTPUT("Enter day of week (S M T W R F A): ");
            choice[0]=getchar();
            choice[1]=getchar();
            printf("You selected: %c",&choice[0]);        
            
    
    
    
    
                
                if(choice[0] == 'S' || choice[0]=='A')
                {
                    OUTPUT("\nIt's the weekend!\n");
                    weekends++;
                    count++;
                }
                
                else
                {
                
                if(choice[0]=='M' || choice[0]=='T' || choice[0]=='W' || choice[0]=='R' || choice[0]=='F')
                {
                    while(STOP == 0)
                    {
                    OUTPUT("Enter hour (0-23): ");
                    scanf("%d",&hour);
                    if(hour >= 0 && hour <=23)
                    {
                        if(hour >=8 && hour <=17) 
                        {    
                            OUTPUT("\nShould be at work.\n");
                            atwork++;
                        }
                        else
                           if(hour >= 0 && hour < 8)
                           {
                               OUTPUT("\nStill Asleep.\n");
                               asleep++;
                           }
                           else 
                           {
                               OUTPUT("\nOff Work\n");
                               offwork++;
                           }
                    count++;
                    STOP =1;
                    }
                    else
                    {
                        OUTPUT("\nInvalid hour entered. Try Again.\n");
                        invalidH++;
                    }
                    }
                    weekdays++;
                }
                else if(choice[0] !='S' || choice[0] !='A')
                {
                    OUTPUT("Invalid Day!\n");
                    invalidD++;
                }
                }
                    
                fflush(stdin);
            }
    
    
        return 0;
    
    
        }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Sounds like it's an issue with the way you read in input. There are several things to work on here, some of which will probably fix your problem:
    • Avoid excessive white space and blank lines in your code. Too much space is just as bad as code that is too crammed.
    • Don't define a macro called OUTPUT just to replace the word printf. It adds nothing to your code and actually costs you by making your code harder to read/understand.
    • fflush(stdin) results in undefined behavior. Read these two links: Cprogramming.com FAQ > Why fflush(stdin) is wrong and Cprogramming.com FAQ > Flush the input buffer.
    • There's no need to declare choice as an array of 3 chars. Make it a single char, read it in with getchar and discard everything after it until the new line (flush the input buffer as per the second link above).
    • Drop the & from &choice[0] in your printf statement.
    • You need to reset STOP to zero each time through your main while loop. Better yet, get rid of the stop variable and use a do while loop for gathering input (example below)
    • You could improve your control flow for handling weekends, weekdays and invalid days by using the else if construct (example below)

    do while example
    Code:
    do {
        // ask user for input and read in hour
        if (hour >= 0 && hour <= 23)
        {
            if (hour >= 8 && hour <= 17)
            ...
        }
        else
        {
            // print invalid time
        }
    } while (hour < 0 || hour > 23);
    else if example
    Code:
    if (choice == 'S' || choice == 'A')
    {
        // weekend
    }
    else if (choice == 'M' || choice == 'T' || ... || choice == 'F')
    {
        // weekday
    }
    else
    {
        // invalid date
    }

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    Updated Code

    I went and applied the fixes, it works now, but it pauses with that flush code, is there a better place for the flush while command?

    Code:
    // Program Two CSE 1030 Alexander Hollis [email protected] csp01
    
    
    
    
    
    
    
    
    
    #define OUTPUT printf
    
    
    
    
    #include <stdio.h>
    
    
    
    
    int main() {
    
    
    	
    	char choice, enter;
    	int ch;
    	int count=0,hour,weekends=0,
    	weekdays=0,invalidD=0,atwork=0,
    	asleep=0,offwork=0,invalidH=0;
    	
    	OUTPUT("Program Two CSE 1030 Alexander Hollis [email protected] csp01\n\n");
    	
    	while(count <5) {
    		OUTPUT("Enter day of week (S M T W R F A): ");
    		choice=getchar();
    		enter=getchar();
    		printf("You selected: %c",choice);		
    		
    
    
    
    
    			
    			if(choice == 'S' || choice == 'A')
    			{
    				OUTPUT("\nIt's the weekend!\n");
    				weekends++;
    				count++;
    			}
    			
    			else if(choice == 'M' || choice == 'T' || choice == 'W' || choice == 'R' || choice == 'F') {
    					do{
    					
    				
    					OUTPUT("Enter hour (0-23): ");
    					scanf("%d",&hour);
    					if(hour >= 0 && hour <=23)
    					{
    					if(hour >=8 && hour <=17) 
    					{	
    						OUTPUT("\nShould be at work.\n");
    						atwork++;
    					}
    					else if(hour >= 0 && hour < 8)
    					   {
    						   OUTPUT("\nStill Asleep.\n");
    						   asleep++;
    					   }
    				        else 
    					   {
    						   OUTPUT("\nOff Work\n");
    						   offwork++;
    					   }
    					count++;
    				
    				}
    				else
    				{
    					OUTPUT("\nInvalid hour entered. Try Again.\n");
    					invalidH++;
    				}
    				}while(hour < 0 || hour > 23);
    				weekdays++;
    			}
    			else
    			{
    			OUTPUT("Invalid Day!\n");
    			invalidD++;
    			}
    			while ((ch = getchar()) != '\n' && ch != EOF);		
    		}
    			
    			
    		
    
    
    	return 0;
    
    
    	}
    Last edited by alexhollis; 10-25-2011 at 05:12 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're closer, but you didn't fix everything. When they enter a weekday, you then ask for an hour. They type one in and hit enter. That enter leaves a new line in the input buffer that throws off your next call to getchar. It looks like you didn't read the links about flushing the input buffer. Reread the second one carefully, and properly flush the input buffer. after each scanf or getchar call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming Assignment?
    By CodingNewb in forum C Programming
    Replies: 3
    Last Post: 11-28-2009, 10:02 PM
  2. C programming assignment
    By nebula1 in forum C Programming
    Replies: 5
    Last Post: 10-05-2009, 10:01 PM
  3. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  4. C++ Programming Assignment Help
    By cplusplus12 in forum C++ Programming
    Replies: 6
    Last Post: 02-04-2006, 10:24 PM
  5. programming assignment help
    By the_winky_files in forum C Programming
    Replies: 3
    Last Post: 09-23-2005, 11:59 AM

Tags for this Thread