floating point problem

This is a discussion on floating point problem within the C Programming forums, part of the General Programming Boards category; I am runnning into a big wall in writing my C program. so my program has to handle 3 simple ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Unhappy 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;
    }
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    %*c should be *%c
    I believe. You have many of these typos.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    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
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    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...
    gcc.exe "C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b .c" -o "C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.exe " -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
    C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c: In function `main':
    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. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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"
    Last edited by Subsonics; 04-26-2010 at 12:13 AM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    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;
    }
    Attached Images Attached Images  

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Did you get rid of all your errors and warnings?

    sscanf wants to know what string to scan.

    Quote 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.
    Last edited by Subsonics; 04-26-2010 at 12:43 AM.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Here are the only warnings it is giving me

    Compiler: Default compiler
    Executing gcc.exe...
    gcc.exe "C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b .c" -o "C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.exe " -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
    C:\Users\Ninja\Documents\Thads\PCC\133U\lab3b.c: In function `main':
    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. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, and how many arguments does sscanf want? Your first argument is the format specifiers.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Hehe im lost lol.. i guess im just not understanding the sscanf function

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Thank you so much! it works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String and Floating Point Conversion Help
    By dfghjk in forum C++ Programming
    Replies: 14
    Last Post: 05-04-2008, 01:11 PM
  2. Floating point checking problem
    By ikkiutsu in forum C Programming
    Replies: 8
    Last Post: 12-03-2003, 06:35 AM
  3. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 11:55 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21