【发布时间】: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
【问题讨论】: