Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

output for the following code and why is it not working properly...

0 votes
asked Oct 22, 2019 by Wali Khan (120 points)
#include <stdio.h>

int

main ()

{

  printf ("Welcome to Waleed Express!!\n");

  printf ("Where would you like to travel??\n");

  printf ("a:Gujranwala\nb:Lahore\nc:Islamabad\nd:Karachi\ne:Pindi\n");

  char Gujranwala, Lahore, Islamabad, Karachi, Pindi;

  int a, b, c, d, e;

  int city;

  city = 0;

  switch (city)

    {

    case 0:

      scanf ("%d", &a);

      printf ("choose time...\na:0800 am\nb:04:00 pm\nc:09:00 pm\n");

      int f, g, h;

      int j;

      int Time;

      Time = 1;

      switch (Time)

{

case 1:

  getchar ();

  scanf ("%d", &f);

  j = 350;

  printf ("Your fare is %d\n", j);

  break;

      case 2:

  getchar ();

  scanf ("%d", &g);

  j = 400;

  printf ("Your fare is %d\n", j);

  break;

      case 3:

  getchar ();

  scanf ("%d", &h);

  j = 450;

  printf ("Your fare is %d\n", j);

  break;

      default:

  printf ("invalid option...\nchoose again");

}

break;

    case 2:

      scanf ("%d", &b);

      printf ("choose time...\na:0800 am\nb:04:00 pm\nc:09:00 pm\n");

      int k, l, m;

      int n;

      int yes;

      yes = 1;

      switch (yes)

{

case 1:

  getchar ();

  scanf ("%d", &k);

  n = 450;

  printf ("Your fare is %d\n", n);

  break;

case 2:

  getchar ();

  scanf ("%d", &l);

  n = 500;

  printf ("Your fare is %d\n", n);

  break;

case 3:

  getchar ();

  scanf ("%d", &m);

  n = 550;

  printf ("Your fare is %d\n", n);

  break;

default:

  printf ("invalid option...\nchoose again");

}

break;

    case 3:

      scanf ("%d", &c);

      printf ("choose time...\na:0800 am\nb:04:00 pm\nc:09:00 pm\n");

      int o, p, q;

      int r;

      int no;

      no = 1;

      switch (no)

{

case 1:

  getchar ();

  scanf ("%d", &o);

  r = 550;

  printf ("Your fare is %d\n", r);

  break;

case 2:

  getchar ();

  scanf ("%d", &p);

  r = 600;

  printf ("Your fare is %d\n", r);

  break;

case 3:

  getchar ();

  scanf ("%d", &q);

  r = 650;

  printf ("Your fare is %d\n", r);

  break;

default:

  printf ("invalid option...\nchoose again");

}

break;

    case 4:

      scanf ("%d", &d);

      printf ("choose time...\na:0800 am\nb:04:00 pm\nc:09:00 pm\n");

      int s, t, u;

      int v;

      int please;

      please = 1;

      switch (please)

{

case 1:

  getchar ();

  scanf ("%d", &s);

  v = 650;

  printf ("Your fare is %d\n", v);

  break;

case 2:

  getchar ();

  scanf ("%d", &t);

  v = 700;

  printf ("Your fare is %d\n", v);

  break;

case 3:

  getchar ();

  scanf ("%d", &u);

  v = 750;

  printf ("Your fare is %d\n", v);

  break;

default:

  printf ("invalid option...\n choose again");

}

break;

    case 5:

      scanf ("%d", &e);

      printf ("choose time...\na:0800 am\nb:04:00 pm\nc:09:00 pm\n");

      int w, x, y;

      int z;

      int nice;

      nice = 1;

      switch (nice)

{

case 1:

  getchar ();

  scanf ("%d", &w);

  z = 750;

  printf ("Your fare is %d\n", z);

  break;

case 2:

  getchar ();

  scanf ("%d", &x);

  z = 800;

  printf ("Your fare is %d\n", z);

  break;

case 3:

  getchar ();

  scanf ("%d", &y);

  z = 850;

  printf ("Your fare is %d\n", z);

  break;

default:

  printf ("invalid option...\n choose again");

}

      break;

      printf ("invalid option\n choose again");

    }

  printf

    ("thank you for choosing waleed express...\n come back again\n Have a safe ride ...!!");

  getchar ();

}

2 Answers

0 votes
answered Oct 26, 2019 by anonymous
you should have to  use scanf library ( #include<conio.h>.)
0 votes
answered Oct 27, 2019 by gameforcer (2,990 points)

You're trying to make a loop depending on the switch options which is not directly possible. There are few workarounds to that.

1. Make a variable responsible for exit from the loop.

#include <stdio.h>

int main()
{
    int breakloop = 1;
    int scanVal = 1;
    
    while(breakloop)
    {
        scanf("%d",&scanVal);
        switch(scanVal)
        {
            case 1:
                //your code for case 1
                breakloop = 0;
                break;
            
            
            case 2:
                //your code for case 2
                breakloop = 0;
                break;

            default:
                //your code for default
        }
    }

    return 0;
}

2. Same as 1st option but with boolean instead of int.

#include <stdio.h>
#include <stdbool.h> //!!!important to include!!!

int main()
{
    int breakloop = true;
    int scanVal = 1;
    
    while(breakloop)
    {
        scanf("%d",&scanVal);
        switch(scanVal)
        {
            case 1:
                //your code for case 1
                breakloop = false;
                break;
            
            
            case 2:
                //your code for case 2
                breakloop = false;
                break;

            default:
                //your code for default
        }
    }

    return 0;
}

3.  Use functions!

#include <stdio.h>


void switchFun()
{
    int scanVal = 1;
    
    while(1)
    {
        scanf("%d",&scanVal);
        switch(scanVal)
        {
            case 1:
                //your code for case 1
                return;
            
            
            case 2:
                //your code for case 2
                return;

            default:
                //your code for default
        }
    }
}


int main()
{
    switchFun();
    return 0;
}

4. Using goto. (Please don't do that unless you really really want to.)

#include <stdio.h>

int main()
{
    int scanVal = 1;
    
    while(1)
    {
        scanf("%d",&scanVal);
        switch(scanVal)
        {
            case 1:
                //your code for case 1
                goto EndWhile;
            
            
            case 2:
                //your code for case 2
                goto EndWhile;

            default:
                //your code for default
        }
    }
    
    EndWhile:
        
        //your code after exit from the switch

    return 0;
}

As already mentioned I advise you not to use goto at all. It's generally low level programming function and if overused can make your code confusing. 

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...