## I can't tell what's wrong with this code

### I can't tell what's wrong with this code

I'm trying to make a program that helps the user with math where they can specify what operations they want to use, the number range, and how many problems they want to solve. But when it compiles it skips the part where the user inputs the operation. This is what I have so far, and I think the problem is with "//Let the user input operation
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.
Last edited by ChippZanuff on Tue Mar 02, 2010 11:19 pm, edited 2 times in total.
ChippZanuff
New User

Posts: 20
Joined: Fri May 30, 2008 12:49 am
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

What kind of error are you getting?

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat

sanddbox
Expert

Posts: 2354
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

it doesn't give an error but it won't let me input the operation. it just skips over that part. and when it gets to the number ranges it closes since theres no operation to tell it which equations to use.
ChippZanuff
New User

Posts: 20
Joined: Fri May 30, 2008 12:49 am
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

Can you pastebin it so I can haz some code highlighting?

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat

sanddbox
Expert

Posts: 2354
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

I would if I knew how.

-- Tue Mar 02, 2010 10:51 pm --

Is this what you want?
Code: Select all
` Its supposed to help the user increase his or her skills in basic math bygiving them simple math equations (the number of which is determined bythe user) to solve according to the criteria defined by them such asoperations and range of numbers. Its supposed to check the input with thecorrect answer and tell the user his or her score at the end of the program#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;}`
ChippZanuff
New User

Posts: 20
Joined: Fri May 30, 2008 12:49 am
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

By syntax highlighting, I meant this.

Are you sure the problem is with that scanf statement? Try printing out the value of op afterwards. Is it null?

EDIT: You say it skips the part where it asks for the operation? Can you post the output of running your program?

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat

sanddbox
Expert

Posts: 2354
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

the problem may not be with the scanf statement but i'm pretty sure it is. it won't even let me enter anything for the operator. it prints enter operation: then prints enter low range.
ChippZanuff
New User

Posts: 20
Joined: Fri May 30, 2008 12:49 am
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

forget scanf ever existed

P.S. http://c-faq.com/stdio/scanfprobs.html

tgoe
Contributor

Posts: 527
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)

### Re: I can't tell what's wrong with this code

@ChippZanuff: Call fflush() with stdin as the parameter after your scanf statements to clear the buffer of other characters. That should solve your problem.
- Apologies to all who I have flamed in the past. Thanks mods for unbanning me.

ckw100 wrote:so i have been pacticeing my batch file hacking for networks
xcurious
Experienced User

Posts: 79
Joined: Sun Sep 21, 2008 3:49 pm
Blog: View Blog (0)