Thread: repeat

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    repeat

    i have a code that runs fine. but when i want it to start over again it does it twice instead of once.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void seq(void);
    void eturn(void);
    int enemy=9999, you=9999, x;
    int main(){
    	seq();
    }
    void seq(void) {
    		printf("Press any key to attack!, Press h key to heal.\nPress q key to quit!");
    	x=getchar();
    	if(x=='q'){
    		exit(0);
    	}
    	if(x=='h'){
    		you+=500;
    		printf("You healed yourself by 500!\nYou are now at %i\n",you);
    	}
    	else{
    		enemy-=500;
    		printf("You took away 500 from him!\nHe is now at %i health\n",enemy);
    	}
    	eturn();
    }
    void eturn(){
    	if(enemy<=100){
    	        enemy+=200;
    		printf("The enemy healed himself by 500.\nHe is now at %i",enemy);
    	}
    	else{
    		you-=500;
    		printf("The enemy took away 500 health from you!\nYou are now at %i health.\n",you);
    	}				
    	if(you<=0){
    		printf("You Lost!\n");
    		exit(0);
    	}
    	if(enemy<=0){
    		printf("You Won!\n");
    		exit(0);
    	}
    	seq();
    }
    I don't know why it repeats twice. Thanx in advance

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The last statement in eturn() calls seq().

    gg

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I want it to go back to the begining but what it does is go back to the begining runs through the program once and then starts over again then the second time waits for a key hit.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You don't want to use recursion to implement looping logic - that's what loop constructs like "for" and "while" are for.
    Try this type of logic:
    Code:
    WHILE <it's not time to exit>
       seq()
       eturn()
    ENDWHILE
    It may be easier to get user input inside the while loop instead of in seq() or eturn() as well.

    Also, since standard C I/O is buffered, getchar() will not return until the user hits <enter>. So if I type "ab<enter>", the first call to getchar() will return 'a', the second call will return 'b', and the third call will return the <enter> as '\n'.
    Play around with this code to drive it home:
    Code:
    int main(void)
    {
        char c;
        do
        {
            c = getchar();
    
            if (c == '\n')
            {
                printf("You hit <enter>\n\n");
            }
            else
            {
                printf("You hit %c\n",c);
            }
        } while (c != 'q');
        
        return 0;
    }
    Check out the FAQ for examples of how to get unbuffered input - lots of other good stuff in the FAQ too.

    gg

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Instead of using goofy indirect recursion, use a loop. It'd be way easier to follow. Just change main to this and delete the calls to seq and eturn in the other functions
    Code:
    int main(){
        while (1){
            seq();
            eturn();
        }
    }
    The problem with doing something twice is because getchar reads just one character and leaves the enter key in the stream. You can fix it by just testing if x is the enter key, if not call getchar one more time to get rid of it
    Code:
    void seq(void) {
        printf("Press any key to attack!, Press h key to heal.\nPress q key to quit!");
        x=getchar();
        if (x != '\n')
            getchar();
        if(x=='q'){
            exit(0);
        }
        if(x=='h'){
            you+=500;
            printf("You healed yourself by 500!\nYou are now at %i\n",you);
        }
        else{
            enemy-=500;
            printf("You took away 500 from him!\nHe is now at %i health\n",enemy);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Repeat for part of string
    By bertazoid in forum C Programming
    Replies: 6
    Last Post: 10-26-2008, 05:06 PM
  2. vector<...>::iterators and insert - I'm stumped.
    By Dino in forum C++ Programming
    Replies: 6
    Last Post: 12-25-2007, 06:11 AM
  3. Making an Algorithm repeat itself
    By mvd1212 in forum C Programming
    Replies: 13
    Last Post: 12-02-2005, 07:10 PM
  4. how do you repeat cout statements?
    By findme in forum C++ Programming
    Replies: 10
    Last Post: 11-21-2005, 11:34 AM
  5. Help with trying to kill repeat function
    By otchster in forum C Programming
    Replies: 13
    Last Post: 10-25-2005, 09:02 PM