0

Here is what I am suppose to do:

  1. Create struct stock - name[20], shares, buyprice,currprice, buycost, currcost, profit.
  2. Load from keyboard - name, shares, buyprice, currprice, currcost,profit, buycost.
  3. Sort on profit from high to low
  4. Find total profit for all stocks
  5. Print how many stocks made money, lost money, and broke even.

Here is what I have so far:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define size 2
#include<stdlib.h>
struct stock
{
    char name[20];
    int shares;
    float buyp, currp, buycost, currcost, profit;
};
void load(struct stock s[], int n)
{
    int i;
    for (i = 0; i<n; i++)
    {
        printf("Enter name:");
        gets(s[i].name);
        fflush(stdin);
        printf("Enter shares:");
        scanf("%d", &s[i].shares);
        printf("Enter buyprice");
        scanf("%f", &s[i].buyp);
        printf("Enter current price");
        scanf("%f", &s[i].currp);

        s[i].currcost = s[i].shares * s[i].currp;
        s[i].buycost = s[i].shares * s[i].buyp;
        s[i].profit = s[i].currcost - s[i].buycost;


    }
}

void print(struct stock s[], int n)
{
        int ne,p,z;
        ne = p = z =0;
    for (int i=0;i<n;i++)
    {
        if(s[i].profit<0)
            ne++;
        if (s[i].profit>0)
            p++;
        if(s[i].profit==0)
            z++;
        printf("Amount of stocks made money:%d\n",p);
        printf("Amount of stocks lost money:%d\n",ne);
        printf("Amount of stocks broke even:%d\n",z);
        printf("The current cost is:%f\n",s[i].currcost);
        printf("The profit is:%f\n",s[i].profit);
        printf("The buycost is:%f\n",s[i].buycost);
    }
}
void totprofit(struct stock s[], int n)

{
    int count = 0,  i;
    float totprofit = 0.0;
    for (i = 0; i < n; i++)
    {
        totprofit +=s[i].profit;
        printf("Total profit is:%f\n",totprofit);
        }
    }

void sort(struct stock s[], int n)
{
    int i; int j;
    stock t;
    for (i = 0; i<n - 1; i++)
        for (j = 0; j<n - 1; j++)
            if (s[j].profit<s[j + 1].profit)
            {
                t = s[j]; s[j] = s[j + 1]; s[j + 1] = t;
            }
}

void main()
{
    stock s[size];
    load(s, size);
    print(s,size);
    totprofit(s, size);
    sort(s,size);
    system("pause");
}

My problem is that when it compiles, it works perfectly until it asks for the name again(because the program runs twice). Why?

Enter name:
Apple
Enter shares:
5
Enter buyprice:
3
Enter current price:
4
Enter name:
Enter shares:
3
Enter buyprice:
4
Enter current price:
5
Amount of stocks made money:1
Amount of stocks lost money:0
Amount of stocks broke even:0
The current cost is:20.000000
The profit is:5.000000
The buycost is:15.000000
Amount of stocks made money:2
Amount of stocks lost money:0
Amount of stocks broke even:0
The current cost is:15.000000
The profit is:3.000000
The buycost is:12.000000
Total profit is:5.000000
Total profit is:8.000000
Press any key to continue . . .
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TheEWL2
  • 79
  • 1
  • 6

1 Answers1

1

You have the function void totprofit has the same name as your variable float totprofit.

Change at least one of them.

As for step 7, I'm not sure what you are asking.

Matthew Carlson
  • 654
  • 5
  • 12