クラスの演習のためにこのコードの一部を実行しましたが、コードは機能していますが、特定の部分で何が起こっているのかよくわかりません。
誰でもこの部分を説明できますか:
if(scanf("%d%c", &num1, &term1) != 2 || term1 != '\n'){
printf("ERROR! You should type an integer e.g 5, 80, 100\n");
} else { ....
完全なコードは次のとおりです。
#include <stdio.h>
int main(){
int num1,num2;
char term1,term2;
printf("Type your first integer\n");
if(scanf("%d%c", &num1, &term1) != 2 || term1 != '\n'){
printf("ERROR! You should type an integer e.g 5, 80, 100\n");
} else {
printf("Type your second integer\n");
if (scanf("%d%c", &num2, &term2) != 2 || term1 != '\n'){
printf("ERROR! You should type an integer e.g 5, 80, 100\n");
}
printf("\n%d", num1);
for (int i=0; i<num1; i++) {
printf(" *");
}
printf("\n");
printf("%d", num2);
for (int j=0; j<num2; j++) {
printf(" *");
}
}
}
課題は、2 つの整数値を要求し、2 つの値で横棒グラフを作成するプログラムを作成することでした。
ありがとう。