简体   繁体   English

C程序,设计检查

[英]C program, designing a check

I'm writing a program that basically just takes a bunch of user input and Outputs it in the form of a check. 我正在编写一个程序,该程序基本上只需要一堆用户输入并将其以检查的形式输出。 My problem is that I'm entering 2 sentences and while the second one is fine it completly skips the first one! 我的问题是我要输入2个句子,而第二个句子很好,但它会完全跳过第一个句子! and the user last name is working but the first name just outputs "z" it is the weirdest thing and my teachers philosophy is figure it out yourself. 用户的姓氏起作用,但名字仅输出“ z”,这是最奇怪的事情,我的老师的想法是自己弄清楚。 So can anyone possibly help me?? 那么有人可以帮助我吗? Here is my code... 这是我的代码...

#include<stdio.h>
#include<string.h>
int main()
{
    char date[8];
    int checkNum;
    char payeeFirst[10];
    char payeeLast[10];
    double amount;
    char memo[50];
    char wordAmount[100];
    printf("Please enter the date: ");
    scanf("%s", &date);
    printf("Please enter the check number: ");
    scanf("%d", &checkNum);
    printf("Please enter the payee First name: ");
    scanf("%s", &payeeFirst);
    printf("Please enter payee last name: ");
    scanf("%s", &payeeLast);
    printf("Please enter amount: ");
    scanf("%d", &amount);
    printf("Please enter amount in words: ");
    fgets (wordAmount, sizeof(wordAmount)-1, stdin);
    printf("Please enter memo: ");
    fgets (memo, sizeof(memo)-1, stdin);


    printf("                               \n");
    printf("Date: %s .\n", date);
    printf("Check Number: %d .\n", checkNum);
    printf("Payee: [%s] [%s] .\n", payeeFirst, payeeLast);
    printf("Amount: %d .\n", amount);
    printf("                                 Check %d \n", checkNum);
    printf("                                 Date: %s \n", date);
    printf("Pay to the\n");
    printf("Order of    %s %s   $%d \n", payeeFirst, payeeLast, amount);
    printf("%s", wordAmount);
    printf("                        \n");
    printf("                        \n");
    printf("Memo: %s \n", memo);
    return 0;
}

Your scanf calls all leave the '\\n' in the stream (the next scanf will ignore it though). 您的scanf调用全部在流中保留'\\n' (但是下一个scanf将忽略它)。

The first fgets reads an empty string (well ... a string containing a single '\\n' ). 第一个fgets读取一个空字符串(嗯...一个包含单个'\\n'的字符串)。

Try reading the numbers with fgets too (to a temporary buffer) and sscanf from the buffer to the correct variable. 尝试使用fgets (到临时缓冲区)读取数字,并将sscanf从缓冲区读取到正确的变量。 This way all the '\\n' will be accounted for. 这样,所有'\\n'都将被考虑在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM