2015-06-23 101 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; 
}