繁体   English   中英

scanf和具有多个输入的循环 - c

[英]scanf and loops with multiple inputs - c

如何让我的程序接受多个输入(使用scanf和循环)并在计算后分别打印结果?

我的程序接受六个输入,但只显示一个输出。 我想得到以下输出: 我想要的样本输出

    #include <stdio.h>
    #include <string.h>

    main() {
        // Declaring variables
        char name[25];
        int hours, rate, gross, bonus, counter;

        printf(" A program to determine the gross pay for six Employees\n Please Enter the Name, No. of hours worked and hourly rate respectively\n");

        for (counter = 1; counter <= 6; counter++) {
            scanf("%s %d %d", &name, &hours, &rate);
            gross = hours * rate; // determining the gross pay
            printf("%s %d %d %d\n", name, hours, rate, gross);
        }
    }

正如@LPs在评论中已经提到的那样。 改变以下行
scanf("%s %d %d",&name,&hours,&rate);
对此
scanf("%s %d %d",name,&hours,&rate);
要么
scanf("%s %d %d",&name[0],&hours,&rate);

试试这个......

#include <stdio.h>
#include <string.h>

int main() { //it must be int main()
    // Declaring variables
    char name[25];
    int hours, rate, gross, bonus, counter;

    printf(" A program to determine the gross pay for six Employees\n Please Enter the Name, No. of hours worked and hourly rate respectively\n");

    for (counter = 1; counter <= 6; counter++) {
        scanf("%s %d %d", &name[0], &hours, &rate); //you are reading string as input, use either &name[0] or name (base address)
        gross = hours*rate; // determining the gross pay
        printf("%s %d %d %d\n", name, hours, rate, gross);
    }
    return 0; //on successful completion 
}

暂无
暂无

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

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