vendredi 5 octobre 2018

In c program, how to just report error without having printf statement before it?

now my program looks like this :

include <stdio.h>

    int main(void) {
       int input1;
       int input2;
       int input3;

       printf("stat1");
       if scanf("%d",&input1 != 1) {
          printf("invalid1");
       }


       printf("stat2");
       if scanf("%d",&input1 != 1) {
          printf("invalid2");
       }

       printf("stat3");
       if scanf("%d",&input1 != 1) {
          printf("invalid3");
       }

       return 0;
    }

now the problem is, I have the text file, which includes:

5
10
asjsdlag

the last one is the invalid input, the correct output should only have:

invalid3

instead, I got the following output by ./a.out < test.txt:

stat1 stat2 stat3 invalid3

PS: please do not tell me to put printf statements below all scanf statements, I still need this program to run by using ./a.out so printf statements are still necessary to give hints when there are inputs.

It will be grateful if someone could help me, thank you.

Aucun commentaire:

Enregistrer un commentaire