我在介绍C类,我遇到数据输入问题.我正在练习子程序,我的代码看起来是正确的,但程序中的一个问题由于某种原因被绕过,我无法弄清楚.
1)程序读入ISBN书号作为10个单独的字符(检查)
2)程序读入书的价格(支票)
3)该课程读取班级中的学生人数(检查)
4)该程序要求天气这本书是较新版本或旧版本(不工作!!)
5)该程序要求天气书是必需的或建议的(检查)
我正在使用char来解决有关新旧问题的问题,以及我们为了利用迄今为止所学到的知识而提出要求或建议的问题.
我无法理解为什么其中一个问题被绕过了.
这是我的输出:
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
Run Code Online (Sandbox Code Playgroud)
如您所见,第四个问题的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
Run Code Online (Sandbox Code Playgroud)
"正常" scanf转换说明符(%d,%e,%s)跳过前导空格.%c转换说明符没有.
要强制跳过空格,请在格式字符串中包含空格:
scanf(" %c", &edition);
Run Code Online (Sandbox Code Playgroud)
否则scanf将读取您用于上一行的[ENTER]