scanf("%c", &op);"
- Code: Select all
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
int main()
{
//Initialize all variables
char op;
int i=0, lowRange, highRange, rand1, rand2, answer, input, score;
int numProblems;
srand((unsigned)time(NULL));
//Introduce user to program and ask for prefered operation
printf("\n\n\t\t\tMathTutor 1.0\n");
printf("Hi! Welcome to MathTutor! This program will help you become");
printf(" more efficient at basic math skills by presenting simple");
printf(" equations for you to solve and then providing the correct ");
printf("answer. To begin, please enter the number of equations you");
printf(" wish to practice: ");
scanf("%d", &numProblems);
//Ask for operation
printf("Next, please enter the operation you would like to practice.");
printf("\n\t(i.e. +, -. *, or /)\nOperation: ");
//Let the user input operation
scanf("%c", &op);
//Validate 'op' by using isalnum() to see if value is a-z, A-Z, or 0-9)
while(isalnum(op))
{
//Alert the user to improper input
printf("\n\nThat is an invalid operator. Please enter a:\n\t");
printf("\"+\" for addition,\n\t\"-\" for subtraction,\n\t");
printf("\"*\" for multiplication,\n\tor \"/\" for division.");
printf("Please re-enter the operation you wish to practice: ");
//Get operator
scanf("%c", &op);
}
//Ask user for range of numbers
printf("\nWhat range of numbers would you like to work with?\n");
printf("\tLowest number:");
scanf("%d", &lowRange);
printf("\tHighest number:");
scanf("%d", &highRange);
//Validate Range
while(lowRange>=highRange)
{
printf("ERROR: The \"low\" range value is greater than or ");
printf("equal to the \"high\" range value. Please enter the");
printf(" number range values again.\n\tLOWEST number:");
scanf("%d", &lowRange);
printf("\n\tHIGHEST number:");
scanf("%d", &highRange);
}
//Begin presenting five equations for user's preferred operation for
//the number of times desired
if(op=='+')
{
printf("Answer the following equations:");
for(i = 0; i < numProblems;)
{
rand1 = (rand() % (highRange-lowRange)) + lowRange;
rand2 = (rand() % (highRange-lowRange)) + lowRange;
answer=rand1+rand2;
printf("\n\t%d + %d = ", rand1, rand2);
scanf("%d", &input);
if(answer==input)
{
printf("Correct!\n");
score= ((score + 1) / numProblems) * 100;
}
else
{
printf("Sorry! The answer is %d.", answer);
}
i++;
}
printf("Your score is: %d\n", score);
}
if(op=='-')
{
printf("Answer the following equations:");
for(i = 0; i < numProblems;)
{
rand1 = (rand() % (highRange-lowRange)) + lowRange;
rand2 = (rand() % (highRange-lowRange)) + lowRange;
answer=rand1-rand2;
printf("\n\t%d - %d = ", rand1, rand2);
scanf("%d", &input);
if(answer==input)
{
printf("Correct!\n");
score= ((score + 1) / numProblems) * 100;
}
else
{
printf("Sorry! The answer is %d.", answer);
}
i++;
}
printf("Your score is: %d\n", score);
}
if(op=='*')
{
printf("Answer the following equations:");
for(i = 0; i < numProblems;)
{
rand1 = (rand() % (highRange-lowRange)) + lowRange;
rand2 = (rand() % (highRange-lowRange)) + lowRange;
answer=rand1*rand2;
printf("\n\t%d * %d = ", rand1, rand2);
scanf("%d", &input);
if(answer==input)
{
printf("Correct!\n");
score= ((score + 1) / numProblems) * 100;
}
else
{
printf("Sorry! The answer is %d.", answer);
}
i++;
}
printf("Your score is: %d\n", score);
}
if(op=='/')
{
printf("Answer the following equations:");
for(i = 0; i < numProblems;)
{
rand1 = (rand() % (highRange-lowRange)) + lowRange;
rand2 = (rand() % (highRange-lowRange)) + lowRange;
answer=rand1/rand2;
printf("\n\t%d / %d = ", rand1, rand2);
scanf("%d", &input);
if(answer==input)
{
printf("Correct!\n");
score= ((score + 1.0) / numProblems) * 100;
}
else
{
printf("Sorry! The answer is %d.", answer);
}
i++;
}
printf("Your score is: %d\n", score);
}
return 0;
}
If anyone can tell me what I'm doing wrong I would really appreciate it.





