A little help please

This is a discussion on A little help please within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have some problems with this program that does very basic calculations. Code: #include <stdio.h> #define true 1 ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    A little help please

    Hello everyone,

    I have some problems with this program that does very basic calculations.

    Code:
    #include <stdio.h>
    #define true 1
    #define false 0
    void displayMenu(void);
    char getOption(void);
    void calculations(void);
    void calculationsSubMenu(void);
    void calcEPS(void);
    
    typedef struct
    {    
        float npat[5];
        float eps[5];
        int total;
    }data_struct;    
    
    int main()
    {
    
    }
    
    
    
    void displayMenu(void)
    {
    
    }
    
    char getOption(void)
    {
    	
    }
    
    void calculationsSubMenu(void)
    {
       
    }
    
    void calculations(void)
    {
    	
    }
    
    void calcEPS (void)
    {
        int issue;
        int ch,counter,i;
        char name[20];
        data_struct data;
    
    
          printf("\n\n\n\n          FUNDAMENTAL ANALYSIS TEST PROGRAM\n");
          printf("\nPlease enter the name of the company: ");
          gets(name);
          printf("\nhow many would you like to calculate? : ");
          scanf("%d", &data.total);
          for (i= 0; data.total > i; i++)
          {
          
            counter++;
            printf("\n\n                       EPS %d ", counter);
            printf("\n                Earnings Per Share\n\n");
            printf("Please enter the net profit after tax(NPAT): ");
            scanf("%f", &data.npat);
            printf("\nPlease enter the number of shares on issue: ");
            scanf("%d", &issue); 
            data.eps[i] = data.npat[i] / issue;
            getchar();
            printf("\n");      
        }
        
        printf("\nthe last %d calculations for %s are", name, counter);
        counter =0;
        for (i = 0; data.total >i; i++)
        {
            counter++;
            printf("\n%d EPS is %0.5f ", counter, data.eps[i]);
        }    
            printf ("\n\nPress [Enter] to continue");
            while ((ch = getchar()) != '\n' && ch != EOF);
              
      
    
    }
    I have deleted all the menu code i dont think is needed.

    I have a few problems.

    1) the program crashes after doing a few large EPS calculations. I am using floating point data type which i thought could handle the large numbers..maybe not?
    2) related to #1 my array

    Code:
    typedef struct
    {    
        float npat[5];
        float eps[5];
        int total;
    }data_struct;
    maybe im mistaken how arrays work, but i only want a max of 5 years to be stored (i know this is not checked). But it seems when if i make it float eps[100], or float eps[500] the program can handle more calculations before crashing.
    3) I cant seem to get the name to display correctly, it is all garbage. not sure why this doesnt work as the function works perfectly as a seperate program.

    sorry for the long post, any help would be appreciated.

    Andrew

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,831
    > gets(name);
    NEVER use gets()
    See the FAQ

    > scanf("%f", &data.npat);
    This (if it does anything) will read into just the first array location.
    Your later use of npat[i] uses junk floats.
    Perhaps
    scanf("%f", &data.npat[i]);

    > for (i= 0; data.total > i; i++)

    I would write
    for (i= 0; i < data.total; i++)

    If you've been taught operand reversal in order to try and trap things like if ( foo = 0 ) by writing if ( 0 = foo ) {which generates an error), now you see why it's a bogus idea.
    The reverse of i < data.total is data.total >= i (not what you've written)

    As written, your code steps off the ends of the arrays.

    Re code formatting
    Please stick to only using spaces when indenting code. Your editor might handle random mixtures of spaces and tabs, but the board doesn't.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The reverse of i < data.total is data.total >= i (not what you've written)
    You're confusing two concepts here, Salem. The negation of i < data.total is i >= data.total. The operand order reversal of i < data.total is data.total > i, just like it was. Test?
    i = 5
    data.total = 5
    i < data.total -> false
    data.total >= i -> true
    thus they aren't the same.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

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