+ Reply to Thread
Results 1 to 8 of 8

Thread: Looping problem ( C )

  1. #1
    Newbie lithiummethoxide is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    10

    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. #2
    Programming Professional Phoenixz will become famous soon enough
    Join Date
    Dec 2008
    Posts
    252
    Blog Entries
    1

    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.

  3. #3
    Programmer fread is on a distinguished road fread's Avatar
    Join Date
    Nov 2008
    Location
    Caribbean
    Age
    25
    Posts
    162

    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

  4. #4
    Programmer fread is on a distinguished road fread's Avatar
    Join Date
    Nov 2008
    Location
    Caribbean
    Age
    25
    Posts
    162

    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

  5. #5
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,640
    Blog Entries
    57

    Re: Looping problem ( C )

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

  6. #6
    Newbie lithiummethoxide is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    10

    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 08:09 PM. Reason: Needed to update information on problem.

  7. #7
    Programmer fread is on a distinguished road fread's Avatar
    Join Date
    Nov 2008
    Location
    Caribbean
    Age
    25
    Posts
    162

    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

  8. #8
    Newbie lithiummethoxide is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    10

    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...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Tutorial: Looping in C#
    By Xav in forum CSharp Tutorials
    Replies: 14
    Last Post: 01-22-2010, 03:21 AM
  2. JNLP saveFileDialog problem
    By d98tp in forum Java Help
    Replies: 0
    Last Post: 03-04-2009, 05:10 AM
  3. Peculiar UI Problem Needs Tackling
    By adriyel in forum C# Programming
    Replies: 2
    Last Post: 04-06-2008, 07:46 AM
  4. Problem read pwd protected Access2K dbase - CR9 & VB6
    By mrbar in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-10-2008, 04:50 AM
  5. How to tackle a programming problem?
    By TcM in forum General Programming
    Replies: 10
    Last Post: 01-07-2008, 11:29 AM

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