Need help with c loops

Please help me. I want the options to loop after the else if statements are executed and if I choose ‘q’ the program will terminate, otherwise, I can still choose the options and pick an app. Is there any advice to make my code any better especially in the output?

Here is my code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main()


   char none;

   char options;

//Power Problem solver

   float baseNum, powerNum, powerResult;

//Factorial Problem solver

   int chosenNum, factorialResult, i, factorialNum = 1;

//Quadratic Equation

   double a, b, c, discriminant, root1, root2;

   while(none != 'o') {

   while(options != 'q' || options != 'Q') {

       printf("\nCHOOSE AN APP \n");

       printf("\na. Power Problem Solver, b. Factorial Problem Solver, c. Quadratic Equation, q. QUIT \n");

       printf("Choice: ");

       scanf("%c", &options);


   if(options == 'a' || options == 'A') {

      printf("Enter base number: " , "\n") ;

      scanf("%f", &baseNum);

      printf("Enter a power number: ");

      scanf("%f", &powerNum);


      powerResult = pow(baseNum, powerNum);

      printf("%.2f raise to the power of %.2f is %.2f",baseNum, powerNum, powerResult);

   } else if(options == 'b' || options == 'B') {

      printf("Enter a number: ");

      scanf("%d", &chosenNum);

    for(i=1; i <= chosenNum; i++) {

       factorialNum *= i;


   printf("The factorial of %d is %d", chosenNum, factorialNum);

   } else if(options == 'c' || options == 'C'){

       printf("Enter 3 constants: ");

       scanf("%lf %lf %lf", &a, &b, &c);

       discriminant = b * b - 4 * a * c;

       root1 = (-b + sqrt(discriminant)) / (2 * a);

       root2 =(-b - sqrt(discriminant)) / (2 * a);

       if(a == 0 && b == 0) {

      printf("No Solution");

       } else if(a == 0) {

         printf("Number of roots is 1 \n");

         printf("Root 1: %.2f", (-c/b)); 

       } else if (discriminant < 0) {

         printf("No Real Roots");

       } else {

         printf("There are two roots \n");

         printf("Root 1: %.2lf \n", root1);

         printf("Root 2: %.2lf \n", root2);






   return 0;

while(options != 'q' || options != 'Q') {

I’m curious about the logic here. It’s this always going to be true? It will always not be one of those. Applying DeMorgan’s Law, isn’t this the same as:

while(!(options == 'q' && options == 'Q')) {

Since options == 'q' && options == 'Q' must always be false, then the negation must always be true.

Am I missing something?


A few things:

You really should initialize these values.

These should be reduced to the smallest scope needed They do not need to be declared for the entirety of main.

I would do this as a switch personally

switch (options) {
  case: 'a'
  case: 'A'
    // ...
  // ...

I would turn these into separate functions. It helps make your code more modular and easier to change.

Also, @kevinSmith has a great point that your while loop condition is slightly wrong.

1 Like

Umm guys I am really new to c and I still haven’t learned about functions, arrays, and stuff. Still really confused about the logic and stuff. I just want to do everything in the main because that is all that is covered in our lessons so far. Do you guys have any alternative solution for my problem?

In that case, I might suggest that you back up a bit and focus on fundamentals. I know that you’re anxious to get in there and rebuild the engine of that old `67 Mustang in your garage, but I might suggest that you should learn the basics of how an engine works first.

1 Like

I think I got it. I just changed this

while(options != ‘q’ || options != ‘Q’)

I think my only problem is in the output of the statement:

a. Power Problem Solver, b. Factorial Problem Solver, c. Quadratic Equation, q. QUIT

doubles, is there any solution so it would just display one time?

Okay sir, thank you for the advice, ill keep that in mind. Thanks for the help :slight_smile:

You are trying to make something that is complex enough that you really need to learn about functions before you get too much further. Making more complex things without learning the more complex parts of the language is a good way to learn very bad habits.

When I say that this should be initialized, I mean that you really should give it an initial value, such as 'z'.

When I say that this should have smaller scope, I mean that this line should be inside your if statement for the “Power Problem solver”.

My job uses C, so if you have more questions, ask away!

1 Like

Thank you so much guys for trying to help. I think I fixed 90% of my problem. I will try to keep your advice. I know I am rushing things out with my code because I am under pressure right now and have no time to learn deeper in c for now because of deadlines. I promise I will learn more in c. I also keep switching between programming languages and maybe that’s why I formed many bad habits.

We all switch among coding languages. But you also need to learn them at some point. Get a solid foundation in them, preferably one at a time.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.