Small looping problem

This is a discussion on Small looping problem within the C Programming forums, part of the General Programming Boards category; Sorry if you have seen this code already, but I have fixed it to almost work perfectly. My issue this ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Small looping problem

    Sorry if you have seen this code already, but I have fixed it to almost work perfectly. My issue this time, is that, when A character is entered exceeding 9 characters, my program does not proceed to the appropreate loop.

    Example of error:
    Code:
    Enter words no longer than 9 characters
    Enter your 1st word :Bill
    Enter your 2nd word :thiswillbemorethannine
    Enter your 3rd word :Enter your 4th word :
    The character checking loop is as follows;
    Code:
    for (b=0; b<10; b++)
    		{
    			buff[a][b]=fgetc(stdin);
    			if (buff[a][b]=='\n')
    			{
    				buff[a][b]='\0';
    				break;
    			}
    
    			if (b>=10)
    			{
    				printf("You have entered a word more than 9 characters\n");
    					while((buff[a][b] = fgetc(stdin))!='\n')
    					{
    						a=-1;
    						goto A;
    					}
    			}
    
    		}
    Maybe I miss understand how loops work, but when 'b' gets increased to 9, the loop runs, then does it get increased to 10, and not follow through with the loop because b<10 is FALSE? Thats my logic of it, so since b = 10 now, it should do my ERROR loop.

    I know for a fact the loop above doesn't remove all the information in the buffer, so I don't exactly know how to do that either. (I can't use fflush)


    Here is my program;
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char word[10][19];
    char buff[10][19];
    int a,b,c,d,e;
    
    int main ()
    {
    printf("Enter words no longer than 9 characters\n");
    A:for (a=0; a<10; a++)
    {
    	if (a==0)
    		printf("Enter your 1st word:");
    	else if (a==1)
    		printf("Enter your 2nd word:");
    	else if (a==2)
    		printf("Enter your 3rd word:");
    	else
    		printf("Enter your %dth word:", a+1);
    
    		for (b=0; b<10; b++)
    		{
    			buff[a][b]=fgetc(stdin);
    			if (buff[a][b]=='\n')
    			{
    				buff[a][b]='\0';
    				break;
    			}
    
    			if (b>=10)
    			{
    				printf("You have entered a word more than 9 characters\n");
    					while((buff[a][b] = fgetc(stdin))!='\n')
    					{
    						a=-1;
    						goto A;
    					}
    			}
    
    		}
    		for (b=0; buff[a][b]!='\0'; b++)
    			if (buff[a][b] >='a' && buff[a][b]<='z')
    				buff[a][b]=buff[a][b] - 'a' + 'A';
    			else if (buff[a][b] >='A' && buff[a][b]<='Z')
    				buff[a][b]=buff[a][b] - 'A' + 'a';
    		b--;
    		for(c=0; b>=0; b--, c++)
    			word[a][c]=buff[a][b];
    		for(b=0; buff[a][b]!='\0'; b++, c++)
    			word[a][c]=buff[a][b];
    		word[a][c]='\0';
    
    }
    printf("Your unsorted list of words\n");
    for (e=0; e<10; e++)
    	printf("%s \n",word[e]);
    
    
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Iconate View Post
    Sorry if you have seen this code already, but I have fixed it to almost work perfectly. My issue this time, is that, when A character is entered exceeding 9 characters, my program does not proceed to the appropreate loop.

    Example of error:
    Code:
    Enter words no longer than 9 characters
    Enter your 1st word :Bill
    Enter your 2nd word :thiswillbemorethannine
    Enter your 3rd word :Enter your 4th word :
    The character checking loop is as follows;
    Code:
    for (b=0; b<10; b++)
    		{
    			buff[a][b]=fgetc(stdin);
    			if (buff[a][b]=='\n')
    			{
    				buff[a][b]='\0';
    				break;
    			}
    
    			if (b>=10)  /*b is never >= 10, it quits the loop, after 9*/
    			{
    				printf("You have entered a word more than 9 characters\n");
    					while((buff[a][b] = fgetc(stdin))!='\n')
    					{
    						a=-1;
    						goto A;
    					}
    			}
    
    		}
    Maybe I miss understand how loops work, but when 'b' gets increased to 9, the loop runs, then does it get increased to 10, and not follow through with the loop because b<10 is FALSE? Thats my logic of it, so since b = 10 now, it should do my ERROR loop.

    I know for a fact the loop above doesn't remove all the information in the buffer, so I don't exactly know how to do that either. (I can't use fflush)


    [/code]
    The variable b will loop from 0 to 9, not ten. Your if statement about b >= 10, is never reached as the program runs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with looping.
    By rina in forum C Programming
    Replies: 9
    Last Post: 10-05-2005, 02:21 AM
  2. Small program big problem :-(
    By Fibre Optic in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2005, 09:53 AM
  3. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 12:03 PM
  4. problem with looping
    By bajanstar in forum C Programming
    Replies: 7
    Last Post: 03-09-2005, 01:40 PM
  5. Looping Problem
    By simly01 in forum Windows Programming
    Replies: 1
    Last Post: 06-28-2002, 02:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21