Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C

int flag=1,n;
    char string[200];
    printf("Enter number <0-5> : \n");
    scanf("%d",&n);
    printf("Insert string <bigger than the number above> : ");
    while(flag)
    {
        gets(string);
        flag=CheckNumOfWords(string,n);
    }
Hi guys
so when i run the script it doesn't pause so i can enter my string, he skips the func "gets(string)", but when i remove the "scanf("%d",&n)" the "gets(string)" workes fine
any suggestions or comments?
tnx!
Posted 27-Dec-12 22:01pm
idobry431
Edited 27-Dec-12 22:02pm
v2
Comments
walterhevedeich at 28-Dec-12 3:35am
   
There were some issues and documented solutions on using scanf before gets and vice versa. Google "scanf before gets". The results suggests some solutions that you might want to try.
idobry at 28-Dec-12 3:40am
   
Thx mate i saw servel solution and i just added "fflush(stdin);" after my scanf and it worked flawless.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

None of those functions "cancels" anything. Execute this code under the debugger; and you will clearly see what goes on.
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100