Originally Posted by
jimblumberg
Did you even read the documentation for scanf()? This function requires at least 2 parameters. Check the links I provided in my last post.
Jim
Sorry about that I didn't realize they were links until just now. scanf() is now correctly implemented thanks for the links.
The issue now is it repeats from prompt1 and 2:
Enter your first number: 1
Enter your second number: 2
Enter your first number: 1
Enter your second number: 2
Enter your first number: 11
Enter your second number: 2
Enter your first number: 1
Enter your second number: 2
Enter your first number: 1
Enter your second number: 2
Enter your first number: 1
Enter your second number: 2
Enter your first number: 1
Enter your second number: 2
Enter your first number: 1
Enter your second number: 2
Enter your first number:
Code:
#include <stdio.h>#include <stdlib.h>
double addfunc(double a, double b); //Add function
double subfunc(double a, double b); //Subtract function
double divfunc(double a, double b); //Divide function
double multifunc(double a, double b); //Multiply function
void inputfunc(double *a, double *b); //Input from console function
void calculate(double *a, double *b); //Choice of operation and calling function
main()
{
double answer1 = 0; //Holds first value inputted
double answer2 = 0; //Holds second value inputted
inputfunc(&answer1, &answer2); //inputfunc function call passing back answer1/2
calculate(&answer1, &answer2); //calculate function call passing answer1/2
}
double addfunc(double a, double b) //Adds two numbers
{
return a+b;
}
double subfunc(double a, double b) //Subtracts two numbers
{
return a-b;
}
double divfunc(double a, double b) //Divides two numbers
{
return a/b;
}
double multifunc(double a, double b) //Multiplies two numbers
{
return a*b;
}
void inputfunc(double *a, double *b) //function to provide input
{
double temp1 = .12351; //temp1 default value
double temp2 = .12351; //temp1 default value
do //do loop continues to repeat if default value are not changed
{
printf("Enter your first number: "); //Ask for input
scanf("%e",temp1); //Collect console input assign to temp1
printf("Enter your second number: "); //Ask for input2
scanf("%e",temp2); //Collect console input assign to temp2
}
while(temp1 == .12351 || temp2 == .12351);
*a = temp1; //pass values collected back
*b = temp2; //pass values collected back
}
void calculate(double *a, double *b) //calculate function that asks for input on operation and calculate/outputs result
{
char operation = 'Z'; //preset value also used in while loop
double temp1 = *a; //temp var to hold passed value
double temp2 = *b; //temp var to hold passed value
while (operation == 'Z') //while loop to keep entry going if value entered doesnt meet if statement condition
{
printf("\nWhich operation would you like to use?(A/S/M/D/(Q)uit): "); //prompt for user to enter operation
operation = getchar(); //operation collecting input
if (operation != 'A' || operation != 'S' || operation != 'M' || operation != 'D' || operation != 'Q') //checking for bad input
{
continue; //if bad input continues loop
}
else
{
switch (operation) //switch statement that takes choice and calc/output result
{
case 'A':
printf("Result: %lf",addfunc(temp1,temp2));
break;
case 'S':
printf("Result: %lf",subfunc(temp1,temp2));
break;
case 'M':
printf("Result: %lf",multifunc(temp1,temp2));
break;
case 'D':
printf("Result: %lf",divfunc(temp1,temp2));
break;
case 'Q':
return;
}
}
operation = 'Z'; //resets var to allow for looping on bad input
}
}