Thread: Repeat condition problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14

    Smile Repeat condition problem

    Hi All!
    I am getting a problem in using scanf for yes/no condition
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    long fibonacci(int count);
    void main()
    {
    int count,n;
    char resp='Y';
    clrscr();
    do
    {
    printf("\nHow many fibonacci: ");
    scanf("%d",&n);
    for(count=1;count<=n;++count)
    printf("\ni= %2d\tF= %ld", count, fibonacci(count));
    printf("\nDo you want to check again? (Y/N):");
    scanf("%c",&resp);
    }while(toupper(resp)!='N');
    printf("\nBye, have a nice day!");
    getch();
    }
    long fibonacci(int count)
    {
    static long int f1=1, f2=1;
    long int f;
    f=(count<3)?1:f1+f2;
    f2=f1;
    f1=f;
    return(f);
    }
    Here i am facing the problem that this programme is not asking to user for the condition "Do You want to contineu Y/N: ".
    Please help..
    Anil
    Last edited by kumar14878; 05-05-2005 at 01:18 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    why do you have two scanf()

    u have to get a single value wheather Y or N

    use just one scanf to read one value from the user either y or n check that for your further processing
    Code:
    scanf("%c",&resp);
    scanf("%c",&resp);}
    please use some tabing space in your prog is looks neat

    s.s.harish

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    4
    try a getchar() before the scanf() statement.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    looking at your code again i found few bugs

    Code:
    .
    .
    void clr_buf();
    . 
    .
    for(count=1;count<=n;++count)
    printf("\ni= %2d\tF= %ld", count, fibonacci(count));
    printf("\nDo you want to check again? (Y/N):");
    clr_buf();                                  // clears the input buffer
    scanf("%c",&resp);}while(toupper(resp)!='N');
    printf("\nBye, have a nice day!");
    getch();
    }
    
    
    void clr_buf()
    {
           int ch;
            while((ch=getchar()) != '\n' || ch != EOF);
    }
    the problem here was the scacnf fucniotn will always leaves the reuturn key in the buffer so when u use scanf twice the first scanf picks up the return('\n' or enter key) and clears the buffer and agian when u use the scanf it gets the value from the user when entered by the user

    dont use the conio.h its not standard library and dont use the clrscr() RAD FAQ

    main() function should always return an int

    s.s.harish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A peculiar problem in displaying a file.
    By cheemais in forum C Programming
    Replies: 22
    Last Post: 10-03-2007, 10:25 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM