Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scanf不接收数据_C_Scanf - Fatal编程技术网

scanf不接收数据

scanf不接收数据,c,scanf,C,Scanf,我在一门C入门课上,在数据输入方面遇到了问题。我在子程序的练习中,我的代码看起来是正确的,但是程序中的一个问题由于某种原因被绕过了,我无法理解。p> 1) 程序以10个独立字符的形式读入ISBN书号(检查) 2) 程序读入一本书的价格(支票) 3) 程序读取一个班级的学生人数(检查) 4) 程序询问这本书是新版本还是旧版本(不起作用!!) 5) 程序询问是否需要或建议使用该书(检查) 我使用char来回答关于新的或旧的、需要的或建议的问题,因为我们假设dto是为了利用我们迄今所学的知识 我不明白

我在一门C入门课上,在数据输入方面遇到了问题。我在子程序的练习中,我的代码看起来是正确的,但是程序中的一个问题由于某种原因被绕过了,我无法理解。p> 1) 程序以10个独立字符的形式读入ISBN书号(检查)

2) 程序读入一本书的价格(支票)

3) 程序读取一个班级的学生人数(检查)

4) 程序询问这本书是新版本还是旧版本(不起作用!!)

5) 程序询问是否需要或建议使用该书(检查)

我使用char来回答关于新的或旧的、需要的或建议的问题,因为我们假设dto是为了利用我们迄今所学的知识

我不明白为什么有一个问题被忽略了

以下是我的输出:

Enter ISBN: 1231231231

Enter list price per copy: 54.99

Enter expected class enrollment: 45

Enter N for new edition or O for Older edition:
Enter R for Required or S for Suggested: R



ISBN: 1-23-123123-1

List Price:  54.99
Expected enrollment: 45
Edition, New or Old: 

Importance, Required or Suggested: R
如您所见,第四个问题的scanf被忽略。 这是我到目前为止编写的代码。非常感谢您的帮助。
多谢各位

#include <stdio.h>

#define WHOLESALE 80

void getInput(char* a, char* b, char* c, char* d, char* e,
              char* f, char* g, char* h, char* i, char* j,
              float* listPrice, int* numStudents, char* edition, char* importance);
void calc();
void calcBooks();
void calcProfit();
void output();


int main (void) {
    // Local declarations
    float   listPrice;
    int     numStudents;
    char    edition;    
    char    importance;

    // ISBN char variables:
    char a; // 1
    char b; // 2
    char c; // 3
    char d; // 4
    char e; // 5
    char f; // 6
    char g; // 7
    char h; // 8
    char i; // 9
    char j; // 10

    // Get input
    getInput(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &listPrice, 
             &numStudents, &edition, &importance);



    // Calculate 



    // Output 
    printf("\nISBN: %c-%c%c-%c%c%c%c%c%c-%c\n", a, b, c, d, e, f, g, h, i, j); // ISBN output
    printf("\nList Price: %6.2f", listPrice);
    printf("\nExpected enrollment: %d", numStudents);
    printf("\nEdition, New or Old: %c", edition);
    printf("\nImportance, Required or Suggested: %c", importance);

    return 0;
} // main 


/* =============== getInput ==========================================
    Gets input from the user.
    Pre:    addresses for ISBN (in seperate characters)
            and for listPrice, numStudents, importance, and edition.
    Post:   Passes back values thru the addresses.  
*/
void getInput(char* a, char* b, char* c, char* d, char* e,
              char* f, char* g, char* h, char* i, char* j,
              float* listPrice, int* numStudents, char* edition, char* importance)
{   
    printf("\nEnter ISBN: ");
    scanf("%c%c%c%c%c%c%c%c%c%c", a,b,c,d,e,f,g,h,i,j);

    printf("\nEnter list price per copy: ");
    scanf("%f", listPrice);

    printf("\nEnter expected class enrollment: ");
    scanf("%d", numStudents);

    printf("\nEnter N for new edition or O for Older edition: ");
    scanf("%c", edition);

    printf("\nEnter R for Required or S for Suggested: ");
    scanf("%c", importance);




    return;
} // getInput
#包括
#定义批发80
void getInput(char*a、char*b、char*c、char*d、char*e、,
char*f,char*g,char*h,char*i,char*j,
浮动*标价,整数*numStudents,字符*版本,字符*重要性);
void calc();
void calcBooks();
无效calcProfit();
无效输出();
内部主(空){
//本地声明
浮动定价;
国际学生;
字符版;
字符重要性;
//ISBN字符变量:
字符a;//1
char b;//2
char c;//3
char d;//4
字符e;//5
charf;//6
charg;//7
char h;//8
char i;//9
char j;//10
//获取输入
getInput&a、b、c、d、e、f、g、h、i、j和listPrice,
&Numstudens,&版本和重要性);
//算计
//输出
printf(“\nISBN:%c-%c%c-%c%c%c%c%c%c-%c\n”,a,b,c,d,e,f,g,h,i,j);//ISBN输出
printf(“\n标价:%6.2f”,标价);
printf(“\n预期注册:%d”,numStudents);
printf(“\n条件,新的或旧的:%c”,版本);
printf(“\n重要性,要求或建议:%c”,重要性);
返回0;
}//主要
/*===================getInput==========================================
获取用户的输入。
前置:ISBN的地址(以单独字符表示)
以及listPrice、numStudents、importance和edition。
Post:通过地址传回值。
*/
void getInput(char*a、char*b、char*c、char*d、char*e、,
char*f,char*g,char*h,char*i,char*j,
浮动*定价,整数*numStudents,字符*版本,字符*重要性)
{   
printf(“\n输入ISBN:”);
scanf(“%c%c%c%c%c%c%c%c%c%c%c”,a、b、c、d、e、f、g、h、i、j);
printf(“\n输入每本的标价:”);
scanf(“%f”,标价);
printf(“\n输入预期的班级注册:”);
scanf(“%d”,numStudents);
printf(“\N为新版输入N,或为旧版输入O:”);
scanf(“%c”,版本);
printf(“\n输入R表示必需,输入S表示建议:”;
scanf(“%c”,重要性);
回来
}//获取输入
转换说明符(%d,%e,%s)跳过前导空格。%c转换说明符不存在

要强制跳过空白,请在格式字符串中包含空格:

scanf(" %c", &edition);
否则,scanf将读取您用于上一行的[ENTER],“normal”
scanf
转换说明符(%d,%e,%s)跳过前导空格。%c转换说明符不存在

要强制跳过空白,请在格式字符串中包含空格:

scanf(" %c", &edition);

否则,scanf将读取您用于上一行的[ENTER]

scanf是。。。行为不端,实际上你永远不想使用它。不过,如果是指定的,我想你必须这样做。谷歌发现。除了flushall(),另一个解决方法是将scanf(“%c”,edition)行增加一倍-第一行将删除延迟的新行,第二行将获得您的输入。

scanf是。。。行为不端,实际上你永远不想使用它。不过,如果是指定的,我想你必须这样做。谷歌发现。除了flushall(),另一个解决方法是将scanf(“%c”,edition)行增加一倍-第一行将删除延迟的新行,第二行将获取您的输入。

您也可以使用

    gets(str);
要接受空格,您还可以使用

    gets(str);

接受空白

谢谢。在我沮丧的时候,我注意到了将来输入字符的方法,但是是的,我必须遵守这个类的规则。@cloudhead:google find建议使用无法提及的函数LOL!永远不要使用
获取
。永远不要使用
fgets
后跟
sscanf
;在格式字符串中使用带空格的scanf;使用getchar()读取字符1乘1,但不要使用
gets
+1到
fgets
sscanf
组合。一个做IO,一个做解析
scanf
的边缘案例太多。谢谢。在我沮丧的时候,我注意到了将来输入字符的方法,但是是的,我必须遵守这个类的规则。@cloudhead:google find建议使用无法提及的函数LOL!永远不要使用
获取
。永远不要使用
fgets
后跟
sscanf
;在格式字符串中使用带空格的scanf;使用getchar()读取字符1乘1,但不要使用
gets
+1到
fgets
sscanf
组合。一个做IO,一个做解析
scanf
有太多的边缘大小写。
scanf
需要
来存储字符
scanf
需要
来存储字符!