# How do I go back to my Menu after every switch and how to error trap effectively?

+1 vote
asked Dec 18, 2020
Here's a part of our program.

#include <stdio.h>

#include <stdlib.h>

int main()

{

int i, input,  AvoCal, IdealCal, ComCal;

double n, P, V, T, V1, V2, n1, n2, P1, P2, T1, T2;

const float R=0.0821;

while(1)

{

printf("\n\nGas Laws Calculator\n\n");

printf("MENU\n");

printf("\n(1) Avogadro's Law\n");

printf("(2) Ideal Gas Law\n");

printf("(3) Combined Gas Law\n");

printf("(4) Quit\n");

printf("\nEnter your choice: ");

scanf("%d",&input);

break;

}

// Solutions

switch(input)

{case 1:

printf("\nAvogadro's Law\n");

printf("Please select what you want to calculate.\n");

printf("\n(1) Find Volume (V1)\n");

printf("(2) Find Volume (V2)\n");

printf("(3) Find Moles (n1)\n");

printf("(4) Find Moles (n2)\n");

printf("(5) Quit\n");

printf("\nEnter your choice: ");

scanf("%d",&AvoCal);

if (AvoCal==1)

{

printf("\nFind V1\n");

printf("Enter given values.\n");

printf("Value of V2: ");

scanf("%lf", &V2);

printf("Value of n2: ");

scanf("%lf", &n2);

printf("Value of n1: ");

scanf("%lf", &n1);

V1=(n1*V2)/n2;

printf("\nAnswer: V1= %.2lf", V1);

}

else if (AvoCal==2)

{

printf("\nFind V2\n");

printf("Enter given values.\n");

printf("Value of n2: ");

scanf("%lf", &n2);

printf("Value of V1: ");

scanf("%lf", &V1);

printf("Value of n1: ");

scanf("%lf", &n1);

V2=(n2*V1)/n1;

printf("\nAnswer: V2= %.2lf", V2);

}

else if (AvoCal==3)

{

printf("\nFind n1\n");

printf("Enter given values.\n");

printf("Value of n2: ");

scanf("%lf", &n2);

printf("Value of V1: ");

scanf("%lf", &V1);

printf("Value of V2: ");

scanf("%lf", &V2);

n1=(n2*V1)/V2;

printf("\nAnswer: n1= %.2lf", n1);

}

else if (AvoCal==4)

{

printf("\nFind n2\n");

printf("Enter given values.\n");

printf("Value of n1: ");

scanf("%lf", &n1);

printf("Value of V2: ");

scanf("%lf", &V2);

printf("Value of V1: ");

scanf("%lf", &V1);

n2=(n1*V2)/V1;

printf("\nAnswer: n2= %.2lf", n2);

}

else if (AvoCal==5);

exit (0);

## 1 Answer

0 votes
answered Dec 21, 2020 by (9,400 points)

https://onlinegdb.com/ByyB3A6hw

I would suggest creating small specialised functions which do small parts of work , like function to print generic menu,function to print Avogardo Menu,function to calculate the missing volume,function to calculate # of moles etc .

I created functions to print menu & tries to correct the code , but will take much more time to fix it completly