0

this is my first post on stack overflow and I would like to apologise for my english since I'm not a native english speaker. Anyway I have pretty much self-taught myself programming so obviously I am not very good at it. I got my hands into some programming exercises (got them from a friend in college) in order to get some experience but I'm having trouble with a certain one. The exercise asks to make an array of 10 names that the user will provide and one with their sales. The program should pass the first array's contents into two other arrays, one with the names that have managed more than 6000 sales and one with those that haven't managed to do so. The ones on the first array get a 500 bonus on their pays and the other ones get 200. The program should calculate the total company cost for their bonuses and print it on screen. The program should also print the two name arrays.

UPDATE:I decided to fix the problems without using dynamic memory allocation first. SO this works perfectly but it keeps printing the last name for some reason instead of every one.

#include <stdio.h>
#include <stdlib.h>
int c =0;
int x =0;
int i;
int sum_500 =0;
int sum_200 =0;
char* names_500[10];
char* names_200[10];
char* names[10];
int sales[10];
char buffer[200];
char trash[10];


int main() {

for (i =0; i<10; i++) {
    printf("Give name:");
    fgets(buffer,201,stdin);
    names[i] = buffer;
    printf("Give sales profit:");
    scanf("%d",&sales[i]);
    fgets(trash,11,stdin);
    if (sales[i] > 6000) {

        names_500[c] = names[i];
        sum_500 = sum_500 + 500;
        c++;
    }
    else {

        names_200[x] = names[i];
        sum_200 = sum_200 + 200;
        x++;
    }
}
if( c>0) {
        for(i =0; i<c; i++) {
        printf("%s\n",names_500[i]);
        }
}

if (x>0) {
        for(i =0; i<x; i++) {
        printf("%s\n",names_200[i]);
        }
}

printf("Company total bonus cost:%d",sum_200 + sum_500);
return 0;
}

UPDATE:I figured it out. It seems that I pass the pointer to the buffer variable to the name arrays so they all print the last string. Using strdup() solves the problem. Here is the final code including dynamic memory allocation for the name arrays.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int c =0;
int x =0;
int i;
int sum_500 =0;
int sum_200 =0;
char ** names_500; 
char ** names_200;
char * name; 
int sales[10]; 
char buffer[40]; 
char trash[10]; 

int main() {
 for (i =0; i<10; i++) {
  printf("Give name:");
  fgets(buffer,40,stdin); 
  name = strdup(buffer); 
  printf("Give sales profit:");
  scanf("%d",&sales[i]); 
  fgets(trash,11,stdin); 
 if (sales[i] > 6000) { 
  names_500= realloc(names_500,(c+1)*sizeof(char*)); 
  names_500[c] = name; 
  sum_500 = sum_500 + 500; 
  c++;
 }
 else { 
  names_200= realloc(names_200,(x+1)*sizeof(char*));
  names_200[x] = name;
  sum_200 = sum_200 + 200;
  x++;
 }
}
if( c>0) { 
 printf("500 bonus :\n");
 for(i =0; i<c; i++) {
  printf("%s",names_500[i]); 
 }
}

if (x>0) { 
 printf("200 bonus:\n");
 for(i =0; i<x; i++) {
  printf("%s",names_200[i]);
 }
}
printf("Company 200 bonus costs:%d",sum_200 ); 
printf("\nCompany 500 bonus costs:%d",sum_500); 
return 0;
}
1
  • Use a debugger (or valgrind) to figure out where it is crashing. Commented Nov 23, 2013 at 14:03

1 Answer 1

1

The program crashes after entering the first sales number.

The reason for your program crash is missing & in scanf's argument. It should be

 scanf("%d",&sales[i]);  

Side notes:
There is no use of allocating names[i] as allocated space is lost after names[i] = buffer;

names[i] = malloc(strlen(buffer));
names[i] = buffer;  

and same for names_500[c]

names_500[c] = malloc(strlen(names[i]));
names_500[c] = names[i];  
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot! This fixes the first crash. Now what happens is that it enters the loop 3 times while omitting the give name input and then crashes. Is it the stdin buffer?
Put {..... fflush(stdin);.....} just at loop beginning and see if the problem is solved.
@RafedNole; Using fflush(stdin) invokes undefined behavior unless you are on MS-DOS.
I used it in gcc (ubuntu) it didn't.I will check it out .
now the loop works but the program crashes sponateously and it seems that it doesn't pass the names correctly to the 2 arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.