Closed Thread
Results 1 to 8 of 8

Thread: Looping problem ( C )

  1. #1
    Join Date
    Feb 2009
    Posts
    10
    Rep Power
    0

    Looping problem ( C )

    I have a problem with my main while loop.

    Code:
    int main()
    {
    	
    	int number;
    	int blank;
    	int length = 0;
    	int keepGoing = 89;
    	
    	while (keepGoing == 89 || keepGoing == 121)
    	{
    		blank = 0;
    		number = getchar();
    		if (number == 32)
    		{
    			blank = 1;
    		}
    		while (blank != 1)
    		{
    			if (number >= 48 && number <= 57)
    			{
    				putchar(number);
    				length++;
    				break;
    			}
    		}
    	printf("\nWould you like to enter another equation? y/n \n");
    	keepGoing = getchar();
    	}
    
    
    
    return 0;
    }
    My problem is that the main while loop skips over the

    Code:
    keepGoing = getchar();
    and just ends. I have no idea what could be wrong as the code seems just fine to me. (But obviously it isn't or I wouldn't be asking this.)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Phoenixz is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    256
    Blog Entries
    1
    Rep Power
    13

    Re: Looping problem ( C )

    Well, fflush(stdin); is handy for clearing the buffer after a scanf, but I just noticed you don't have one at all in it, even after asking the question so theres no way to read in what they put?

    I'm not sure why you'd want keepGoing = getchar(); but as far as I know.. that's kinda not do-able? to assign a command to an integer?

    Rather do it on seperate lines.

  4. #3
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: Looping problem ( C )

    It doesn't skip over that line. getchar returns a char and you declared number as int. So if you input 8 say it will return 56. Check the ascii chart. You should probably just use scanf. Put in a print statement to see whats actually going on.
    Perfection of means and confusion of ends seem to characterize our age. Albert Einstein

  5. #4
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: Looping problem ( C )

    If its absolutely necessary that you use getchar you can subtract and get the int value:

    Code:
    number = getchar();
    number = number - 48;
    Perfection of means and confusion of ends seem to characterize our age. Albert Einstein

  6. #5
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: Looping problem ( C )

    as an aside, you also aren't handling upper case characters.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Join Date
    Feb 2009
    Posts
    10
    Rep Power
    0

    Re: Looping problem ( C )

    Let me just explain sort of what I'm doing here, because I'm not sure people really understand that. (Not anyone's fault, I never explained it).

    This is the beginning of a basic calculator application. This is the part to get the input, however, our teacher wants us to use getchar() and putchar() for inputting and outputting numbers.

    This loop checks to make sure the input is a valid statement of:

    x *operator* y

    There needs to be an operand before the operator and an operand after the operator. But there can be any amount of spaces in between that data. So that's what this loop is checking for.

    Obviously that loop hasn't gotten to that point yet, I'm just figuring out why the keepGoing = getchar(); is not working correctly. And I have tried changing keepGoing to a char.

    :EDIT:

    I've just discovered something interesting. While the variable keepGoing is being tested in the while loop the keepGoing = getchar(); line just seems to be skipped. However, when I tried another variable that wasn't tested at all. It worked fine. And when I then tested that variable in the while loop, it would get skipped again.
    EX:
    Code:
    char a = 'y';
    char b = 'y';
    
    while (a == 'y')
    {
         ...
    
         a = getchar();
    }
    That "skips" a = getchar();. However, when I write:
    Code:
    char a = 'y';
    char b = 'y';
    
    while a == 'y')
    {
         ...
    
         b = getchar();
    }
    the program actually waits for my input.

    Does that make any sense to anyone?
    Last edited by lithiummethoxide; 03-21-2009 at 06:09 PM. Reason: Needed to update information on problem.

  8. #7
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: Looping problem ( C )

    I have had problems with printchar before. I think i grabs the enter after you type a char. Im not sure how to explain this problem to you, but ill show you how i usually fix it(patch).

    Code:
    int main(){
    	
    int number1,number2,dump;
        /*Simple infinite loop prompting for values and outputting them*/	
    while(1){
        puts("\nEnter number1\n");         
        number1 = getchar();
        dump = getchar(); //using dump to store \n
        printf("this is number1\n");   
        putchar(number1);
        
        puts("\nEnter number2"); 
        number2 = getchar();
        dump = getchar();   //using dump to store \n
        printf("this is number2\n");    
        putchar(number2);
        }
    return 0;
    Now comment out the two instance of
    Code:
    dump = getchar();
    and see how it behaves different.
    Ived used this patch for quite some time. To lazy to actually research. A proper explanation would be interesting.
    Perfection of means and confusion of ends seem to characterize our age. Albert Einstein

  9. #8
    Join Date
    Feb 2009
    Posts
    10
    Rep Power
    0

    Re: Looping problem ( C )

    Ah, thanks for that little workaround. I'll try that. Maybe a simple flush of the buffer might fix the problem...

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. need help with some looping
    By BRUTAL in forum Python
    Replies: 2
    Last Post: 01-24-2011, 02:38 PM
  2. need looping help
    By BRUTAL in forum Python
    Replies: 2
    Last Post: 01-22-2011, 07:57 AM
  3. help with looping
    By Root23 in forum Python
    Replies: 7
    Last Post: 06-10-2010, 08:08 PM
  4. Explain looping
    By digitalbeef in forum C# Programming
    Replies: 1
    Last Post: 11-01-2009, 09:28 PM
  5. Looping statement problem : TOTAL Course fee
    By syarif20 in forum C and C++
    Replies: 8
    Last Post: 09-12-2009, 02:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts