Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

sometimes my code doesn't let me enter a character if it is asked earlier in the program

as the title says, sometimes my code doesn't let me enter a character if it is asked too earlier in the program. I know that it's not a big deal and that i could just let the upper part in commentary to test the bottom part, but i feel like i'm missing something i should know...

[CODE lang="c" title="code" highlight="9"]printf("Calculatrice de longueur de l'hypothénuse :\n");
printf("Longueur de l'adjacent :\n");
scanf("%f", &k);
printf("Longueur de l'opposé :\n");
scanf("%f", &l);
printf("Longueur de l'hypothénuse : %f\n", sqrt(k*k+l*l));

printf("entrez un caractère en minuscule : \n");
scanf("%c", &e);
if(e=='a'||e=='e'||e=='i'||e=='o'||e=='u'||e=='y')
{
printf("ce caractère est une voyelle\n");
}
else
{
printf("ce caractère est une consonne\n");
}[/CODE]

this returns, in the terminal :
Calculatrice de longueur de l'hypothénuse :
Longueur de l'adjacent :
9
Longueur de l'opposé :
9
Longueur de l'hypothénuse : 12.727922
entrez un caractère en minuscule : <<<<<<i'm supposed to enter a character, but it wont let me and keeps going...
ce caractère est une consonne


...Program finished with exit code 0
Press ENTER to exit console.

however, if i put the top part (the hypothenus calculus) in commentary, the bottom part (determining if a letter is a consonant or a vowel) works just fine (it lets me enter a character).

thanks a lot for your help !
 
Bienvenue maxime61210,

I think the problem is that when you read a floating-point number with 'printf', the newline character created when you press enter is not taken off so when you read a character, you actually read that newline character. To test if this is the case, look if e == '\n'.
 
thanks ^^
thank you very much ! i hadn't thought of that. i have tried to print e after scanning on the bottom part, however i get a warning that says "warning: format ‘%c’ expects argument of type ‘int’" on the line :
printf("%c", &e);

which bugs me since e is declared as a char and is supposed to have a character in it. plus, printing is returns me a question mark icon, that makes me think that it can't print the right character but just can say "hey, there's a character here but i don't know how to draw it"

thanks again 🙂
 
With printf, you must give, as argument, the value of the variable, not its address 😉 :
C++:
printf("%c", e);
oh yeah it look like i'm not even trying lol
anyway so it does seem like it prints "\n" as it has jumped a line whereas it didn't before i put the print there.
So how is it that it doesn't do that when i remove the hypothenus program, even though there is a line of code that looks like it could cause the same problem ?

the line in question :

C++:
printf("entrez un caractère en minuscule : \n");

thanks again for helping me figure out how all of this works !
 
Let us say you enter '1234' to the console. What will be received by the console is "1234\n" (The '\n' is created when you press the key 'enter'). If you execute 'scanf("%f", &var);', then, only the '1234' part will be taken off by 'scanf'. So, when you execute 'scanf("%c", &e);', what is read is '\n'.
 
right so i understand it is not the \n from an earlier printf but from me pressing enter. is there a way for it to not do that ? as if i actually wanted the program to calculate a hypothenus and then to tell me if a character i entered is actually a consonant or a vowel ?
 
Oh, I did not notice your reply! I was going to say to simply use 'getchar' first to discard the newline character, but what Krusty said is probably better here!
You would have to give 'stdin' to the function 'fflush':
C:
fflush(stdin);
Bascially, 'stdin' is a pointer to an object of type FILE that is linked to the console, by using 'fflush', you empty all that it previously received from the user entering text into the console.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom