0

My assignment for my CS class is to create a program in C to solve for a power without utilizing the pow() command and while utilizing the While loop. My program works, however, I am having a problem with my "undefined" answers and I don't know how to fix it.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    double base, exponent, result, result2, result3;

    printf("enter  two numbers to fit this format x^n\n");
    printf("x can be any number. insert n only as a whole number.\n");
    scanf("%lf %lf", & base, &exponent);

    if ((base == 0) && (exponent == 0)) {
        printf("UNDEFINED\n");
    } else if ((base == 0) && (exponent <= -1)) {
        printf("UNDEFINED\n");
    }

    result = 1;
    result2 = 1;

    while (exponent != 0) {

        if (exponent <= -1) {
            result2 = result2 * base;
            exponent = exponent + 1;
        } else if (exponent > -1) {
            result = result * base;
            exponent = exponent - 1;
        }
    }

    result3 = 1 / result2;

    if ((result3 < 1) && (result > 0)) {
        printf("The answer is %lf\n", result3);
    } else {
        printf("the answer is %lf\n", result);
    }
}

When I enter my base and exponent that should give me an "UNDEFINED" answer (power and exponent are both 0 OR when the base is 0 and the exponent is a negative number), my answer comes up as "undefined" but the program also tells me the solution is "1". How do I get the "answer is 1" thing to go away?

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
larn
  • 398
  • 2
  • 16
  • 1
    Possible duplicate of [How do I “break” out of an if statement?](https://stackoverflow.com/q/8523538/608639), [How to exit a while-loop?](https://stackoverflow.com/q/13803072/608639) and [How to end a program with an if statement?](https://stackoverflow.com/q/21789720/608639). Also see [How to debug a C program](https://stackoverflow.com/q/2590931/608639). – jww Sep 25 '18 at 08:55
  • 1
    Also, remove the `>` and make it nicely indented to make it easier to read. – domen Sep 25 '18 at 08:56
  • Hey @larn i have edit your question, kindly accept it – scienticious Sep 25 '18 at 09:10

4 Answers4

0

You have to stop your program when you have an "undefined" case. Then you can just add "return (0);" after your printf.

I advise you to have a better indentation syntax, yours is rather illisible.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void) // better to add "void" when you're not using program's argument
{

    double base;
    double exponent;
    double result;
    double result2;
    double result3;

    printf("enter  two numbers to fit this format x^n\n");  
    printf("x can be any number. insert n only as a whole number.\n");   
    scanf("%lf %lf", &base, &exponent);

    if (base == 0 && exponent <= 0) { // useless surparenthesis that make harder to read the line
        printf("UNDEFINED\n");
        return (0);
    }
    // You can merge your two condition "if ((base==0) && (exponent==0))" and " if ((base==0) && (exponent<=-1))" into one

    result = 1;
    result2 = 1;

    while (exponent != 0) {
        if (exponent <=- 1) {
            result2  = result2 * base;
            exponent = exponent + 1;
        } else { // useless if ... if (exponent > -1)
            result = result * base;
            exponent = exponent - 1;
        }
    }

    result3 = 1 / result2;

    if (result3 < 1 && result > 0) {
        printf("The answer is %lf\n", result3);
    } else {
        printf("the answer is %lf\n", result);
    }

    return (0);
}
Tom's
  • 2,448
  • 10
  • 22
0

Modify the condition like this:

if ((base == 0) && (exponent <= 0)) {
    printf("UNDEFINED\n");
    return 0;
}
NiVeR
  • 9,644
  • 4
  • 30
  • 35
0

Your problem is that the code after the else if is excuted because your program doesnt terminate after the printf. To avoid that you can either add a return after the printf or you put the rest of the code in a else like that:

OPTION 1:

...
    if ((base == 0) && (exponent == 0)) {
            printf("UNDEFINED\n");
            return 0;
    } else if ((base == 0) && (exponent <= -1)) {
            printf("UNDEFINED\n");
            return 0;
    }
...

OPTION 2:

int main() {

  double base, exponent, result, result2, result3;
  printf("enter  two numbers to fit this format x^n\n");
  printf("x can be any number. insert n only as a whole number.\n");
  scanf("%lf %lf", & base, & exponent);

  if ((base == 0) && (exponent == 0)) {
    printf("UNDEFINED\n");
  } else if ((base == 0) && (exponent <= -1)) {
    printf("UNDEFINED\n");
  } else {

    result = 1;
    result2 = 1;

    while (exponent != 0) {

      if (exponent <= -1) {
        result2 = result2 * base;
        exponent = exponent + 1;
      } else if (exponent > -1) {
        result = result * base;
        exponent = exponent - 1;
      }

    }

    result3 = 1 / result2;

    if ((result3 < 1) && (result > 0)) {
      printf("The answer is %lf\n",
        result3);
    } else {
      printf("the answer is %lf\n", result);
    }

  }

}
davidh
  • 49
  • 4
0

Everybody tells you to add a return statement after printing UNDEFINED. That is the answer to your question, however, you must also test the return value of scanf.

As it is now, if scanf is unable to read the two values, the values remain undetermined because they have not been initialized. (They are automatic variables, allocated on the stack, and automatic variables are never initialized.)

So:

    while (scanf(....)!=2) printf("You must enter two numbers. Please try again\n");
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41