Originally Posted by
Eman
Ok dude, I have fixed your code as best as I can, because I have no idea what you want to do.
I am surprised you didn't get bugs when you ran it.
When you pass by point value, you don't have to dereference a the pointer again and use ampersand in your scanf(). That doesn't make sense. n and d have an address, so the address operator - & is irrelevant in this case.
as for your scanOperator(), the reason it closes is because you do not use it. You return a value - the value is floating somewhere in memory it isn't used. return 0 ; just closes the program.
Also your while loop is messed up.
if the condition is true, who do you terminate the while loop?
You do not decrement or increment.
Look at your code again, fix it and write back.
good luck
I thought that since in the while loop the user could re enter a value that would be outside the loop conditions, that would terminate the loop?
I've managed to get the program to scan for a value. There is still a slight problem though. The first two functions work and run fine. However, in the scanOperator function, if the user enters one of (+, -, *, /) the initial time, it will correctly print back the operator. However, if the user enters in an incorrect value, then regardless of what is printed the second time, it keeps saying it is incorrect. I can't seem to fix the problem which I believe would be in the loop.
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: ");
}
//this function will print the operator entered by the user
char scanOperator(void)
{
char op;
printf("Enter an arithmetic operator (+, -, *, /): ");
getchar();
scanf("%c",&op);
if (op =='+' || op =='-' || op=='*' || op =='/')
printf("You have entered: %c",op);
else
{
while (op !='+' || op !='-' || op!='*' || op !='/')
{
printf("The arithmetic operator you have entered is invalid.\n");
printf("Enter an arithmetic operator (+, -, *, /): ");
getchar();
scanf("%c",&op);
}
printf("You have entered: %c\n",op);
}
return op;
}
int main (void)
{
int a,b;
char operator;
scanFraction(&a,&b);
printFraction(a,b);
scanOperator();
return 0;
}