-1

I'm trying to write a C program that computes X^Y (X to the Y power) without using the 'pow' function. The program works fine when I enter numbers, but I'm having an issue with the code not stopping when the user enters a character that isn't a number. It runs through the program once more after giving the message "The character you have entered is not a number.Please try again.The character you have entered is not a number.Please try again. The value is 1." Can someone help? I'm going insane trying to figure this out.

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

int main()
{
int x, y, i,n=1,val; 

printf("Please enter a number: \n"); 
scanf("%d", &x);

if (x!=1)
    printf("The character you have entered is not a number.Please try again.");

else
    printf("What power is the number raised to?\n");
    scanf("%d", &y);

    if (y!=1)
    printf("The character you have entered is not a number.Please try again.");

    else
        for(i=1;i<=y;i++)
        {
        val=x;
        n=n*val;
        }
    printf("The value is %d.\n", n);
return 0;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rozay
  • 13
  • 7

3 Answers3

1

When your program detects invalid input, it prints a message, but otherwise just continues on ahead as if a valid input had been read. Instead, it needs to do one of these things:

  • abort when invalid input is detected, or
  • consume and ignore the invalid input, AND
    • loop back to provide a chance to enter new input, or
    • use a default value.

Its message suggests that the program will provide for entering new input, but in fact it does not follow through. Moreover, if it is the input for the first prompt that is invalid, then that input is still waiting to be read when the program prints the second prompt, where it is still invalid, and the program blithely continues on a second time, ultimately printing the initial value of n, 1, without having calculated anything.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • This all makes sense. I'm trying to work with a do-while loop at the moment but I keep running into errors with this as well. I think my issue lies in my if else statement when my statements say either "(x!=1)" or "(x==1)" and the same with "y". – Rozay Feb 07 '19 at 12:39
  • Well, @Rozay, done right, checking the value of `x` and / or `y` can tell you about many cases in which you *successfully* scan numbers -- those in which the number scanned is different from the prior values of thosee variables -- but it does not reliably tell you about failures. For that, you should be checking the function's **return value**. I.e. `rval = scanf("%d", &x); if (rval != 1) ...`. – John Bollinger Feb 07 '19 at 13:02
  • that makes sense but appears that it is more difficult than I anticipated. The assignment called for a simple program and I was looking to add a little extra with this. For the time being I'm going with the simple program until I get further in my coding studies. – Rozay Feb 09 '19 at 19:00
0

You need to check how many items scanf successfully read:

int numItemsRead = 0;

printf("What power is the number raised to?\n");
numItemsRead = scanf("%d", &y);

if (numItemsRead!=1)
   printf("The character you have entered is not a number.Please try again.");
} else {
   '''
}
FredK
  • 4,094
  • 1
  • 9
  • 11
  • so set the values of both "x" and "y" (aka numItemsRead) equal to zero at the beginning of the program? – Rozay Feb 07 '19 at 01:16
  • No. x is the number that you want to raise to the y power. Neither is the same thing as numItemsRead.The method scanf() returns an int that is the number of items successfully read. In my example for reading y, if numItemsRead is equal to one, then y was successfully input. If numItemsRead is zero,it means that no number was successfully read ,(the value input for y is not a legitimate number). – FredK Feb 07 '19 at 23:14
0
int main()
{
int x, y, i,n=1,val; 

printf("Please enter a number: \n"); 
scanf("%d", &x);

if (x!=1)
printf("The character you have entered is not a number.Please try again.");
return 0;

else
printf("What power is the number raised to?\n");
scanf("%d", &y);

Maybe if u do like that even if u entered a invalid character program will be end.

  • This works to end the program if a letter is entered, however the program ends even if a number is entered. – Rozay Feb 07 '19 at 01:11
  • hey bro look your code carefully if you enter a number except 1 even if ıt is a number the program will be give error :) –  Feb 07 '19 at 01:34
  • Thank you!! That's what I thought was the problem...but I don't know what info to put there instead of this to say "if x is not a number then..." or "if y is not a number then...". I'm a real newb here so I apologize if this is simple and I'm making it much harder than need be. – Rozay Feb 07 '19 at 12:42
  • np bro.If you are beginner you can think like that "The user will be entered just numbers" .You think far-reaching ıts a good think but for just now dont think like that :)) –  Feb 07 '19 at 13:08