So i have to write a calculator program that
a.entering the letter q quits (i got this part)
b.if more than one character is added into the opperator return an error message and give another chance (i think i need to use a char buf[80] and then state that
if (char[2]!='\n'){ continue;})
c.if a user enters an operator when a number is expected tell them to enter another number(i have no idea)
d. add an "n" opperator that skips entering a value for the second number and causes doOp to set acc to the negative value(i got this part)
e.add an s opperator that stores the 1st number entered (i think i did the correctly)
f. add a p opperator to set op to '+'. thus adding together the stored number and the new number meaning adding the new acc to the stored one. (i attempted and it doesnt work but i dont really understand how to do this)
Any help would be greatly appreciated!
#include<stdio.h>
#include<ctype.h>
float mem;
float doOp(int op, float num1, float num2, float memory, float acc);
int main(int argc, char * argv[]) {
int c;
float num,acc;
char quit;
printf("ECE 15 Assignment 3 \n"); // Print: ECE 15 Assignment 3
printf("Cristina Zepeda\n\n"); // Print: Names of partners; end each name with \n
printf("Welcome to Calculator\n\n\n"); // Greeting
while(1) {
printf("Enter number:"); // Get number
scanf("%f",&acc) || scanf ("%c", &quit);
if (quit == 'q'){ //checking for q to quit
printf("Calculator exiting\n");
break;
}
printf("The number into acc is %f\n",acc); //prints the enter value onto the screen
while(1) { // Loop until clear
printf("Enter op:");
do c = getchar(); while(isspace(c)); // Get op
printf("The op char is:%c\n",c);
if(c)
if(c=='q') { // if q is entered then quit
printf("Calculator exiting\n");
return 0;
}
if (c=='n'){
goto answer;
} // skips entering num and goes to answer
if (c=='s'){
goto stored;
} // stores acc to mem
if (c=='p'){
printf("Calculator exiting\n"); ;
} // skips entering num and goes to answer
else if (c != '+' && c != '-' && c != '*' && c != '/' && c != 'n' && c != 's' && c != 'p') { // Checks to make sure opperation is acceptable
printf ("Error! invalid opperator. Please enter +, -, *, /, n, s, or p\n");
continue;
}
printf("Enter another number:");
scanf("%f",&num) || scanf ("%c", &quit); // Get number num
if (quit == 'q'){ //checking for q to quit
printf("Calculator exiting\n");
return 0;
}
printf("Number into num is: %f\n",num);
answer: {
acc = doOp(c, acc, num, mem, acc); // Perform op
printf("The answer is:"); // Display answer
printf("%f\n\n\n",acc);
stored: {
printf("\n\n\n");{
break;
}
}
}
}
}
return 0;
}
float doOp(int op, float num1, float num2 , float memory, float acc){
switch (op) {
case '+' : return num1 + num2;
break;
case '-' : return num1 - num2;
break;
case '*' : return num1 * num2;
break;
case '/' : return num1 / num2;
break;
case 'n' : return 0 - acc;
break;
case 's' : return mem=acc;
break;
case 'p' : return acc+mem;
break;
default: return num1;
}
}