Thread: Program not working as expected

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Program not working as expected

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(){
    	
    	char changeschedule;
    	char moviename[50];
    	int showtime;
    	int screennumber;
    	char addanothermovie;
    	char mname[50];
    	printf("Do you want to change the movie schedule? (Y for Yes, N for No)");
    	scanf("%c", &changeschedule);
    	if (changeschedule == 'Y'){
    		do{
    			printf("Enter the name of the movie:");
    			fgets(moviename, 50, stdin);
    			printf("Enter the showtime of the movie:");
    			scanf("%d", &showtime);
    			printf("Enter the screen number of the movie:");
    			scanf("%d", &screennumber);
    			printf("Do you want to add another movie to movie schedule? (Y for Yes, N for No)");
    			scanf("%c", &addanothermovie);
    		}while (addanothermovie == 'Y');
    	}
    	printf("Enter the name of the movie you want to buy ticket(s) for:");
    	fgets(mname, 50, stdin);
    	return 0;
    }
    The program is outputting:
    Do you want to change the movie schedule (Y for Yes, N for No) Y
    Enter the name of the movie:Enter the showtime of the movie:1230
    Enter the screen number of the movie:3
    Do you want to add another movie to movie schedule (Y for Yes, N for No)Enter t
    he name of the movie you want to buy ticket(s) for:The Social Network

    Why is the program not letting me enter the name of the movie?
    And why is the program not letting me say Yes or No to specify whether I want to add another movie to movie schedule?

    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add getchar(), after your scanf(), before the fgets() for the name of the movie. It's taking the left over newline, and saying "OK, I've got my name - moving on!".

    Do that for both scanf()'s. It always leaves the newline char: '\n' behind in the keyboard buffer.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Or ditch scanf() and use fgets() for all input.

    Then use sscanf() on the buffer in memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Thank you

    Thank you. Your solutions worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-07-2008, 11:38 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Program not working
    By jat421 in forum C Programming
    Replies: 6
    Last Post: 03-20-2005, 08:28 PM
  5. Program logic not working..
    By ronkane in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2002, 08:31 PM