1. ## floating point problem

I am runnning into a big wall in writing my C program. so my program has to handle 3 simple math equations using information entered by the user. the format of entry is indicated in the printf statements. whenever i try to debug this program it solves the first equation just fine but immediately brings up garbage for the next two equations. i am using dev-c++ to write the program and to me my code looks fine, i would very greatly appreciate any help someone could give me a hand. attached is a screenshot of whats happening and below is the code i am using:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main() {

int varA, varB, varC, varD, varE, varF, varG, varH;
char ch1, ch2, ch3, ch4;
float varFA, varFB, varFC, varFD, varFE, varFF;

//Gather ints and perform the indicated operations

printf("Enter '(int * int) + (int / int) + (int %% int)': ");
scanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varA,&varB,&ch1,&varC,&varD,&ch2,&varE,&varF);
printf("You Entered: (%d * %d) %c (%d / %d) %c (%d %% %d)\n",varA,varB,ch1,varC,varD,ch2,varE,varF);
printf("The Equation Is Equal To: %d\n",(varA * varB) + (varC / varD) + (varE % varF));

//Second expression: (a / b) * (b / a)
printf("Enter '(float / float) * (float / float)':\n ");
scanf("(%f / %f)%*c%c%*c(%f / %f)",&varFA,&varFB,&ch3,&varFC,&varFD);
printf("You Entered: (%f / %f) %c (%f / %f)\n",varFA,varFB,ch3,varFC,varFD);
printf("The Equation Is Equal To: %03f\n",(varFA / varFB) * (varFC / varFD));

//Third expression: (a - b) % (int)(c * d)
printf("Enter '(int - int) %% (float * float)':\n ");
scanf("(%d - %d) %% (%f * %f)",&varG,&varH,&ch4,&varFE,&varFF);
printf("You Entered: (%d - %d) %c (%f * %f)\n",varG,varH,ch4,varFE,varFF);
printf("The Equation Is Equal To: %d\n",(varG - varH) % (int)(varFE * varFF));

scanf("%*c");
return 0;
}```

2. %*c should be *%c
I believe. You have many of these typos.

3. scanf returns when the matching of the control string fails.

> scanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)"
This leaves the \n on the input stream.
All well and good for the first expression, but it's a trap...

> scanf("(%f / %f)%*c%c%*c(%f / %f)"
This tries to match ( with \n, and fails.
It then returns immediately, leaving all the vars uninitialised.

Use this in place of each scanf
Code:
```char buff[BUFSIZ]
fgets( buff, BUFSIZ, stdin );
if ( sscanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varA,&varB,&ch1,&varC,&varD,&ch2,&varE,&varF) == 8 ) {
// parsed OK. do stuff
} else {
// that wasn't what was asked for
}```

4. Hey thanks for the help. When i try to compile with the code you suggested it displays 12 compiler errors:

Compiler: Default compiler
Executing gcc.exe...
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:14 : error: syntax error before "int"

C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:21 : error: `buff' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:21 : error: (Each undeclared identifier is reported only once
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:21 : error: for each function it appears in.)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:22 : error: `varA' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:22 : error: `varB' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:22 : error: `varC' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:22 : error: `varD' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:22 : error: `varE' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:22 : error: `varF' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:34 : warning: passing arg 2 of `sscanf' from incompatible pointer type

C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:45 : error: `varG' undeclared (first use in this function)
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:45 : error: `varH' undeclared (first use in this function)

Execution terminated

Code:
```#include <stdio.h>
#include <stdlib.h>

int main() {

char buff[1024]
int varA, varB, varC, varD, varE, varF, varG, varH;
char ch1, ch2, ch3, ch4;
float varFA, varFB, varFC, varFD, varFE, varFF;

//Gather ints and perform the indicated operations

printf("Enter '(int * int) + (int / int) + (int %% int)': ");
fgets( buff, 1024, stdin );
if ( sscanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varA,&varB,&ch1,&varC,&varD,&ch2,&varE,&varF) == 8 ) {
printf("You Entered: (%d * %d) %c (%d / %d) %c (%d %% %d)\n",varA,varB,ch1,varC,varD,ch2,varE,varF);
printf("The Equation Is Equal To: %d\n",(varA * varB) + (varC / varD) + (varE % varF));

} else {
printf("You did not enter a valid statement.");
}
//scanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varA,&varB,&ch1,&varC,&varD,&ch2,&varE,&varF);

//Second expression: (a / b) * (b / a)
printf("Enter '(float / float) * (float / float)': ");
fgets( buff, 1024, stdin );
if ( sscanf("(%f / %f)%*c%c%*c(%f / %f)",&varFA,&varFB,&ch3,&varFC,&varFD) == 5 ) {
printf("You Entered: (%f / %f) %c (%f / %f)\n",varFA,varFB,ch3,varFC,varFD);
printf("The Equation Is Equal To: %03f\n",(varFA / varFB) * (varFC / varFD));
} else {
printf("You did not enter a valid statement.");
//scanf("(%f / %f)%*c%c%*c(%f / %f)",&varFA,&varFB,&ch3,&varFC,&varFD);
}

//Third expression: (a - b) % (int)(c * d)
printf("Enter '(int - int) %% (float * float)':\n ");
fgets( buff, 1024, stdin );
if ( sscanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varG,&varH,&ch4,&varFE,&varFF) == 5 ) {
printf("You Entered: (%d - %d) %c (%f * %f)\n",varG,varH,ch4,varFE,varFF);
printf("The Equation Is Equal To: %d\n",(varG - varH) % (int)(varFE * varFF));
} else {
printf("You did not enter a valid statement.");
}

//scanf("(%d - %d) %% (%f * %f)",&varG,&varH,&ch4,&varFE,&varFF);
scanf("%*c");
return 0;
}```

5. Code:
```int main() {

char buff[1024];
int varA, varB, varC, varD, varE, varF, varG, varH;```
If you look at the errors, you get a clue where to look. There is a syntax error before int on line 14.

C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:14 : error: syntax error before "int"

6. The only problem now is, that it will not display the user entered equation. should i change the sscanf value from 8 to something? and do i need to get rid of the fgets new line char

Code:
```#include <stdio.h>
#include <stdlib.h>

int main() {

char buff[1024];
int varA, varB, varC, varD, varE, varF, varG, varH;
char ch1, ch2, ch3, ch4;
float varFA, varFB, varFC, varFD, varFE, varFF;

//Gather ints and perform the indicated operations

printf("Enter '(int * int) + (int / int) + (int %% int)': ");
fgets( buff, 1024, stdin );
if ( sscanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varA,&varB,&ch1,&varC,&varD,&ch2,&varE,&varF) == 8 ) {
printf("You Entered: (%d * %d) %c (%d / %d) %c (%d %% %d)\n",varA,varB,ch1,varC,varD,ch2,varE,varF);
printf("The Equation Is Equal To: %d\n",(varA * varB) + (varC / varD) + (varE % varF));

} else {
printf("You did not enter a valid statement.\n\n");
}
//scanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varA,&varB,&ch1,&varC,&varD,&ch2,&varE,&varF);

//Second expression: (a / b) * (b / a)
printf("Enter '(float / float) * (float / float)': ");
fgets( buff, 1024, stdin );
if ( sscanf("(%f / %f)%*c%c%*c(%f / %f)",&varFA,&varFB,&ch3,&varFC,&varFD) == 5 ) {
printf("You Entered: (%f / %f) %c (%f / %f)\n",varFA,varFB,ch3,varFC,varFD);
printf("The Equation Is Equal To: %03f\n",(varFA / varFB) * (varFC / varFD));
} else {
printf("You did not enter a valid statement.\n\n");
//scanf("(%f / %f)%*c%c%*c(%f / %f)",&varFA,&varFB,&ch3,&varFC,&varFD);
}

//Third expression: (a - b) % (int)(c * d)
printf("Enter '(int - int) %% (float * float)': ");
fgets( buff, 1024, stdin );
if ( sscanf("(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varG,&varH,&ch4,&varFE,&varFF) == 5 ) {
printf("You Entered: (%d - %d) %c (%f * %f)\n",varG,varH,ch4,varFE,varFF);
printf("The Equation Is Equal To: %d\n",(varG - varH) % (int)(varFE * varFF));
} else {
printf("You did not enter a valid statement.\n\n");
}

//scanf("(%d - %d) %% (%f * %f)",&varG,&varH,&ch4,&varFE,&varFF);
scanf("%*c");
return 0;
}```

7. Did you get rid of all your errors and warnings?

sscanf wants to know what string to scan.

Originally Posted by man page
int sscanf(const char *restrict s, const char *restrict format, ...);
s here is your string, format is the format specifiers, the dots are the variables, one or more that you supply.

8. Here are the only warnings it is giving me

Compiler: Default compiler
Executing gcc.exe...
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:22 : warning: passing arg 2 of `sscanf' from incompatible pointer type

C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:34 : warning: passing arg 2 of `sscanf' from incompatible pointer type
C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c:45 : warning: passing arg 2 of `sscanf' from incompatible pointer type

Execution terminated
Compilation successful

9. Yes, and how many arguments does sscanf want? Your first argument is the format specifiers.

10. Hehe im lost lol.. i guess im just not understanding the sscanf function

11. Heh, ok you need to tell sscanf what string it suppose to scan.

Code:
`sscanf( buff, "(%d * %d)%*c%c%*c(%d / %d)%*c%c%*c(%d %% %d)",&varA,&varB,&ch1,&varC,&varD,&ch2,&varE,&varF )`

12. Thank you so much! it works now