Error in my C Program

Asked

Viewed 137 times

1

When I type 0 it does not inform as the program should: "Incorrect input. Enter an integer value".

#include <stdio.h>

void LimpaBuffer(void)
{  
    int valorLido; /* valorLido deve ser int */
    do {
        valorLido = getchar();
       } while((valorLido = '\n') && (valorLido !=EOF));
}
int main(void)
{
    int umInt, outroInt, nValoresLidos;
    printf("Digite um valor inteiro");
    nValoresLidos = scanf("%d", &umInt);
    while(nValoresLidos == 0) { //Nenhum inteiro for lido
        LimpaBuffer();
        printf("Entrada incorreta. Digite um valor inteiro");
        nValoresLidos = scanf("%d", &umInt);
    }
    printf("Digite outro valor inteiro");
    nValoresLidos = scanf("%d", &outroInt);
    while (nValoresLidos == 0) {
        LimpaBuffer();
        printf("Entrada incorreta. Digite um valor inteiro");
        nValoresLidos = scanf("%d", &outroInt);

    }
    printf("\n%d + %d = %d", umInt, outroInt, umInt + outroInt);
    return 0;
}
  • I figured out the problem with your question, one is missing != instead of = in while((valorLido = '\n') &&

2 answers

5

This program is correct. What is wrong is the way AP thinks.

The while(nValoresLidos == 0) that is to say:

As the scanf not read, in this case, a %d gives reading error!

For example if you type:

Digite um valor inteiro
0
Digite outro um valor inteiro
1

0 + 1 = 1

That is absolutely correct.

In this case '0' is not a wrong form in input.


[Edit]

In addition this line lacks a !:

} while((valorLido = '\n') && (valorLido !=EOF));

should be :

} while((valorLido != '\n') && (valorLido !=EOF));

That way it’ll work properly.

3


In the loop while you are checking the nValoresLidos, but the scanf does not assign the value to nValoresLidos and yes to outroInt.

Do so:

scanf("%d", &outroInt);
nValoresLidos = outroInt;
  • I did it exactly like in the book.

  • Is the most recent edition of the book? If it is, you can send the correction to the author, he must have some means for it, which is in the book itself.

  • The book is from 2008 Programming in C - Volume I - Ulysses de Oliveira

  • 2

    Unfortunately most of the programming books are very bad. Staff do not take any care in the examples that can be easily checked. Imagine if you can trust the other information that’s harder to assess if you’re right or not when you’re learning.

  • @bigown this example works as it should be, see my answer.

  • @Jorgeb. hadn’t seen his answer yet, he ran. But I was confused is it correct or need to correct? In the first part you say you’re right and then you say you missed one not. Anyway it gets the record because many books are wrong even :)

  • @mustache is missing a not (I don’t know) but the author’s problem is another, he wants the error message to appear when typing 0, but does not coincide with the operation of the programme, because 0 is also a whole.

  • So while (nValoresLidos == 0) means "as long as an integer is not typed, error and read again"

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.