The task goes like this:
Write a program that calculates the power of a number (as a pow function from the "math.h" library), except the limitation here is that the exponent can only be an integer. The base and exponent are entered from the keyboard, where the base can be of the real type, while the exponent is a positive or negative integer. Attention should be paid to checking data entry; special treatment should be given to cases where the number is not entered and when the number entered is not an integer when entering the exponent!
Example input/output:
Enter a base number: abc
You didn't enter a number!
Enter a base number: 3.3
Enter exponent: something
You didn't enter a number!
Enter a base number: 3.3
Enter exponent: 5
3.3^5 = 391.354
Enter a base number: 12
Enter exponent: 2.5
Entered number is not an integer!
This is my code so far:
#include <stdio.h>
int main() {
double base, result = 1;
int exp, i;
printf("Enter a base number: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%d", &exp);
for (i=1; i<=exp; i++) {
result *= base;
}
printf("%.2lf^%d = %.2lf", base,exp,result);
return 0;
}
It calculates the power of n successfully. But how do I add the "You didn't enter a number!" text when the input is wrong. Also, some results should be printed with 3 decimals, some with 6, etc.(depends on the result).