So I have got two functions:
void Fun1() {
int x, y;
char term;
printf("Provide 2 numbers: ");
if (scanf_s("%d %d%c", &x, &y, &term)<2 || term != '\n') {
printf("Wrong data.\n");
}
else
{
if (x > y) {
printf("Greater number is %d.\n", x);
}
else if (x < y) {
printf("Greater number is %d.\n", y);
}
else {
printf("Numbers are equal.\n");
}
}
}
void Fun2() {
char buf[100];
printf("Provide your name and surname: ");
if (fgets(buf, sizeof(buf), stdin) != NULL){
printf("Hello %s\n", buf);
}
else {
printf("Wrong data\n");
}
}
I execute one function after another. Why when I provide wrong data in fun1 it pass them to func2? This is simple form of my code with only two functions, but generally I have got four and even when I provide right data in the last function it displays 'Hello ' skipping the part of taking your name and surname.
Provide 2 numbers: 2 a
Wrong data
Provide your name and surname: Hello a