2016-09-28 8 views
1

私のプログラムは複数の入力を受け入れることができます(scanf &ループを使用)&計算後にそれぞれ結果を出力しますか?複数の入力を持つscanfとループ-c

私のプログラムは6つの入力を受け入れますが、1つの出力しか表示しません。次の出力を得たいと思います:sample output I want

#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); 
     } 
    } 
+1

'のscanf( "%sの%D%D"、&名、および時間、&rate);'へ
scanf("%s %d %d",&name,&hours,&rate);
(%s%d%d)、&name [0]、&hours、&rate); ' – LPs

+1

' main() ' - >' scanf( "%s%d%d"、名前、&時間、&rate); 'または' - > 'int main(void)' – LPs

+0

あなたはその情報を保持する 'struct'を定義し、次にarその 'struct 'のレイは、多くの従業員の情報を保持することができます。 –

答えて

0

コメントで@LPsで既に述べたように。 を変更し、次の行この
scanf("%s %d %d",name,&hours,&rate);
または
scanf("%s %d %d",&name[0],&hours,&rate);

+0

&name [0]は不要です - 名前は –

+0

ですが、&name [0]は間違っていますか? 。私は名前がすることを知っている。私が間違っているなら、私を修正してください。 @edHeal。それが私がそれを声明で述べた理由です。 – sinsuren

0

、このいずれかを試してみてください...

#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 
} 
+0

あなたは '&name [0]'を必要としません - 'name'は何をしますか –

+0

@EdHealもちろん、どちらも同じです:) – roottraveller

関連する問題