什么`scanf函数(QUOT;%* [^ \\ n] *%C")`是什么意思? [英] What does `scanf("%*[^\n]%*c")` mean?
问题描述
我想要在C循环,当程序要求一个整数,用户输入一个非数字字符时,程序整数又问。
我刚刚发现下面的code。但我不明白这是什么意思 scanf函数(%* [^ \\ n] *%C)
。这是什么 ^ \\ n
是什么意思?什么是 *
在 ^ \\ n
和 C
意思?
/ * 这个程序计算出用户的4个人得分的平均分,
并输出均值和最终成绩
输入:score1,score2,score2,score3
输出:平均值,FinalGrade* /
#包括LT&;&stdio.h中GT;
//#包括LT&;&stdlib.h中GT;诠释主要(无效){
INT userScore = 0; //保存执行得分的用户输入
浮meanValue = 0.0; //存储所有音符的用户平均
焦炭testChar ='F'; //用于避免在code崩溃
字符级='E'; //存储最终
INT I = 0; //附配的用于声明 的printf(\\ nWelcome到程序\\ n告诉我,如果足够聪明进出口\\ n设计对于人类\\ n \\ n \\ n!);
的printf(请输入0和100之间的4笔记来计算你的课程成绩\\ n \\ n); //问4笔记。
对于(; I< = 3;我++){
的printf(请输入你的分数号%D:,我+ 1); //如果说明是无效的,再问吧 //这是测试是否用户输入是一个有效的整数。
如果((scanf函数(%D%C,&安培; userScore,&安培;!testChar)= 2 || testChar ='\\ n')!){
异= 1;
scanf函数(%* [^ \\ n]的%* C); }否则{//输入此处,如果用户输入是一个整数
如果(userScore> = 0&放大器;&安培; userScore< = 100){
//添加值均值
meanValue + = userScore;
}其他{//在这里输入,如果用户输入一个非有效的整数
异= 1;
// scanf函数(%* [^ \\ n]的%* C);
}
}
} //计算的分数的平均值
meanValue = meanValue / 4; //根据最终的平均中选择您的最终成绩
如果(meanValue> = 90安培;&安培; meanValue< = 100){
等级='A';
}否则如果(meanValue> = 80安培;&安培; meanValue< 90){
级='B';
}否则如果(meanValue> = 70&放大器;&放大器; meanValue&下; 80){
等级='C';
}否则如果(meanValue> = 60安培;&安培; meanValue< 70){
等级='D';
}
的printf(你的最后得分是:%2.2F - >%C \\ n \\ n,meanValue,等级); 返回0;
}
击穿scanf函数(%* [^ \\ n]的%* C)
:
-
%* [^ \\ n]
扫描一切,直到\\ n
(不扫描\\ n
),并将其丢弃,即不存储扫描数据的任何地方。 -
%* C
然后扫描并丢弃\\ n
字符,高达其中第一个格式说明已经扫描。
两者%[
和%C
的格式说明。你可以看到他们这里 。在这两个符星号告诉 scanf函数
,不要储存这些格式说明符读取数据。
在你的情况,这 scanf函数
清除标准输入
当用户输入一个无效的输入。
I want to make a loop in C that, when the program asks for an integer and the user types a non-digit character, the program asks again for an integer.
I just found the below code. but I don't understand what this means scanf("%*[^\n]%*c")
. What does ^\n
mean? What does the *
before ^\n
and c
mean?
/*
This program calculate the mean score of an user 4 individual scores,
and outputs the mean and a final grade
Input: score1, score2,score2, score3
Output: Mean, FinalGrade
*/
#include <stdio.h>
//#include <stdlib.h>
int main(void){
int userScore = 0; //Stores the scores that the user inputs
float meanValue = 0.0f; //Stores the user mean of all the notes
char testChar = 'f'; //Used to avoid that the code crashes
char grade = 'E'; //Stores the final
int i = 0; //Auxiliar used in the for statement
printf("\nWelcome to the program \n Tell me if Im clever enough! \n Designed for humans \n\n\n");
printf("Enter your 4 notes between 0 and 100 to calculate your course grade\n\n");
// Asks the 4 notes.
for ( ; i<=3 ; i++ ){
printf("Please, enter your score number %d: ", i+1);
//If the note is not valid, ask for it again
//This is tests if the user input is a valid integer.
if ( ( scanf("%d%c", &userScore, &testChar)!=2 || testChar!='\n')){
i-=1;
scanf("%*[^\n]%*c");
}else{ //Enter here if the user input is an integer
if ( userScore>=0 && userScore<=100 ){
//Add the value to the mean
meanValue += userScore;
}else{ //Enter here if the user input a non valid integer
i-=1;
//scanf("%*[^\n]%*c");
}
}
}
//Calculates the mean value of the 4 scores
meanValue = meanValue/4;
// Select your final grade according to the final mean
if (meanValue>= 90 && meanValue <=100){
grade = 'A';
} else if(meanValue>= 80 && meanValue <90){
grade = 'B';
} else if (meanValue>= 70 && meanValue <80){
grade = 'C';
} else if(meanValue>= 60 && meanValue <70){
grade = 'D';
}
printf("Your final score is: %2.2f --> %c \n\n" , meanValue, grade);
return 0;
}
Breakdown of scanf("%*[^\n]%*c")
:
%*[^\n]
scans everything until a\n
(Doesn't scan\n
) and discards it,i.e, doesn't store the scanned data anywhere.%*c
then scans and discards the\n
character, upto which the first format specifier has scanned.
Both %[
and %c
are format specifiers. You can see what they do here. The asterisks in both the specifiers tell scanf
, not to store the data read by these format specifiers.
In your case, this scanf
clears the stdin
when the user enters an invalid input.
这篇关于什么`scanf函数(QUOT;%* [^ \\ n] *%C&QUOT;)`是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!