简体   繁体   English

输入超过 6 个用户的值后程序停止工作

[英]Program stops working after entering the value for more than 6 users

I have a school project which asks me to write a code using struct and functions, regarding sales in Greek cities.我有一个学校项目,要求我使用结构和函数编写一个关于希腊城市销售的代码。 You are prompted to enter the values of 'em.系统会提示您输入 'em 的值。 The program works just fine if I enter that there's 6 salesmen, but a number larger than that makes it stop after entering all the values.. An example would be:如果我输入有 6 个销售员,该程序就可以正常工作,但是比这个数字更大的数字使它在输入所有值后停止。一个例子是:

Number of salesmen: 7销售员人数:7

( (

  1. Enter the his id:输入他的id:

  2. Enter his surname:输入他的姓氏:

  3. Enter the number of sales:输入销售数量:

  4. Enter the area code:输入区号:

) x7 times. ) x7 次。

Then, when you enter the last value, it would just stop.然后,当您输入最后一个值时,它就会停止。 Not exit, not stop responding/crash, literally just stop, and you would be unable to type anything more, only option would be to exit.不退出,不停止响应/崩溃,实际上只是停止,您将无法再输入任何内容,唯一的选择是退出。

Thing is, there's no error or warning in both the build messages and the log, which has me quite confused.事实是,构建消息和日志中都没有错误或警告,这让我很困惑。

My guess is that the error is in the function "calcSales" but I wanted to post the whole code just in case you need more info on it.我的猜测是错误出在函数“calcSales”中,但我想发布整个代码,以防万一您需要更多信息。

Could you take a look at the code and tell me if you find anything wrong?你能看一下代码,如果你发现任何错误,请告诉我吗? Thank you.谢谢你。

#include <stdio.h>
#include "genlib.h"
#include <simpio.h>
#include <string.h>
#define N 20
#define M 4

struct{
     int id;
     char surname[16];
     long sales;
     int area;
} salesmen[N];

void info(int *count);
void calcSales(int *count);

int main(){

     int count;

     printf("Give me the number of salesmen:\n");
     count=GetInteger();

     info(&count);

     calcSales(&count);
}

void info(int *count){

for (int i=0; i<*count; i++){
    printf("\nInfo for salesman number %d:\n", i+1);

    printf("\nGive me his id: ");
        salesmen[i].id=GetInteger();

    printf("\nGive me his surname: ");
        gets(salesmen[i].surname); 

    printf("\nGive me the number of sales: ");
        salesmen[i].sales=GetLong();

    printf("\n 1=Thessaloniki, 2= Athens, 3= Volos, 4= Hrakleio \n");
    printf("\nLastly, give me the number of his area: ");
        salesmen[i].area=GetInteger();

    if (salesmen[i].area>4){
        printf("\nThe number you are trying to enter doesn't match to an area.\n");
        break;
        }
    }
}

void calcSales(int *count){


    long tSales[4];
    for (int i=0; i<*count; i++){
    tSales[i]=0;
    }

for (int i=0; i<*count; i++){

    if(salesmen[i].area==1){
            tSales[0]+=salesmen[i].sales;
        }
    if(salesmen[i].area==2){
            tSales[1]+=salesmen[i].sales;
        }
    if(salesmen[i].area==3){
            tSales[2]+=salesmen[i].sales;
        }
    if(salesmen[i].area==4){
            tSales[3]+=salesmen[i].sales;
    }
}

for (int i=0; i<4; i++){
    printf("\nSales for area number %d: %ld\n",i+1, tSales[i]);
    }
}

Your guess is correct.你的猜测是正确的。 The mistake in calcSales function. calcSales函数中的错误。

Exactly, the following part:确切地说,以下部分:

long tSales[4];
for (int i=0; i<*count; i++) {
        tSales[i]=0;
}

The *count has value of 20. Which means the loop goes from i = 0 until i = 19. When you access tSales[i] for i = 4 and above. *count值为 20。这意味着循环从 i = 0 到 i = 19。当您访问tSales[i] for i = 4 及以上时。 You're invoking an undefined behavior.您正在调用未定义的行为。 It's a memory that you didn't reserve for tSales.这是您没有为 tSales 保留的记忆。 I suggest using the following:我建议使用以下内容:

long tSales[4];
for (int i=0; i<4; i++) {
        tSales[i]=0;
}

Or better:或更好:

long tSales[4];
for (int i=0; i<sizeof(tSales)/sizeof(tSales[0]); i++) { // number of elements is the total size of array divided by the size of one element.
        tSales[i]=0;
}

Or even better, you don't need a loop at all:或者更好的是,您根本不需要循环:

long tSales[4] = {0};

Besides your problem:除了你的问题:

For the code you're providing, you don't need to pass a pointer to the functions.对于您提供的代码,您不需要传递指向函数的指针。

You can make the code look as follows:您可以使代码如下所示:

#include <stdio.h>
#include "genlib.h"
#include <simpio.h>
#include <string.h>
#define N 20
#define M 4

struct{
     int id;
     char surname[16];
     long sales;
     int area;
} salesmen[N];

void info(int count);
void calcSales(int count);

int main(){

     printf("Give me the number of salesmen:\n");
     int count=GetInteger(); // I've made declaration and assignment in same line. That seems cleaner to me.

     info(count);
     calcSales(count);
}

void info(int count){

    for (int i=0; i<count; i++){
        printf("\nInfo for salesman number %d:\n", i+1);

        printf("\nGive me his id: ");
        salesmen[i].id=GetInteger();

        printf("\nGive me his surname: ");
        gets(salesmen[i].surname); 

        printf("\nGive me the number of sales: ");
        salesmen[i].sales=GetLong();

        printf("\n 1=Thessaloniki, 2= Athens, 3= Volos, 4= Hrakleio \n");
        printf("\nLastly, give me the number of his area: ");
        salesmen[i].area=GetInteger();

        if (salesmen[i].area>4 || salesmen[i].area < 1){ // Added an extra condition.
            printf("\nThe number you are trying to enter doesn't match to an area.\n");
            break;
        }
    }
}

void calcSales(int count){

    long tSales[4] = {0};

    for (int i=0; i<count; i++){
            tSales[salesmen[i].area - 1]+=salesmen[i].sales; // No need for if conditions.
    }

    for (int i=0; i<4; i++){
        printf("\nSales for area number %d: %ld\n",i+1, tSales[i]);
    }
}

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

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