2010-11-09 33 views
1

我在一个介绍性的C类,并有数据输入的问题。我正在对子程序进行练习,并且我的代码看起来是正确的,但程序中的一个问题由于某种原因被绕过,我无法弄清楚。scanf没有收取数据

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 

正如你可以看到,第四届问题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 

回答

8

“正常” scanf转换说明(%d,%E,%S)跳过前导空白。 %c转换说明符没有。

要强制空白的跳跃,包括在格式字符串中空格:

scanf(" %c", &edition); 

否则scanf函数将读取[设置]时使用的前行

+0

'scanf'需要'&'存储字符! – haccks 2013-06-30 00:34:02

0

scanf的是......不行为良好,实际上你从不想使用它。但是,如果它被分配了,我想你必须。 Google找到this。除了flushall()之外,另一个解决方法是将scanf(“%c”,edition)行加倍 - 首先会吃掉延续的换行符,第二行将得到您的输入。

+0

谢谢。在我沮丧的时候,我注意到未来输入字符的方法,但是,我必须遵循这个类的规则。 – supergalactic 2010-11-09 00:50:32

+3

@cloudhead:谷歌发现建议使用* unmentionable函数* LOL! **不要经常使用'gets'。永远不要**使用'fgets'后跟'sscanf';使用格式字符串中带有空格的scanf;使用getchar()逐个读取字符---但不要使用'gets' – pmg 2010-11-09 01:25:23

+0

+1来'fgets'和'sscanf'组合。一个是IO,一个是解析。 'scanf'边缘案例太多。 – leesei 2015-09-24 04:09:13

-1

你也可以使用

gets(str); 

接受空格