Thread: a do...while loop is the right answer??

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    51

    a do...while loop is the right answer??

    I am working through the C tutorials and in Lesson 8 Arrays a code is given to find if the char 'a' is in an input string. I am trying to modify the code a little to keep asking until 'a' is found in the input string.
    I thought do...code, while astring[i] != 'a';
    I take this to mean if there is no 'a' in the string elements, loop the program, if there is then end.
    The loop works but even if 'a' is found, it loops again.
    is the problem my while condition?
    Code:
    #include <stdio.h>
    
    int main(){
    	
    	char astring[10];	//declare the array
    	int i = 0;			//initialise the variable
    	
    	do {
    		printf("\nEnter a word:\n");
    		scanf("%s", astring);			//put the string in a memory location, no &
    		
    		for (i = 0; i <10; ++i) {		// for (initial; condition; increment)
    			if (astring[i] == 'a') {	// if onr of the elemnts contain 'a'
    				printf("Your word contains a!\n");
    			}
    		}
    		
    	} while (astring[i] != 'a');
    	}

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    You may want to run a loop like:

    Code:
    i=0;
    while(astring[i] != 'a' && astring[i] != '\0') i++;
    then you can just check if i is equal to strlen(astring) you didn't find 'a', else you found it

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Look:
    Code:
    		for (i = 0; i <10; ++i) {		// for (initial; condition; increment)
                    [...............]
    	} while (astring[i] != 'a');
    What will be the value of i at the end of the do..while(), when the condition is executed?

    This loop will repeat infinitely unless astring[9] happens to be an a. Also, you should be using strlen() to set the condition for the for() loop rather than using the array size.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Thanks, strlen is beyond my knowledge at the moment. I was just trying to take a simple code showing how arrays work and make it loop until it found "a".

    What will be the value of i at the end of the do..while(), when the condition is executed?
    Your right, I don't know. I just thought it might be the way. I will research strlen and have a go at it that way,

    Thanks for the input.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    You really don't need to use strlen(), it was just a tip, if you still don't know it you can just do something like:
    Code:
    i=0;
    while(astring[i] != 'a' && astring[i] != '\0') i++;
    // you just need to check if astring[i] == 'a' or not by using an if..else statement
    In fact when the loop breaks astring[i] will be either 'a' or the '\0' character

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry, that was a rhetorical question. The answer is "9", which I thought would be obvious but don't worry, I'll try and explain why:
    Code:
    		for (i = 0; i <10; ++i) {		// for (initial; condition; increment)
                    [...............]
    	} while (astring[i] != 'a');
    What will "i" equal at the end of the for loop? What part of astring do you think "astring[i]" refers to in the while condition? Remember, the for loop is nested inside the while loop, so the entire for loop is executed (according to it's conditions) before the while loop finishes the first time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by MK27 View Post
    Look:
    Code:
    		for (i = 0; i <10; ++i) {		// for (initial; condition; increment)
                    [...............]
    	} while (astring[i] != 'a');
    What will be the value of i at the end of the do..while(), when the condition is executed?

    This loop will repeat infinitely unless astring[9] happens to be an a.

    Wait, wouldn't i be 10?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, sorry, I always think ++i will end differently than i++ here but nope.

    So it's 10, not 9. That's even worse, because this is now out of bounds.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Thanks,
    I wasn't too sure where to put the new while statement
    Code:
    while(astring[i] != 'a' && astring[i] != '\0') i++;
    and the if else loop.

    edit:Now it just enters the string twice and the second time prints"you have an a!" regaurdless.
    I need some input as to how to arrange the statements.

    is this the correct order:
    1. enter the string
    2. perform string check with for statement
    3. while != a and != /0
    4. loop again
    5. else print "your word contains an a"

    I know this code below isn't in this order, I posted this before the edit when I thought it was working (before checking a few times)

    Code:
    int main(){
    	
    	char astring[10];	//declare the array
    	int i = 0;			//initialise the variable
    	
    	// while (astring[i] != 'a' && astring[i] != '\0') i++;
    	{
    		printf("\nEnter a word:\n");
    		scanf("%s", astring);			//put the string in a memory location, no &
    		
    		while (astring[i] != 'a' && astring[i] != '\0') i++;
    		{
    		//for (i = 0; i <10; ++i) {		// for (initial; condition; increment)
    			if (astring[i] != 'a') {
    				printf("\nEnter a word:\n");
    				scanf("%s", astring);	
    			}
    			
    			else (astring[i] == 'a'); {	// if onr of the elemnts contain 'a'
    				printf("Your word contains a!\n");
    				
    			}
    		}
    		
    	
    }
    }
    Last edited by dunsta; 04-20-2010 at 04:57 PM. Reason: after error checking found that code ran strange

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    I need help with the if astring==0, break-- else ??? statement
    (I think)

    Code:
    #include<stdio.h>
    int main()
    {
    char astring[10];
    int i=0;
    //do {
    	
    //for(i=0; i<10; ++i)
    while (astring[i] != 'a' && astring[i] != '\0') i++;
    {
    	printf(" enter a word\n");
    scanf("%s", astring);
    	if (astring[i] == 'a')
    	{
    		printf ("you entered an a!\n");
    	//break;
    	}
    }
    //}while (astring[i] != 'a');
    
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while (astring[i] != 'a' && astring[i] != '\0') i++;
    That is the body of that loop.
    Code:
    {
    	printf(" enter a word\n");
    scanf("%s", astring);
    	if (astring[i] == 'a')
    	{
    		printf ("you entered an a!\n");
    	//break;
    	}
    }
    This is its own code block, which has nothing to do with any loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    I still can't get the program to loop back to the start if there is no 'a' character found

    Code:
    #include<stdio.h>
    int main()
    {
    char astring[10];
    int i=0;
    
    	printf(" enter a word\n");
    	scanf("%s", astring);
    	//for(i=0; i<10; ++i){
    	if (astring[i] == 'a'){
    		printf ("you entered an a!\n");
    		}
    	while (astring[i] != 'a' && astring[i] != '\0') i++;
    }
    rob90 advised I use an if...else statement to check if the string contains 'a' or not, but I can't find how to make it loop back to the beginning.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there a reason you don't just use strchr?

    You aren't wrapping the part that reads a string up in your loop:
    Code:
    int main( void )
    {
        ...declare variables...
        do
        {
            printf( "enter a word of 9 or less characters\n" );
            scanf( "%s", astring );
            if( findana( astring ) == FOUNDANA )
                break;
        } while( ...some condition... );
    }
    That's the general idea.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    so I have this code,
    but am looking to break out of it if the string does contain 'a'

    EDIT: got it, finally!!

    Code:
    #include <stdio.h>
    
    int main(){
    	
    	char astring[10];	//declare the array
    	int i = 0;			//initialise the variable
    	
    	do {
    		printf("\nEnter a word:\n");
    		scanf("%s", astring);			//put the string in a memory location, no &
    		
    		for (i = 0; i <10; ++i) {		// for (initial; condition; increment)
    			if (astring[i] == 'a') {	// if onr of the elemnts contain 'a'
    				printf("Your word contains a!\n");
    break;
    			}
    		}
    		
    	} //while (astring[i] != 'a');
    		while (astring[i] != 'a' && astring[i] != '\0'); i++;
    	}
    Last edited by dunsta; 04-20-2010 at 11:52 PM. Reason: found the solution

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    do I read this line
    Code:
     while (astring[i] != 'a' && astring[i] != '\0'); i++;
    like this
    while
    1. != 'a' , no 'a' in any element
    and
    2. != '\0' no null terminator
    3. i++ --- this means "do it again!!" ??????

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  3. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  4. for loop
    By john_murphy69 in forum C Programming
    Replies: 5
    Last Post: 02-05-2003, 01:14 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM