I’ve been teaching myself the basics of programming with this book I bought. Enjoying it so far, I get it for the most part, but am a true beginner. In this next episode of, “I can’t figure out what is going on.” I am using if statements.
When my variable testFor == 10, the first if statement is supposed to fire but for one reason or another it defaults to the else. I scanf to read an int from the keyboard, store it in num1. I pass num1 to my ifStatement function. A local variable testFor = num1. When testFor == 10, I get the default case. Seems to work for all other cases though. Screen shots and code for posterity. I have some seemingly random printf in there but I was trying to see what was being assigned to num1 and testFor…shows 10.
IDK why if those were assigned 10 the if (testFor == 10) does not fire.
Any help is appreciated. Up to this point, I have learned some important things from each help thread I’ve put up. Look forward to your feedback.
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
void getData (int* num1);
void ifStatement (int num1);
int main (void)
{
int num1 = 0;
getData(&num1);
printf("%d\n", num1);
ifStatement(num1);
return 0;
}
void getData (int* num1)
{
printf("Be careful with the integer you type. Choose from 0-10\n");
scanf("%d", num1);
return;
}
void ifStatement (int num1)
{
int testFor = num1;
float squareTest = 0;
float prod = 0;
printf("%d\n", testFor);
if (testFor == 10)
{
squareTest = pow(testFor, 2);
}
else ;
if (testFor == 9)
{
printf("RE-ROLL! Enter a number 0-10\n");
scanf("%d", &testFor);
}
else ;
if (testFor == 2 || testFor == 3)
{
prod = testFor * 99;
printf("Multiply by 99 and we get %.2f\n", prod);
}
else
{
printf("ERROR: YOU DIDN'T ENTER A NUMBER 0-10 DID YOU? JUMP IN A LAKE.");
}
return;
}
Here is what I get in console: