简体   繁体   中英

Why does my C program crash after the first for loop is completed?

So basically after the computer finishes asking you to input all of the data (the first for loop) the compiler just crashes and I'm confused. I'm using code blocks 16.01 with the mingw32 gcc compiler.

As A side note, if you have the time, could you give me some ideas as to how you would improve this program? I'm talking efficiency wise and less redundancy. It's supposed to allow the user to input 4 different data points for 5 different stocks, and then calculate initial cost, current cost, and profit for each of the data points for each stock, then display them out to the user. As you can see, I just shoved all the data points for all the stocks in a single array (called "cost") and just did my calculations within the array. I know the logic and execution is messy, so that's why I'm asking you guys for alternatives.

#include <stdio.h>
#include <string.h>
void main () {
    const char *stock [5];
    int i = 0, j = 0, k = 0;
    float stockData[4], cost[15];
    stock[0] = "IBM";
    stock[1] = "ORACLE";
    stock[2] = "SUN MICRO";
    stock[3] = "LINKSYS";
    stock[4] = "CISCO";

    for (;i<=4;i++){
        printf("Enter the number of shares, buying price per share, current price per share, and the yearly fees for %s: \n", stock[i]);
        scanf ("%f%f%f%f",&stockData[0],&stockData[1],&stockData[2],&stockData[3]);
            if (i==0){
                cost[0] = (stockData[0]*stockData[1]);
                cost[1] = (stockData[0]*stockData[2]);
                cost[2] = (cost[1]-cost[0]-stockData[3]);
                memset(stockData, 0, sizeof(stockData));
                }
            if (i==1){
                cost[3] = (stockData[0]*stockData[1]);
                cost[4] = (stockData[0]*stockData[2]);
                cost[5] = (cost[4]-cost[3]-stockData[3]);
                memset(stockData, 0, sizeof(stockData));
            }
            if (i==2){
                cost[6] = (stockData[0]*stockData[1]);
                cost[7] = (stockData[0]*stockData[2]);
                cost[8] = (cost[7]-cost[6]-stockData[3]);
                memset(stockData, 0, sizeof(stockData));
            }
            if (i==3){
                cost[9] = (stockData[0]*stockData[1]);
                cost[10] = (stockData[0]*stockData[2]);
                cost[11] = (cost[10]-cost[9]-stockData[3]);
                memset(stockData, 0, sizeof(stockData));
            }
            if (i==4){
                cost[12] = (stockData[0]*stockData[1]);
                cost[13] = (stockData[0]*stockData[2]);
                cost[14] = (cost[13]-cost[12]-stockData[3]);
                memset(stockData, 0, sizeof(stockData));
            }
        }

        for (;j<=4;j++){
            printf("Stock Name: %s, Initial Cost: $%.2f, Current Cost: $.2%f, Profit: $.2%f ", stock[j],cost[++k],cost[++k],cost[++k]);
        }
    }

EDIT: it was the array lengths that was causing the problem. I fixed it in the original code, now I'm just getting weird outputs. I will update again If I can't fix it myself.

Check your stock array. You defined it for 4 elements, but assigned 5 elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM