Trying to create a guess my letter code. How do I incorporate the case where the input char from the user equals my letter?

Andrew

I am trying to create a program that compares the letter a user inputs to my letter. If the letters are the same, the program should say that they are the same, then terminate. If they are not the same, then the user should be prompted to enter another character until they guess it correctly.

I have tried nesting an if statement and nesting a while loop to achieve the case where the letters are equal.

#include <stdio.h>

int main()
{

    char myLetter = 'a';

    printf("insert a char:");

    char userLetter;

    scanf("%1s", &userLetter);

    while (userLetter !=  myLetter)
    {
        printf("%c does not match mine, try again:", userLetter);

        scanf("%1s", &userLetter);
    }

    while (userLetter == myLetter)
    {
        printf("char matches! program will terminate now. ");

        break;
    }
}

expected:

insert a char:h
h does not match mine, try again:j
j does not match mine, try again:g
g does not match mine, try again:f
f does not match mine, try again:a
char matches! program will terminate now.

actual:

insert a char:h
h does not match mine, try again:j
j does not match mine, try again:g
g does not match mine, try again:f
f does not match mine, try again:a
a does not match mine, try again:a does not match mine, try again:^C
Barmar

The proper format operator for reading a single character is %c, not %1s. The latter reads a single character, but writes it into a null-terminated string, so it will write a null byte outside the userLetter variable, which causes undefined behavior.

You should put a space before the operator to make scanf skip over whitespace before reading the character. This is needed to make it ignore the newline after each response.

You should also turn off output buffering or flush the buffer after each prompt, since they don't end with a newline.

There's no need for the while loop at the end, since you don't get out of the first loop until the characters match.

This is a working version:

#include <stdio.h>

int main()
{

    char myLetter = 'a';

    setbuf(stdout, NULL);
    printf("insert a char:");

    char userLetter;
    scanf(" %c", &userLetter);

    while (userLetter !=  myLetter)
    {
        printf("%c does not match mine, try again:", userLetter);
        scanf(" %c", &userLetter);
    }

    printf("char matches! program will terminate now.\n");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get my (if "input" in list) to check every letter of the input and not just the first letter of input?

How can I convert all my user inputs to the upper case letter using JavaScript?

How do I allow the user to guess the letter and corresponding number, hangman style?

How do I compare user input to an uppercase letter string in a list?

How do I only accept capital letter as user input?

How to incorporate user code into my own app?

How do I show my switch function that my string variable equals user input

How do i close the letter gap accross my navbar at the top

How do I lowercase the first letter of part of my urls address?

How do i take the input quote from a user and shuffle it then put every letter separate in a table in JavaScript

How do I add multiple lines of txt into an array and compare each letter to a user-input letter?

How do I change the following SVG code from letter 'B' to 'A'

I'm trying to figure out how to incorporate 3 variables into my tail recursion code for racket

How create a guessing game with letters. For example I need to guess a letter from the word fallout. Which functions are useful

How do I input data from a CSV file into my code?

How do I set my rails app up so that I can click a letter and see only the entries starting with that letter?

How do I limit my user input

How can I stop my average output from displaying the same letter grade as my original grade output? Also how to make sentinel values case insensitve?

How do i make the user input a number, and if the user inputs a letter it returns an error - VB.net

how do I convert the first letter of every word in a list from upper case to lower case?

How do i make this pattern from user input using a for loop (i want my code to be able to produce the output on the bottom)

How can I get my print statement to work to where it prints the average score and the correlating letter grade?

how do i remove a letter from string

How come if I enter a letter input my program gets stuck in its while loop?

How to uppercase every letter of my input sentences? (In Java)

I'm struggling to make the function work on my code. I'm missing the part where i want to print the letter grade

Where do I add code to capitalize the first letter in the first word?

How to write a program that ask the user a lower-case letter and then print tower of letters beginning from letter "a" up to the letter entered?

how do you get the first letter of a name from user input and put it into a if loop?