Ok, this is my 3rd time asking a question so far I can see that people here are very helpful and I appreciate it :) today I have a question if something like this is possible

#include<stdio.h>
#include<stdlib.h>
int difficulty()
{
    char difficulty;
    printf("Level of difficulty? (l-Low,h-High) - ");
    scanf("%c",&difficulty);
    if (difficulty == 'l')
    printf("WEAK! :3\n");
    else if (difficulty == 'h')
    printf("PRO! XD");
    else
    printf("WRONG LETTER!\n");
    return 0;
}
int cheat()
{
    char cheat;
    printf("need cheats? <y-Yes,n-No>");
    scanf("%c",&cheat);
    if (cheat=='y')
    return 0;
    if (cheat=='n')
    return 1;
}
    
int main()
{
    difficulty();
    cheat(cheat);
    system("pause");
    return 0;
}

I can compile it but only 1 of the scanf worked :( I will appreciate any help given.

Recommended Answers

All 4 Replies

%c ALWAYS takes the next character, including things like space and newline.

Try say
scanf(" %c",&var)

Wow it works THANKS A LOT! :). so if I will make another function with scanf I should do something like scanf(" %c",&var)?

It's a bit strange to ask a question, then mark it as solved.

For your question, I would suggest not using scanf() at all, and instead only use fgets() to read the input into a buffer, then use sscanf(), or any other string functions, to extract what you need from the buffer.

scanf() is too tricky to make it robust in anything except the simplest of programs.

Oh Sorry kinda new on this site :) can I know what's the diffrence between scanf() and fgets()?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.