Thread: Looping Program

  1. #1
    Registered User Melon's Avatar
    Join Date
    Jan 2003
    Posts
    13

    Looping Program

    Hello everyone,
    I'm pretty new to C programing and don't know too much about anything. My problem is that I need to write a program that at the end should ask the user if they want to run the program once more, with a yes or no. Any help would be great. Thanks.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    This problem's really simple to solve - do you have any code to show us?

  3. #3
    Registered User Melon's Avatar
    Join Date
    Jan 2003
    Posts
    13
    No I don't. I don't even know how to attempt to make such an action occur. This is only my 2nd week of programing. I'm attempting to write a simple program to be used for naming dogs based on gender and the breed of dog. That was not hard at all, but getting the program to repeat so that the user can try to find a different name is what I have no idea what to do. I can send my current program code if that would help.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to encase your main code is a loop. Something like this:

    Code:
    looping = true
    
    while (looping)
      do stuff
      ask user if they want to exit
      if YES: looping = false
    end while
    Which bit are you having trouble with?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    hi there.

    http://cboard.cprogramming.com/showt...threadid=33349

    I think the above thread might help. it will scanf a yes or no answer then return the function necessary. As for looping you can try a for loop or a while loop.
    there are only 10 people in the world, those who know binary and those who dont

  6. #6
    Registered User Melon's Avatar
    Join Date
    Jan 2003
    Posts
    13
    Hey thanks everybody for you help. I figured the problem out with the help of one of my friends. I used a "char flag" command, where "flag = 'Y'. That worked great. I'm on my home computer so don't have the code in front of me, but if anyone wants to see it let me know and I can post it tomorrow. Thanks for help though everyone, I never tried the code given by Hammer so it might work. Thanks again everyone, I'm sure I'll be asking for help again soon.

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > if anyone wants to see it let me know and I can post it tomorrow.

    Sure, why don't you put it up here - maybe we can still offer input.

  8. #8
    Registered User Melon's Avatar
    Join Date
    Jan 2003
    Posts
    13
    I'm really happy you asked me to post my code, because when I went back over it, it didn't work at all. Now I have two problems that I don't know how to fix. First off, when I run the program and enter breed of dog, I get all three names for the dog instead of just the one. Second, the program loops just fine, but will not terminate when flag is changed to anything else. Let me know what you guys see wrong.
    Code:
    /* Dog naming program */
    #include <stdio.h>
    
    int main (void)
    {
    	char gender, breed, temp, flag;				/* declaration */
    	flag = 'Y';
    	while( flag = 'Y' ) { 
    	
    	/* processing phase */
    	
    	printf( "Let's pick out a name for your dog.\n" );	/* prompt */
    	printf( "What gender is your dog? Enter 'm'\n");
    	printf( "for male and 'f' for female: " );
    	scanf( "%d", &gender );					/* read gender */
    	temp = getchar ();					/* clear char */
    	
    	printf( "What breed of dog do you have?\n" );		/* prompt */
    	printf( "Enter 'c' for Collie, 's' for\n" );
    	printf( "St. Bernard and 'd' for Dalmation.\n" );
    	scanf( "%d", &breed );					/* read breed */
    	temp = getchar ();					/* clear char */
    	
    	if ( gender = 'm' ) {
    	
    	if ( breed = 'c' )
    		printf( "Try naming your dog Weinberg.\n" );
    	if ( breed = 's' )
    		printf( "Try naming your dog Gunther.\n" );
    	if ( breed = 'd' )
    		printf( "Try naming your dog Spots.\n" ); 
    		
    	}
    	
    	else {
    	
    	if ( breed = 'c' )
    		printf( "Try naming your dog Rosie.\n" );
    	if ( breed = 's' )
    		printf( "Try naming your dog Heidi.\n" );
    	if ( breed = 'd' )
    		printf( "Try naming your dog Princess.\n" );
    	}
    	
    	/* termination phase */
    	
    	printf( "Would you like me to try to give\n" );		/* prompt */
    	printf( "a dog a new name? 'Y' or 'N'?\n" );
    	scanf( "%c", &flag );					/* read flag */
    	temp = getchar ();					/* clear char */
    }
    	return 0;
    	}

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    when your checking the breed and the flag use == not =

    i.e. while( flag == 'Y' )

    you might also want to make sure there is no confusion between 'y' and 'Y' or 'n' and 'N' you can use toupper( flag )
    it will make the letter become the upper case version so wether they type y or Y it will become Y.

    Hope this helps.
    T

  10. #10
    Registered User Melon's Avatar
    Join Date
    Jan 2003
    Posts
    13
    okay i put in == for my =, and now no names show up and it always terminates. the 'y' and 'Y' is most likely the problem, but where do I insert the toupper( flag )?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    Sorry I should of said that you need to include a specila header file to use toupper() :

    #include <ctype.h>

    and you can use to uppper as soon as you have the character ie.
    scanf( ... ); /* You have new got the character */
    toupper( the character ); /* you can now upper case it */

    you might also replace the above with something like this-

    flag = toupper( getch() );

    this makes flag hold the key pressed in its upper case format.

    getch() is in another header file-
    #include <conio.h>

    Hope this helps
    T

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please don't advise the use of getch(), its a non-standard function.

    @Melon, change all of these:
    temp = getchar();

    for these:
    while (getchar() != '\n');

    Your input buffer isn't being cleared fully, the while loop will do that for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User Melon's Avatar
    Join Date
    Jan 2003
    Posts
    13
    I really appreciate everyone's help on this problem of mine. Once again I'm quite new to programming and your help is always awesome. The thing is, my problems still exist. My program will not give a name for the dog choosen and will not loop. I swear this is the last time I'll ask about this question, and just fake the program instead. Here is my current, and possibly final code.
    Code:
    /* Dog naming program */
    #include <stdio.h>
    #include <ctype.h>
    
    int main (void)
    {
    	char gender, breed, flag;				/* declaration */
    	flag = 'Y';
    	while( flag == 'Y' ) { 
    	
    	/* processing phase */
    	
    	printf( "Let's pick out a name for your dog.\n" );	/* prompt */
    	printf( "What gender is your dog? Enter 'm'\n");
    	printf( "for male and 'f' for female: " );
    	scanf( "%d", &gender );					/* read gender */
    	while (getchar() != '\n');				/* clear char */
    	
    	printf( "What breed of dog do you have?\n" );		/* prompt */
    	printf( "Enter 'c' for Collie, 's' for\n" );
    	printf( "St. Bernard and 'd' for Dalmation.\n" );
    	scanf( "%d", &breed );					/* read breed */
    	while (getchar() != '\n');				/* clear char */
    	
    	if ( gender == 'm' ) {
    	
    	if ( breed == 'c' )
    		printf( "Try naming your dog Weinberg.\n" );
    	if ( breed == 's' )
    		printf( "Try naming your dog Gunther.\n" );
    	if ( breed == 'd' )
    		printf( "Try naming your dog Spots.\n" ); 
    		
    	}
    	
    	else {
    	
    	if ( breed == 'c' )
    		printf( "Try naming your dog Rosie.\n" );
    	if ( breed == 's' )
    		printf( "Try naming your dog Heidi.\n" );
    	if ( breed == 'd' )
    		printf( "Try naming your dog Princess.\n" );
    	}
    	
    	/* termination phase */
    	
    	printf( "Would you like to try again?" );		/* prompt */
    	printf ( "'Y' or 'N'?\n" );
    	scanf( "%c", &flag );					/* read flag */
    	toupper( flag );
    	while (getchar() != '\n');				/* clear char */
    }
    	return 0;
    	}
    Thanks again for all your help everyone.
    Ten out of ten people die, so don't take life so seriously.

  14. #14
    Unreg1
    Guest
    You're using %d when you should be using %c, in here:
    scanf("%d", &gender);
    and other places.

    Make it
    scanf("%c", &gender);

  15. #15
    Registered User Melon's Avatar
    Join Date
    Jan 2003
    Posts
    13
    I just want to thnk everyone that has helped me with my problem. The program runs great now.
    Ten out of ten people die, so don't take life so seriously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not looping
    By abs.emailverify in forum C Programming
    Replies: 9
    Last Post: 11-09-2006, 02:51 AM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM