【问题标题】:scanf not taking in datascanf 不接收数据
【发布时间】:2010-11-09 00:32:04
【问题描述】:

我正在上 C 入门课程,但遇到了数据输入问题。我正在进行子程序练习,我的代码看起来是正确的,但由于某种原因,程序中的一个问题被绕过了,我无法弄清楚。

1) 程序读入一个 ISBN 书号作为 10 个单独的字符(检查)

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

如您所见,第 4 个问题的 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

【问题讨论】:

    标签: c scanf


    【解决方案1】:

    “正常”scanf 转换说明符(%d、%e、%s)跳过前导空格。 %c 转换说明符没有。

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

    scanf(" %c", &edition);
    

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

    【讨论】:

    • scanf 需要&amp; 来存储一个字符!
    【解决方案2】:

    scanf ……表现不佳,实际上您永远不想使用它。但是,如果已分配,我想您必须这样做。谷歌找到this。除了 flushall(),另一种解决方法是将 scanf("%c",edition) 行加倍 - 第一个会吃掉挥之不去的换行符,第二个会得到你的输入。

    【讨论】:

    • 谢谢。在我沮丧的时候,我注意到了未来输入字符的方式,但是是的,我必须遵守这个类的规则。
    • @cloudhead:谷歌发现建议使用不可提及的功能哈哈! 永远不要使用gets。 EVER NEVER NEVER 使用fgets,后跟sscanf;在格式字符串中使用带有空格的 scanf;使用 getchar() 逐个读取字符 --- 但不要使用 gets
    • +1 到 fgetssscanf 组合。一个做 IO,一个做解析。 scanf 有太多边缘情况。
    【解决方案3】:

    你也可以使用

        gets(str);
    

    接受空格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2021-08-02
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多