Strange output in for loop

Discussion in 'C' started by rakoczimrks, Jul 18, 2011.

  1. rakoczimrks

    rakoczimrks New Member

    Joined:
    Jul 11, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I have the next C code:

    PHP:
    #include <stdio.h>

    int main(void)
    {
        
    char test '\0';       //test the value for ending input
        
    int iCount=0;           //counter in for loop 

        
    for(iCount=0iCount<15 ;iCount++)
        {
            
    printf("\nDo yout want to enter details of a%s car [Y/N]?",
                   
    iCount!=0?"nother":"");
            
    scanf("%c", &test); 
            if(
    tolower(test)=='n')          //testing if n or N was pressed
            
    break;
            
    printf("\nThis is car nr. %d"iCount); //output a statement in every loop
        
    }

        return 
    0;
    }
    I simplified for better understanding, the output it's a little strange because the testing is made on every second loop. I don't understand why. Please help.
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    What is the output you get and how it is strange?
     
  3. rakoczimrks

    rakoczimrks New Member

    Joined:
    Jul 11, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Sorry, i didn't posted my output.
    Here they are:


    Do you want to enter details of a car [Y/N]?y

    This is car nr. 0
    Do you want to enter details of another car [Y/N]?
    This is car nr. 1
    Do you want to enter details of another car [Y/N]?y

    This is car nr. 2
    Do you want to enter details of another car [Y/N]?
    This is car nr. 3
    Do you want to enter details of another car [Y/N]?y

    This is car nr. 4
    Do you want to enter details of another car [Y/N]?
    This is car nr. 5
    Do you want to enter details of another car [Y/N]?n


    i'm expecting to ask in every loop if i want to continue or not.
     
  4. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Use the fflush to flush the buffer before each scanf
     
  5. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    sir(rakoczimrks)
    what you have entered source code is absolutely correct but
    when you are giving input a small problem occur i.e..,
    after entering input you will press enter key so ,when you are reading for second time
    it will treat the previous enter as a character and it may print its ascii value or some garbage

    so please use fflush(stdin)
    means you are buffering the output from the input device you have given
     
  6. Delois

    Delois New Member

    Joined:
    Aug 6, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Thanks shabir, i have the same problem with my c program thanks for giving solution, now its working
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice