2015-06-23 136 views
-3

代碼:如何在同一時間打印兩個輸入的輸出?

#include <stdio.h> 

int main() 
{ 
    int w,x,y,z; 
    float v; 
    printf("Enter the driver salary\n"); 
    scanf("%d",&x); 
    printf("Enter the car mileage in km per litre\n"); 
    scanf("%d",&y); 
    printf("Enter the cost of petrol per litre\n"); 
    scanf("%d",&z); 
    printf("Enter the taxi fare for a km\n"); 
    scanf("%d",&w); 
    printf("Enter the distance of travel\n"); 
    scanf("%f",&v); 
    if(w==200 && y==10 && z==60 && x== 20 && v==10.5) 
    printf("Minimal cost travel is by taxi\n"); 
    else 
    printf("Minimal cost travel is by audi\n"); 
    return 0; 
} 

對於兩種不同的w組輸入值,yzxv,我需要打印在同一時間兩個輸出語句。我獲得第一個輸出,但是如何同時獲得兩個輸出?

+0

刪除'else',並把它一起下'if'?沒有真正理解這個問題 – Ediac

+0

你能更具體一些「兩組不同的輸入值」有點模糊,沒有幫助。通常有幫助的是爲給定的輸入提供期望的輸出,然後爲該輸入提供實際的輸出。 – Daniel

+0

請嘗試多解釋一下你的問題。 –

回答

0

您需要將核心功能包裝在for/while循環中。我將建議將核心功能也放在一個功能中。

void processInput() 
{ 
    int w,x,y,z; 
    float v; 

    printf("Enter the driver salary\n"); 
    scanf("%d",&x); 
    printf("Enter the car mileage in km per litre\n"); 
    scanf("%d",&y); 
    printf("Enter the cost of petrol per litre\n"); 
    scanf("%d",&z); 
    printf("Enter the taxi fare for a km\n"); 
    scanf("%d",&w); 
    printf("Enter the distance of travel\n"); 
    scanf("%f",&v); 
    if(w==200 && y==10 && z==60 && x== 20 && v==10.5) 
     printf("Minimal cost travel is by taxi\n"); 
    else 
     printf("Minimal cost travel is by audi\n"); 
} 

int main() 
{ 
    int i; 
    for (i = 0; i < 2; ++i) 
    { 
     processInput(); 
    } 
    return 0; 
} 
+0

「_i需要同時打印兩個輸出語句_」 - 不是在每個輸入 –

+0

@CoolGuy之後,好點。我打算給OP一些時間發表評論。如果這不是他們想要的,我會刪除答案。 –

1

如果你想同時輸出一個標誌數組來存儲results.Also你可能希望存儲在一個浮點數10.5的值。 Read more。我加在你的代碼檢查的幾行,如果它的工作原理:

#include<stdio.h> 
int main() 
{      
    float l=10.5;    //to be safe about float rounding up 
    int i,fl[2];    //stores results for output 

    for(i=0;i<2;i++)   //add this 
    { 
    int w,x,y,z; 
    float v; 
    printf("Enter the driver salary\n"); 
    scanf("%d",&x); 
    printf("Enter the car mileage in km per litre\n"); 
    scanf("%d",&y); 
    printf("Enter the cost of petrol per litre\n"); 
    scanf("%d",&z); 
    printf("Enter the taxi fare for a km\n"); 
    scanf("%d",&w); 
    printf("Enter the distance of travel\n"); 
    scanf("%f",&v); 

    if(w==200 && y==10 && z==60 && x== 20 && v==l) 
     fl[i]=1; 
    else 
     fl[i]=0; 
    } 
    for(i=0;i<2;i++) 
    { 
    if(fl[i]==1) 
     printf("Case %d : Minimal cost travel is by taxi\n",i+1); 
    if(fl[i]==0) 
     printf("Case %d : Minimal cost travel is by audi\n",i+1); 
    } //close braces 

    return 0; 
}