Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c - scanf not taking in data

I am in an introductory C class and am having a problem with data input. I am in the middle of an exercise on subroutines and my code appears correct, but one question within the program is being bypassed for some reason, and I cannot figure it out.

1) The program reads in an ISBN book number as 10 separate characters (check)

2) The program reads in a price of the book (check)

3) The program reads in the number of students in a class (check)

4) the program asks weather the book is a newer edition or an older edition (not working!!)

5) The program asks weather the book is Required or Suggested (check)

I am using char for the questions regarding new or old, and required or suggested, as we are suppose dto in order to utilize what we have learned so far.

I cannot understand why one of the questions is being bypassed.

Here is my output:

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

As you can see, the scanf for the 4th question is ignored. Here is the code I have written so far. Any help is greatly appreciated.
Thank you.

#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("
ISBN: %c-%c%c-%c%c%c%c%c%c-%c
", a, b, c, d, e, f, g, h, i, j); // ISBN output
    printf("
List Price: %6.2f", listPrice);
    printf("
Expected enrollment: %d", numStudents);
    printf("
Edition, New or Old: %c", edition);
    printf("
Importance, 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("
Enter ISBN: ");
    scanf("%c%c%c%c%c%c%c%c%c%c", a,b,c,d,e,f,g,h,i,j);

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

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

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

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




    return;
} // getInput
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The "normal" scanf conversion specifiers (%d, %e, %s) skip leading whitespace. The %c conversion specifier doesn't.

To force a skip of whitespace, include a space in the format string:

scanf(" %c", &edition);

otherwise scanf will read the [ENTER] you used for the previous line


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...