Thread: Looping

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Looping

    I just need help with finishing off this simple program i made that applies the ROT13 process to the string entered. It all works fine but i can't get it to loop properly..

    I want to after running through the program once prompt the user again asking if they would like to enter another string and if so it would loop through the program again.. if not it would end. i have tried a do while loop but it just doesn't work.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    void readin(char str[])
    {
    
    	printf("Enter string:\n ");
    	scanf("%s", str);
    	
    	return;
    	
    }
    			
    void rot13(char str[])
    {
        int i;
        for (i = 0; i < strlen(str); i++)
        {
            if (isupper(str[i]))
            {
                if (str[i] <= 77)
                    str[i] += 13;
                else
                    str[i] -= 13;
            }
            if (islower(str[i]))
            {
                if (str[i] <= 109)
                    str[i] += 13;
                else
                    str[i] -= 13;
            }
        }
    
    	
    	return;
    }
    
    void print(char str[])
    {
    	printf("The String is now: %s\n", str);
    	
    	return;
    }
    
    void goagain(char loop)
    {
    	printf(" would you like to enter another string? y or n:\n ");
    	scanf("%c", &loop);
    	
    	return;
    }
    
    int main()
    {	
    		const int MAXSIZE = 100;
    		char str[MAXSIZE] = {'\0'};
    		char loop;
    		
    		
    	do{
    		readin(str);
    		rot13(str);
    		print(str);
    		goagain(loop);
    		
    	}while(loop != 'n');
    		
    	
    	return(0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The function goagain has no way of affecting the value of loop in your main program. (It can affect the value of its own variable called loop, but those are not the same variable.) You may want to have goagain return the char instead.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    hmmm i tried it just then.. still doesn't work..

    Code:
    char goagain(char loop)
    {
    	printf(" would you like to enter another string? y or n:\n ");
    	scanf("%c", &loop);
    	
    	return(loop);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Changing it there is only half the battle -- did you make the appropriate changes in your main as well?

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    the only change i made was loop = goagain(loop).. not sure what else i can do =\ I'm really stuck, never had this much trouble getting a loop to work.

    Code:
    int main()
    {	
    		const int MAXSIZE = 100;
    		char str[MAXSIZE] = {'\0'};
    		char loop;
    		
    		
    	do{
    		readin(str);
    		rot13(str);
    		print(str);
    		loop = goagain(loop);
    		
    	}while(loop != 'n');
    		
    	
    	return(0);
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Try
    Code:
    scanf(" %c", &loop);
    , which will keep out any stray enter-key presses.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    4
    thanks so much works perfect.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by timeless View Post
    thanks so much works perfect.
    Now that you have it working, why don't you try to refine it...

    Code:
    char goagain(char loop)
    {
    	printf(" would you like to enter another string? y or n:\n ");
    	scanf("%c", &loop);
    	
    	return(loop);
    }
    You aren't using the passed in value inside the function, so there's no need to pass in a value.
    It's bad ju ju to use a variable passed in to hold the results of a calculation, especially one done by pointers.

    So get rewrite the function to not take any parameters and declare a local variable to hold the result of scanf().

    Second returning the character works, but it might be better to return true or false (1 or 0).

    Once you have it doing that, you can move the function call directly into your while statement...

    Code:
    int goagain( void )
    {  char loop;
        printf(" would you like to enter another string? y or n:\n ");
        scanf(" %c", &loop);
        return tolower(loop) == 'y';  // returns true if y or Y false for everything else
    }
    
    
    
    // in main
      do
       {  readin(str);
         rot13(str);
         print(str);
       }while(goagain());
    Half the fun of programming is simplifying and cleaning up code... the time to start forming these good habits is when you are just starting out. The alternative is that later on you will spend half your time fixing up after bad habits...
    Last edited by CommonTater; 05-18-2011 at 11:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Looping
    By Harryt123 in forum C Programming
    Replies: 11
    Last Post: 05-21-2006, 01:16 PM
  2. Looping
    By DeathDealer in forum C++ Programming
    Replies: 8
    Last Post: 02-23-2005, 01:13 PM
  3. looping
    By cnoob in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 07:16 PM
  4. help with looping.
    By tdoctasuess in forum C Programming
    Replies: 1
    Last Post: 05-13-2004, 01:00 PM
  5. looping
    By volk in forum C Programming
    Replies: 14
    Last Post: 01-06-2003, 02:53 PM