0

hello so this code is for drowing shapes the problem is when the traitement which is in the loop while is finished and is lanced again after the function showinterface it don't wait for getting the char and then it repeat that function and wait for getting the char this is the code :

int main ()
{   while (1)
    {
     fflush(stdin);

    ShowInterface () ;

    ch = GetNumber () ;
    fflush(stdin);
    switch (ch)
    {
    case '1':
        
        printf ( " donner la largeur du carre ");
        scanf ("%d" , &langeur_carre) ;
        
        Drawing_shape (langeur_carre , langeur_carre) ;
        printf (" \n  ----------------------------------------------------- \n") ;
        fflush(stdin);

        break;
    
    case '2':
        
        printf ( " donner la largeur du rectangle  ");
        scanf ("%d" , &largeur_rect) ;

        
        printf ( " donner la longeur du rectangle  ");
        scanf ("%d" , &langeur_rect) ;
        Drawing_shape (largeur_rect ,langeur_rect) ;
        printf (" \n  ----------------------------------------------------- \n") ;
        fflush(stdin);
        break;
    case '3':
        exit(0);
    
    }

    }

and this is the output

  ----------------------------------------------------- 
        Bonjour Bienvenu !!       
1-Dessiner un carré  
 2-Dessiner un rectangle 
3-Quitter 
  
  ----------------------------------------------------- 
choisir le touche que vous convient : 1
 donner la largeur du carre 3
###
# #
###
 
  ----------------------------------------------------- 
 
  ----------------------------------------------------- 
        Bonjour Bienvenu !!       
1-Dessiner un carré  
 2-Dessiner un rectangle 
3-Quitter 
  
  ----------------------------------------------------- 
choisir le touche que vous convient :  
  ----------------------------------------------------- 
        Bonjour Bienvenu !!       
1-Dessiner un carré  
 2-Dessiner un rectangle 
3-Quitter 
  
  ----------------------------------------------------- 
choisir le touche que vous convient : 1 

As you see in the second time it don't get the char and even i don't touch any key

this the code of the function GetNumber :

char GetNumber ()
{       
        printf("choisir le touche que vous convient : ");
        scanf ("%c" , &ch);
        return (ch) ;
}    
ing23
  • 69
  • 1
  • 1
  • 7
  • https://stackoverflow.com/questions/2979209/using-fflushstdin – William Pursell Jul 08 '21 at 20:22
  • What is `GetNumber`? And there is other missing code too. Please provide a [complete minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – kaylum Jul 08 '21 at 20:23
  • @kaylum GetNumber is just a function that get the key pressed by the user ex 1 or 2 or 3 it is a char – ing23 Jul 08 '21 at 20:25
  • The duplicate posts explain your problem. FYI, this is why we always need complete code. Details matter in programming and even code you think may be unrelated could be significant (as it is in this case). – kaylum Jul 08 '21 at 20:29

1 Answers1

-1

I'd get rid of the fflush on stdin. Also you didn't show us your GetNumber() code. But it's probably that fflush.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • i have add the code of that function it doesn't work even i added it fflush – ing23 Jul 08 '21 at 20:32
  • The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement – ing23 Jul 08 '21 at 20:40