Hey!
I'm trying to write a C program that will eventually do operations with fractions. However, I'm stuck on one part. The section of code for the function scanOperator, when run through the program, does not allow for user input. The weird thing is when I change %c to %d, it will allow input. However, with %c, it simply prints the line "Enter an arithmetic operator (+, -, *, /)", then exits the program. I can't figure out why it does this.
Thanks in advance!
Code:
#include <stdio.h>
//this function prints the user inputted fractions
void printFraction (int n, int d)
{
printf("%d/%d\n",n,d);
}
//this function will scan for user input
void scanFraction (int *n, int *d)
{
printf("Enter the numerator and denominator of a common fraction: ");
scanf("%d %d",&*n,&*d);
if (*d <= 0)
{
while (*d <= 0)
{
printf("The common fraction you entered is invalid.\n");
printf("Enter the numerator and denominator of a common fraction: ");
scanf("%d",&*n);
scanf("%d",&*d);
}
}
else
printf("You have entered: ");
printf("You have entered: ");
}
//this function will print the operator entered by the user
char scanOperator(void)
{
char op;
printf("Enter an arithmetic operator (+, -, *, /): ");
scanf("%c",&op);
return op;
}
int main (void)
{
int a,b;
char operator;
scanFraction(&a,&b);
printFraction(a,b);
scanOperator();
return 0;
}