Reputation: 11
I am currently coding something in PuTTY and seem to be having technical difficulties with my if statements. My program is currently incomplete, but I wanted to fix this bug before I complete the code. The program will compile fine and prints; however, only doing the first type of differentiation works and trying to do integration or another type of differentiation exits the program:
int main()
{
int probtype, diftype, intype, vartype, vartest;
probtype = 0;
diftype = 0;
intype = 0;
vartype = 0;
vartest = 0;
char variable, variable2;
float fpower, fconstant, fpower2, fconstant2, fdivisor;
int ipower, iconstant, ipower2, iconstant2, idivisor;
printf("Alright, starting off would you like to do differentiation or integration? Type '1' for differentiation or '2' for integration. ");
scanf("%d", &probtype);
if (probtype == 1)
{
printf("Okay, what kind of differentiation problem are you interested in? Type 1 for 'Power Rule with a Constant', Type 2 for 'Product Rule', Type 3 for 'Quotient Rule, Type 4 for 'Chain Rule', Type 5 for 'Trigonometric Problems', Type 6 for 'Exponential Problems' or Type 7 for 'Natural Log Problems. ");
scanf("%d", &diftype);
if (diftype == 1)
{
printf("Before we begin. Type 1, if the constant and power are both integers, type 2 if the constant is a decimal and the power is an integer, and type 3 for all other cases. " );
scanf("%d", &vartype);
if (vartype == 1)
{
printf("First off enter the function's constant. If it has no visible constant enter 1. ");
scanf("%d", &iconstant);
printf("Alright, next enter the variable you are using. ");
scanf("%s", &variable);
printf("Finally, enter the power of the variable. ");
scanf("%d", &ipower);
iconstant = iconstant * ipower;
ipower = ipower - 1;
if (ipower == 0)
{
printf("The derivative of the function would be: %d \n", iconstant);
}
else
{
printf("The derivative of the function would be: %d%s^%d \n", iconstant, &variable, ipower);
}
}
if (vartype == 2)
{
printf("First off enter the function's constant. Enter it in DECIMAL form! ");
scanf("%f", &fconstant);
printf("Next, enter the variable you are using. ");
scanf("%s", &variable);
printf("Finally, enter the power of the variable. ");
scanf("%d", &ipower);
fconstant = fconstant * ipower;
ipower = ipower - 1;
if (ipower == 0)
{
printf("The derivative of the function would be %f \n", fconstant);
}
else
{
printf("The derivative of the function would be %f%s^%d \n", fconstant, &variable, ipower);
}
}
if (vartype == 3)
{
printf("First off, enter the function's constant. Enter it in DECIMAL form! ");
scanf("%f", &fconstant);
printf("Okay, now enter which variable you are using. ");
scanf("%s", &variable);
printf("Finally, enter the power of the variable. Enter this in DECIMAL form also! ");
scanf("%f", &fpower);
fconstant = fconstant * fpower;
fpower = fpower - 1;
printf("The derivative of the function would be: %f%s^%f \n", fconstant, &variable, fpower);
}
else if (diftype == 2)
{
printf("Enter the first function's constant. If it doesn't have one, type a 1 ");
}
else if (diftype == 3)
{
printf("Quotient Rule");
}
else if (diftype == 4)
{
printf("Chain Rule");
}
else if (diftype == 5)
{
printf("Trigoometric Problems");
}
else if (diftype == 6)
{
printf("Exponential Problems");
}
else if (diftype == 7)
{
printf("Natural Log Problems");
}
}
else if (probtype == 2)
{
printf("Okay, now what kind of integration problem are you interested in? Type 1 for 'Indefinite Integrals', Type 2 for 'Definite Integrals', Type 3 for 'Substitution', Type 4 for 'Trignometric Integrals', Type 5 for 'Integrations by Part', Type 6 for 'Exponential Problems', Type 7 for 'Natural Log Problems. ");
scanf("%d", &intype);
if (intype == 1)
{
printf("Before we get started: type 1 if both the constant and power are integers, type 2 if the constant is an integer and the power is a decimal, type 3 if the constant is a decimal and the power is an integer, and type 4 if both are decimals." );
scanf("%d", &vartype);
if (vartype == 1)
{
printf("Alright first off, enter the constant. If there is no constant, type a 1. " );
scanf("%d", &iconstant);
printf("Is there a variable in this equation? Type 1 if there is, Type 2 if there isn't. ");
if (vartest == 2)
{
printf("The indefinite integral of the function is: %dx + C", iconstant);
}
else
{
printf("Next, enter the variable that will be used for the problem. ");
scanf("%s", &variable);
printf("Finally enter the power of the function. ");
scanf("%d", &ipower);
ipower = ipower + 1;
idivisor = ipower + 1;
printf("The indefinte integral of the function is: %d%s^%d / %d \n", iconstant, &variable, ipower, idivisor);
}
}
}
else if (intype == 2)
{
printf("Definite Integrals");
}
else if (intype == 3)
{
printf("Substitution");
}
else if (intype == 4)
{
printf("Trignometric Integrals");
}
else if (intype == 5)
{
printf("Integrations by Part");
}
else if (intype == 6)
{
printf("Exponential Problems");
}
else if (intype == 7)
{
printf("Natural Log Problems");
}
}
}
}
I can't seem to find what is causing the error, I apologize if my information is not specific enough. I am aware there are more efficient ways of doing this program, so please don't change my code, but rather just show me what I did wrong and how I can fix it. Thank you.
Upvotes: 0
Views: 608
Reputation: 206567
One problem I see is:
scanf("%s", &variable);
This is syntactically correct but semantically incorrect. Since variable
is of type char
, you really cannot read a string into it. You end up accessing memory that you are not supposed to and it leads to undefined behavior.
You need something like:
char variable[100]; // Make the array as large you need to
scanf("%s", variable);
PS Fixing this may not solve any other problems.
Upvotes: 0
Reputation: 121347
It's because the condition else if (probtype == 2)
is inside the condition if (probtype == 1)
and connected to some other if
.
Instead add another brace before:
} // <--- Add an extra brace here in addition to what you have.
else if (probtype == 2)
{
and remove one at the end of your outer if
statement.
You have too many nested if else statements. I suggest you re-write your code to use switch
-case
and/or use separate functions for sub-tasks so that your code is more readable and you'd be able to debug easily.
Upvotes: 1