Thread: C Stock Sorting Program Issue

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21

    Post C Stock Sorting Program Issue

    Hey guys, I'm new here and just started C this year in college. Next year I'm going to work on c++.

    To kick my first post of, I have a question. I receive an error message with Visual c++ 2008 Express in the savetext function. Look like this:
    C Stock Sorting Program Issue-error-jpg

    Here's the code:


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    /////////////////
    #define SIZE 2
    /////////////////
    struct Stock
    {
        char name[10];
        int numShares;
        float buyShare,currPrice,fees;
        float initCost,currCost,profit;
    };
    /* Load the data from the keyboard and calculates 
    the Initial Cost, Current Cost, and Profit 
    for each stock via an array */
    
    
    void load(struct Stock s[], int n) 
    {
        int i;
        for(i=0;i<n;i++)
        {
            printf("Enter the Stock Name\n");
            printf(">");
            gets(s[i].name);
            printf("Enter the Number of Shares\n");
            printf(">");
            scanf("%d", &s[i].numShares);
            printf("Enter the Buying Price Per Share\n");
            printf(">");
            scanf("%f", &s[i].buyShare);
            printf("Enter the Current Price Per Share\n");
            printf(">");
            scanf("%f", &s[i].currPrice);
            printf("Enter the Yearly Fees\n");
            printf(">");
            scanf("%f", &s[i].fees);
    
    
            s[i].initCost = (float)s[i].numShares * s[i].buyShare;
            s[i].currCost = (float)s[i].numShares * s[i].currPrice;
            s[i].profit = s[i].currCost - s[i].initCost - s[i].fees;
    
    
            fflush(stdin);
        }
    }
    
    
    /* Sort the array of structures 
    on stock name and print the array 
    after the sort is completed */
    
    
    void sort(struct Stock s[], int n)
    {
        Stock t;
        for(int i=0;i<n-1;i++)
            for(int j=0;j<n-1;j++)
                if(strcmp(s[j].name, s[j+1].name)>0)
                {
                    t=s[j];
                    s[j]=s[j+1];
                    s[j+1]=t;
                }
    }
    
    
    /* Calculate and print the total profit for all of the stocks. 
    That is, find the sum of the 5 profits for each stock. In 
    addition, find and print out the number of stocks that 
    had a positive profit, the number of stocks that had a 
    negative profit, and the number of stocks that broke 
    even, that is had a profit of $0.00 */
    
    
    void calc(struct Stock s[],int n)
    {
        float total=0;
    
    
        int Pos=0;
        int Neg=0;
        int Even=0;
    
    
        for(int i=0;i<n;i++)
        {
            total +=s[i].profit;
            if (s[i].profit>0)
                ++Pos;
            else
            if (s[i].profit<0)
                ++Neg;
            else
                ++Even;
        }
    
    
        printf("%d of stocks broke Positive\n",Pos);
        printf("\t%d of stocks broke Negative\n",Neg);
        printf("\t\t%d of stocks broke Even\n",Even);
        printf("\n");
        printf("The Total Trofit is $0.2%f\n", total);
    }
    
    
    void print(struct Stock s[], int n)
        {
            for(int i=0;i<n;i++)
            {
                printf("Your stock name is %s\n", s[i].name);
                printf("\tYour Initial cost is $%0.2f\n", s[i].initCost);
                printf("\t\tYour Current cost is $%0.2f\n", s[i].currCost);
                printf("\t\t\tAnd your Profit is $%0.2f\n", s[i].profit);
                printf("\n");
                printf("Good job! ");
            }
        }
    //Save the array of structures to a text file.
    void savetext(struct Stock s[], int n)
    {
        FILE *f;
        f = fopen("e:\final.txt", "w"); 
        int i;
        for(i=0;i<n;i++)
        {
            fprintf(f,"%s\n", s[i].name);
            fprintf(f, "%d  %f  %f  %f  %f  %f  %f\n", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
        }
        fclose(f);
        fflush(stdin);
    }
    //Retrieve and print the text file.
    void loadtext(struct Stock s[], int n)
    {
        FILE *f;
        f = fopen("e:\final.txt", "r");
        int i;
        for(i=0;i<n;i++)
        {
            fgets(s[i].name, sizeof(s[i].name), f);
            fscanf(f,"%d%f%f%f%f%f%f\n", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
        }
        fclose(f);
    }
    //Save the array of structures to a binary file.
    void savebin(struct Stock s[], int n)
    {
        FILE *f;
            f = fopen("e:\final.bin", "wb");
        fwrite(&s, sizeof(s[10]), n, f);
        fclose(f);
    }
    //Retrieve and print the binary file.
    void loadbin(struct Stock s[], int n)
    {
        FILE *f;
        f = fopen("e:\final.bin", "rb");
        fwrite(&s, sizeof(s[10]), n, f);
        fclose(f);
    }
    
    
    void main()
    {
        Stock s[SIZE];
        load (s, SIZE);
        sort (s, SIZE);
        savetext (s, SIZE);
        savebin (s, SIZE);
        print (s, SIZE);
        calc (s, SIZE);
        loadtext (s, SIZE);
        print (s, SIZE);
        loadbin (s, SIZE);
        print (s, SIZE);
        system("PAUSE");
    }
    You can also see it here on Pastebin.com: [C] csort_struct - Pastebin.com

    Thanks for help in advance!
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting issue
    By Ssalty in forum C++ Programming
    Replies: 1
    Last Post: 05-18-2011, 10:03 AM
  2. Stock Data
    By bskaer1 in forum C++ Programming
    Replies: 0
    Last Post: 10-16-2010, 11:59 PM
  3. compare issue while sorting
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2004, 10:34 AM
  4. Stock market
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2003, 05:12 PM
  5. Stock Taking program
    By C Babe in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2003, 07:40 PM

Tags for this Thread