clearing scanf()

This is a discussion on clearing scanf() within the C Programming forums, part of the General Programming Boards category; Ok.. Ive looked and looked and looked... all of the solutsions to stop scanf() from being a miserable old thing ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    clearing scanf()

    Ok.. Ive looked and looked and looked... all of the solutsions to stop scanf() from being a miserable old thing aint working for me

    ive tride fflush(stdin)
    using getchar()
    everything it seams

    Any help, I just wanted to re-post what has been asked a million times so that I can get an answer related to my setup

    running windows using scanf() and the mingw compiler

    I am sorry if this is a stupid question, and hey, if you think there is a post that can help me, please link.. the only reason why I'm not beating my head against finding a solution without asking you guys is because I'm on a schedual which involves 15 hours of programing a day to catch up on assingments, the more time I work on this, rather than finding solutions to easy problems, the better mark ill get. so I am leaving this pain in the but to some people who have already gone through what is annoing me... btw I know scanf() is supposedly crap, well its for a C assignment, which I never plan on using C again in my life, they just like torchering us with it so I don't want to learn how to better code in c, I just want an easy fix I can put at the end of my functions which use scanf() so I don't have to re-format all of my 1000+ lines of code.

    So yeah, I know I'm seamin like what everone on forum hates, a lazy person who can't find the solution for them self, I'm just trying to get my assingments done and Im willing to stoop to a stupid post about an easy problem.
    thanks to all you who don't criticize me, and I understand those whom do.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    OK, let's stop the ..........ing and start the fixing. What is the exact problem you are having with scanf?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Falconmick View Post
    So yeah, I know I'm seamin like what everone on forum hates, a lazy person who can't find the solution for them self, I'm just trying to get my assingments done and Im willing to stoop to a stupid post about an easy problem.
    thanks to all you who don't criticize me, and I understand those whom do.
    Ok... happy now, you got that off your chest?

    What exatly is the problem?

    Show us some code samples that demonstrate the problem...

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    When I use a use scanf() if a user enters an invalid input, I am amusing that scanf() doesn't throw it away, as the people at the C standards like to not just through stuff away they would either save it somwhere, or just plain keep it in the buffer.. I am amusing that they have chosen in this case to keep it on the buffer untill next use... I want to clear the buffer so that it is as is scanf() hasn't been used yet... fflush(stdin) didn't work for me... and that is what everone says to use

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1)fflush(stdin) is undefined behaviour. It could work, but it might also blow up your microwave, hence the term undefined. So don't use it and ignore everyone who says otherwise.

    2)It's hard to tell what you are trying to accomplish since you haven't actually specified an example yet, but you could look at other methods of taking input, such as using fgets() to read entire lines of text and then sscanf() to read in a formatted way or strtok to tokenize the line.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You should never use an output flush on an input buffer... so no fflush(stdin) is not going to work...

    Since you don't seem prone to post your code and your explanation is clear as mud.. there's not much we can do to help you. Most often there are simple solutions but there is no one solution for every situation...

    If you're referring to the problem with the trailing newline, most often the solution is simple...
    Code:
    scanf("%[^\n]", mystring);
    
    // or
    
    scanf ("%s %d", mystring, myint);
    while(getchar() != '\n');
    If it's something else... POST CODE... and lets see what we can do...

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    You should never use an output flush on an input buffer... so no fflush(stdin) is not going to work...

    Since you don't seem prone to post your code and your explanation is clear as mud.. there's not much we can do to help you. Most often there are simple solutions but there is no one solution for every situation...

    If you're referring to the problem with the trailing newline, most often the solution is simple...
    Code:
    scanf("%[^\n]", mystring);
    
    // or
    
    scanf ("%s %d", mystring, myint);
    while(getchar() != '\n');
    If it's something else... POST CODE... and lets see what we can do...
    I'm not using strings, so I tried making for the
    Code:
     scanf("%[^\n]", mystring);
    Code:
    int mystring;
    which didn't work (program froze) then I tried
    Code:
    while(getchar() != '\n');
    which failed(froze) I assume because I entered a valid input so it kept looking for \n which didn't exist:
    heres a segment of the code which is the issue:

    Code:
    void main_menu(int index)
    {
        int ch;
        int input;
        float inputTwo;
        switch(index)
        {
            case 1:
                printf("please enter new particle limit\n<default 2000> <min 50> <max 2000>\nnew limit: ");
                scanf("%d%*c", &input);
                if(input >= 50 && input <= 10000)
                {
                    num_particles = 0;
                    particleLimit = input;
                    printf("success there now are: %d particles\n", particleLimit);
                }
                else
                {printf("%d is not alloud\n", input);
    
                }
            break;
    
            case 2:
                printf("please enter new fountain spray radius\n<default 1> <min .3> <max 1>\nnew spray radius: ");
                scanf("%f%*c", &inputTwo);
                if(inputTwo >= 0.3 && inputTwo <= 1)
                {
    
                    howClose = inputTwo;
                    printf("success spray radius now equals: %f\n", howClose);
                }
                else
                {
                    printf("%f is not alloud\n", inputTwo);
                }
            break;
    
            case 3:
                if(evening == true)
                {
                    evening = false;
                }
                else
                {
                    evening = true;
                }
            break;
    
            case 4:
    
            break;
    
            case 5:
                printf("please enter new spawn rate(seconds)\n<default 0.01> <min 0.001> <no max>\nnew spawn rate: ");
                scanf("%f%*c", &inputTwo);
                if(inputTwo >=0.001)
                {
                    spwnTime = inputTwo*1000;
                    printf("success, spawn rate now equals: 1 particle every %d milliseconds\n", spwnTime);
                }
                else
                {
                    printf("that was too low\n");
                }
            break;
    
            case 6:
                printf("please enter new mass for particles\n<defaul 1> <min 1> <no max>\nnew mass: ");
                scanf("%f%*c", &inputTwo);
                if(inputTwo >= 1)
                {
                    currentMass = inputTwo;
                    printf("success, new mass = %f\n", currentMass);
                }
                else
                {
                    printf("failed, mass MUST be positive\n");
                }
            break;
    
            case 7:
                printf("please enter new size for particles\n<defaul 0.015> <min 0.001> <max 1>\nnew size: ");
                scanf("%f%*c", &inputTwo);
                if(inputTwo >= 0.001 && inputTwo <= 1)
                {
                    currentSize = inputTwo;
                    printf("success, new size = %f\n", currentSize);
                }
                else
                {
                    printf("%f is not acceptable\n", inputTwo);
                }
                break;
    
            case 8:
                crrntPrtclType++;
                if(crrntPrtclType > 1)
                {
                    crrntPrtclType = 0;
                }
            break;
    
            case 9:
                printf("please enter new definition for objects with definition settings\n<default 10> <min 3> <no max>\nnew definition: ");
                scanf("%d%*c", &input);
                {
                    if(input >= 3)
                    {
                        prtclDef = input;
                        printf("success, new definition: %d\n", prtclDef);
                    }
                    else
                    {
                        printf("definition can't be less than 3\n");
                    }
                }
            break;
    
            case 10:
                spurtType++;
                if(spurtType>2)
                {
                    spurtType = 0;
                }
                printf("%d\n", spurtType);
            break;
    
            case 11:
                printf("please enter new fountain power\n<default 4> <min 2> <no max>\nnew fountain power: ");
                scanf("%f%*c", &inputTwo);
                if(inputTwo>=2)
                {
                    fountaintSpeed = inputTwo;
                    printf("success, new fountain power: %f\n", fountaintSpeed);
                }
                else
                {
                    printf("power must be greater than 2\n");
                }
            break;
    
            case 12:
                exitModual();
            break;
        }
    
    
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try...
    Code:
    scanf("%f%[*^\n]", &inputTwo);
    or

    Code:
    scanf("%f",&inputTwo);
    while(getchar() != '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clearing the MRU.
    By The_Inferno in forum Windows Programming
    Replies: 2
    Last Post: 08-19-2005, 05:41 PM
  2. clearing buffer after reading string w/ scanf()
    By fisheromen1031 in forum C Programming
    Replies: 11
    Last Post: 08-01-2005, 09:33 AM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. clearing scanf
    By wagner in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 01:51 PM

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