scanf函数在数据未服用 [英] scanf not taking in data

查看:137
本文介绍了scanf函数在数据未服用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个介绍C级和时遇到的数据输入有问题。我在上一个子程序锻炼​​的中间,我的code出现正确的,但是在程序中的一个问题是被绕过出于某种原因,我不能弄明白。

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

2)程序的书(检查价格读取)

3)该项目在学生中的一类(检查读取次数)

4)程序要求渡过本书是一个较新的版本或较旧版本的(不工作!)

5)的程序要求渡过书要求或建议(检查)

我使用的字符作为对新的或旧的,并且要求或建议的,因为我们都是为了利用我们已经学会为止假设DTO的问题。

我不明白为什么的问题之一被绕过。

下面是我的输出:

 输入ISBN:1231231231输入标价每份:54.99输入您希望班招生:45输入N新版本,O代表旧版本:
对于建议所需或S输入R:RISBN:1-23-123123-1原价:54.99
预计招生:45
版,新的或旧的:重要的,必需或建议,R

如你所见,第四届问题scanf函数被忽略。
这里是code到目前为止,我写的。任何帮助是极大的AP preciated。结果
谢谢。

 的#include<&stdio.h中GT;批发的#define 80无效getInput(字符*一,字符* B,字符* C,字符* D,字符* E,
              字符*楼的char *克,字符* h时,字符*我的char *Ĵ,
              浮* listPrice,为int * numStudents,字符*版,字符*的重要性);
无效计算();
无效calcBooks();
无效calcProfit();
无效输出();
诠释主要(无效){
    //局部声明
    浮listPrice;
    INT numStudents;
    焦炭版;
    焦炭重要性;    // ISBN字符变量:
    所以char a; // 1
    炭B: // 2
    焦炭℃; // 3
    字符D组; // 4
    焦炭ê; // 5
    字符F; // 6
    焦克; // 7
    焦炭H; // 8
    烧焦我; // 9
    焦炭焦耳; // 10    //获取输入
    getInput(安培; A,和B,和C,和D,急症室的,和放大器; F,放克,和放大器; H,功放I,功放;Ĵ,&安培; listPrice,
             &放大器; numStudents,&放大器版,&放大器;重要性);    // 计算    //输出
    的printf(\\ nISBN:%C-%C%C-%C%C%C%C%C%C-%C \\ N,A,B,C,D,E,F,G,H,I ,J); // ISBN输出
    的printf(\\ NLIST价格:%6.2f,listPrice);
    的printf(\\ n期望招生数:%d,numStudents);
    的printf(\\ nEdition,新的或旧:%C版);
    的printf(\\ nImportance,要求或建议:%C的重要性);    返回0;
} //主
/ * =============== getInput ================================ ==========
    从用户获取输入。
    pre:为ISBN地址(在单独的字)
            和用于listPrice,numStudents,重要性和版本。
    帖子:形式传回直通地址的值。
* /
无效getInput(字符*一,字符* B,字符* C,字符* D,字符* E,
              字符*楼的char *克,字符* h时,字符*我的char *Ĵ,
              浮* listPrice,为int * numStudents,字符*版,字符*的重要性)
{
    的printf(\\ n输入ISBN:);
    scanf函数(%C%C%C%C%C%C%C%C%C%C,A,B,C,D,E,F,G,H,I,J);    的printf(\\ n输入列表每份价格:);
    scanf函数(%F,listPrice);    的printf(\\ n输入有望班招生:);
    scanf函数(%D,numStudents);    的printf(\\ n输入N表示新版本的,O代表旧的版本:);
    scanf函数(%C,版本);    的printf(\\ n输入R代表要求或S的建议:);
    scanf函数(%C,重要性);
    返回;
} // getInput


解决方案

正常 scanf函数转换说明(%D,%E,%S)跳过前导空白。在%C转换指定符则不会。

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

  scanf函数(%C,&安培;版);

否则scanf函数将读取[设置]您用于previous行

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("\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

解决方案

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

这篇关于scanf函数在数据未服用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆