简体   繁体   中英

How do I print outputs for two inputs at the same time?

Code:

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

For two different set of values of inputs for w , y , z , x , v , I need to print both output statements at same time. I am getting first output, but how do I get two outputs at the same time?

If you want to output at same time have a flag array to store results.Also you may want to store the value of 10.5 in a float. Read more .I added a few lines in your code check if it works :

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

You need to wrap the core functionality in a for/while loop. I am going to suggest putting the core functionality in a function too.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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