S
S
Sahib Jabarov2021-06-10 13:59:40
C++ / C#
Sahib Jabarov, 2021-06-10 13:59:40

Why is the function not reading a character?

Hello! Is there a problem why the scanf() function from this program code does not read a character? Maybe I'm writing something wrong? Here is the program code:

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

int main(void)
{
   
    float x1, x2;
    int y1, y2;
    char z1, z2;
    srand(time(NULL));
    system("chcp 65001");
    printf("Задайте диапазон для целого числа:");
    scanf("%d", &y1, &y2);
    printf("%d\n", rand() % (y2 - y1) + y1);
    printf("Задайте диапазон символов:");
    scanf("%c%c", &z1, &z2); // вот именно в этом месте не работает
    printf("%c", rand() % (z2 - z1) + z1);
    return 0;
}

Instead of waiting for input, it spits out a random character and the program exits. In the first case it works correctly, but in the second it doesn't. Help me please.

The answer has been found! the first time you call the function, instead of typing scanf("%d %d", &y1, &y2); wrote with one "%d". Thank you kind person for pointing out the mistake!) But for some reason your comment was deleted (but if you are reading this, know that you saved me and my nerves!)) You are a hero!))))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question