Sscanf

This is a discussion on Sscanf within the C Programming forums, part of the General Programming Boards category; Hi, I am only just starting to learn C and am trying to write a simple compound interest finding program. ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    Sscanf

    Hi, I am only just starting to learn C and am trying to write a simple compound interest finding program.

    All 4 of the fgets/sscanf statements give this compile error, althoguh I can't work out how to fix it.

    Error E2285 compound.cpp 16: Could not find a match for 'fgets(char,int,FILE *)' in function main()
    Error E2268 compound.cpp 16: Call to undefined function 'fgets' in function main()
    Error E2034 compound.cpp 17: Cannot convert 'int' to 'const char *' in function main()
    Error E2342 compound.cpp 17: Type mismatch in parameter '__buffer' (wanted 'const char *', got 'char
    ') in function main()

    Code:
    /* Financial Math - Compound Interest */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
    
    int principal = 0, interestRate = 0, numberUnits = 0, timeUnits = 0, amount = 0;
    char principalInput, interestInput, numberInput, timeInput;
    
    printf("Compound interest is calculated with the use of the compound interest formula; A = P[(1 + r/100n)^nt]");
    printf("\n Principal (initial amount): ");
    fgets(principalInput, 10, stdin);
    sscanf(principalInput, "%i", &principal);
    
    printf("\n Interest rate (%): ");
    fgets(interestInput, 10, stdin);
    sscanf(interestInput, "%i", &interestRate);
    
    printf("\n Number of times compounded per time unit: ");
    fgets(numberInput, 10, stdin);
    sscanf(numberInput, "%i", &numberUnits);
    
    printf("\n Number of time units: ");
    fgets(timeInput, 10, stdin);
    sscanf(timeInput, "%i", &timeUnits);
    
    amount = (principal * (1 + (interestRate/(100 * numberUnits)))) pow(numberUnits * timeUnits);
    printf("\n Compound interest = $%i", amount);
    return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Thats because, the fgets and sscanf function are expecting a char array but not just a char.

    fgets(principalInput, 10, stdin);
    sscanf(principalInput, "%i", &principal);

    principalInput should be a char array!

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    amount = (principal * (1 + (interestRate/(100 * numberUnits)))) pow(numberUnits * timeUnits);
    There is something wrong with that calculation.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Thanks for the help. I fixed the char problem and are working on the calculation now.

    Principal and number of times are both showing as 0. Interest rate is showing at 929269760 and number of years is showing at a similar ridiculous rate. Why?

    Code:
    /* Financial Math - Compound Interest */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
    
    printf("Financial Math - Compound Interest v2.0 \n");
    
    float principal = 0, interestRate = 0, numberUnits = 0, timeUnits = 0, amount01, amount02, power, test;
    char principalInput[100], interestInput[100], numberInput[100], timeInput[100];
    
    printf("Compound interest is calculated with the use of the compound interest formula; A = P[(1+r/100n)^nt]");
    printf("\nPrincipal (initial amount): ");
    fgets(principalInput, 10, stdin);
    sscanf(principalInput, "%d", &principal);
    
    printf("Interest rate (%): ");
    fgets(interestInput, 10, stdin);
    sscanf(interestInput, "%d", &interestRate);
    
    printf("Number of times compounded per time unit: ");
    fgets(numberInput, 10, stdin);
    sscanf(numberInput, "%d", &numberUnits);
    
    printf("Number of years: ");
    fgets(timeInput, 10, stdin);
    sscanf(timeInput, "%d", &timeUnits);
    
    printf("	Principal: %d, Interest rate: %d", principal, interestRate);
    printf("\n	Number of times: %d, Number of years: %d \n", numberUnits, timeUnits);
    
    amount01 = principal * (1 + (interestRate/(100 * numberUnits)));
    power = (numberUnits * timeUnits);
    amount02 =  pow(amount01, power);
    printf("Compound interest = $%i", amount02);
    return 0;
    }
    Last edited by Dink87522; 10-05-2009 at 07:58 AM.

  5. #5
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974
    be mindful of your printf codes...you're printing floats as a %d...%d is for integers...that's why you're getting the odd answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf error
    By roaan in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 06:44 AM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  3. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  4. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 03:43 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM

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