如何同时打印两个输入的输出?

时间:2015-06-23 05:25:53

标签: c

代码:

#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;
}

对于wyzxv的两组不同输入值,我需要打印两个输出语句同时。我正在获得第一个输出,但是如何同时获得两个输出?

2 个答案:

答案 0 :(得分:1)

如果你想同时输出一个标志数组来存储结果。你也可以将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;
}

答案 1 :(得分: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;
}