#C and scanf

25 messages · Page 1 of 1 (latest)

pallid caveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#

@warped harness

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. You can use !solved to close a post and mark it as solved.

warped harness
#

Hello,

does someone know why my code does not ask for any input despite of the "scanf" ?

Thanks !


#include <stdio.h>
#include <stdlib.h>

int main(){

   char letter;
   int nbLignes;
   int occurrences = 0;
   
   scanf("%c", &letter);
   scanf("%d", &nbLignes);
   
   
   
   for(int i=1;i<=nbLignes;i++){
      char ligne[1001];
      scanf("%[^\n]\n", ligne);
      for (int j=0;ligne[j]!='\0';j++){
         if (ligne[j] == letter){occurrences++;}
      }
   }
   printf("%d", occurrences);
   return 0;
}
inner marlin
#

every once in a while this always pops up withered

warped harness
#

how can I avoid this issue ?

inner marlin
#

you can make your scanf line like this:
scanf(" %d", &nbLignes);

#

here is a blog post I wrote about this issue that is common with beginners using scanf

warped harness
#

no ?

inner marlin
inner marlin
warped harness
#

Okk thank, i'm gonna read that now

warped harness
inner marlin
#

it is

#

just look at the first half of my post

#

it explains why your problem occurs

warped harness
#

"After scanf reads in the number, the newline character stays . . . " what is "the new line character" ?

#

like

#

when i scanf my int

#

it does not go to the next line ?

inner marlin
#

for now you can understand newline as "when you press the enter key"

warped harness
#

Okk i see

#

i understand, tyvm for your help !

inner marlin
warped harness
#

!solved