简体   繁体   English

为什么我的 C 程序在第一个 for 循环完成后崩溃?

[英]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.所以基本上在计算机完成要求您输入所有数据(第一个 for 循环)之后,编译器就崩溃了,我很困惑。 I'm using code blocks 16.01 with the mingw32 gcc compiler.我将代码块 16.01 与 mingw32 gcc 编译器一起使用。

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.它应该允许用户输入 5 种不同股票的 4 个不同数据点,然后计算每只股票的每个数据点的初始成本、当前成本和利润,然后将它们显示给用户。 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.您为 4 个元素定义了它,但分配了 5 个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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