简体   繁体   中英

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. 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:

Number of salesmen: 7

(

  1. Enter the his id:

  2. Enter his surname:

  3. Enter the number of sales:

  4. Enter the area code:

) x7 times.

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.

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.

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. You're invoking an undefined behavior. It's a memory that you didn't reserve for 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]);
    }
}

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