Question: I need help adding a loop to repeat the menu as long as the user enters an invalid choice for this C program: #include #include
I need help adding a loop to repeat the menu as long as the user enters an invalid choice for this C program:
#include
double otherPlanetW(double weight) { int planet; printf("Choose planet: "); printf("1. Mercury 2. Venus 3. Mars 4. Jupiter 5. Saturn 6. Uranus 7. Neptune "); printf("Enter your choice: "); scanf("%d",&planet); switch(planet) { case 1: printf("Your weight on Mercury is: %.2lf ", 0.38 * weight); break; case 2: printf("Your weight on Venus is: %.2lf ", 0.9 * weight); break; case 3: printf("Your weight on Mars is: %.2lf ", 0.38 * weight); break; case 4: printf("Your weight on Jupiter is: %.2lf ", 2.36 * weight); break; case 5: printf("Your weight on Saturn is: %.2lf ", 1.08 * weight); break; case 6: printf("Your weight on Uranus is: %.2lf ", 0.8 * weight); break; case 7: printf("Your weight on Neptune is: %.2lf ", 1.12 * weight); break; default: printf("Invalid Input! "); break; } } int converttime() { int option,hours,minutes,days,seconds; printf("Choose option: "); printf("1. Days to Seconds 2. Hours to Seconds 3. Minutes to Seconds 4. Seconds to Seconds "); printf("Enter your choice: "); scanf("%d",&option); switch(option) { case 1: printf("Enter days: "); scanf("%d",&days); printf("%d seconds", days * 24 * 60 * 60); break; case 2: printf("Enter hours: "); scanf("%d",&hours); printf("%d seconds", hours * 60 * 60); break; case 3: printf("Enter minutes: "); scanf("%d",&minutes); printf("%d seconds", minutes * 60); break; case 4: printf("Enter seconds: "); scanf("%d",&seconds); printf("%d seconds", seconds); break; default: printf("Invalid Input! "); break; } } double orbitPlanet() { int planet; printf("Choose planet: "); printf("1. Mercury 2. Venus 3. Mars 4. Jupiter 5. Saturn 6. Uranus 7. Neptune "); printf("Enter your choice: "); scanf("%d",&planet); switch(planet) { case 1: printf("Mercury takes %.0lf seconds to orbit the sun.", 87.97 * 24 * 60 * 60); break; case 2: printf("Venus takes %.0lf seconds to orbit the sun.", 224.70 * 24 * 60 * 60); break; case 3: printf("Mars takes %.0lf seconds to orbit the sun.", 686.98 * 24 * 60 * 60); break; case 4: printf("Jupiter takes %.0lf seconds to orbit the sun.", 4332.82* 24 * 60 * 60); break; case 5: printf("Saturn takes %.0lf seconds to orbit the sun.", 10755.70 * 24 * 60 * 60); break; case 6: printf("Uranus takes %.0lf seconds to orbit the sun.", 30687.15 * 24 * 60 * 60); break; case 7: printf("Neptune takes %.0lf seconds to orbit the sun.", 60190.03 * 24 * 60 * 60); break; default: printf("Invalid Input! "); break; } }
int main() { int days,choice,planet; double weight; printf("Choose option: "); printf("1: How much do you weigh on other planets? 2: Convert Earth days, hours, minutes, seconds into just seconds. 3: How many seconds does it take other planets to orbit the sun? "); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter weight: "); scanf("%lf",&weight); otherPlanetW(weight); break; case 2: converttime(); break; case 3: orbitPlanet(); break; default: printf("Invalid Input! "); break;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
