Reputation: 29
I need to create a program that supposed to go like this:
"Each person sets aside a certain amount of money to give and evenly divides this money among all those s/he is gifting. No fractional currency exists. If the gifter decides to divide 5 among 2 friends, they would get 2 each with 1 left in the gifter's "account".
Given a group of n friends, the money each person in the group spends on gifts, and a list of friends to whom each person gives gifts, your goal is to determine how much more money each person gives than they receive."
It's supposed to read its input from a file called gift.txt.
This is my code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char name[11];
int received;
int given;
} ppl;
int main()
{
FILE *file = fopen("D:\\Leo\\C Programs\\gift-giving\\gift.txt", "r");
int n, i, j, k, amt, ng, gift, amtgvn, diff;
char temp[11];
char temp2[11];
ppl friends[10];
fscanf(file, "%d", &n);
for(i=0; i<n; i++)
{
fscanf(file, "%s", temp);
strcpy(friends[i].name, temp);
}
for(i=0; i<n; i++)
{
fscanf(file, "%s", temp);
fscanf(file, "%d %d", &amt, &ng);
gift = amt/ng;
for(j=0; j<ng; j++)
{
fscanf(file, "%s", temp2);
for(k=0; k<n; k++)
{
if(strcmp(friends[k].name, temp2)==0)
{
friends[k].received = gift;
if(friends[k].received != 0)
{
friends[k].received += gift;
}
}
}
}
for(j=0; j<n; j++)
{
amtgvn = gift*ng;
if(strcmp(friends[j].name, temp)==0)
{
friends[j].given = amtgvn;
}
}
}
for(i=0; i<n; i++)
{
diff = friends[i].received - friends[i].given;
printf("%s %d", friends[i].name, diff);
}
fclose(file);
return 0;
}
I tried it with a gift.txt file containing:
5
ryan
becky
owen
joel
mimi
ryan
200 3
becky
owen
joel
owen
500 1
ryan
mimi
150 2
joel
owen
becky
0 2
mimi
joel
joel
0 0
The output is supposed to look like:
ryan 302
becky 66
owen -359
joel 141
mimi -150
But there is an error whenever I try to run it. A message appears saying the program has stopped working (I use Code Blocks on Windows).
What seems to be the problem in the code?
Upvotes: 0
Views: 2558
Reputation: 206697
The error is caused by division by 0
. The last line in your input file reads:
0 0
After those numbers are read, amt = 0
and ng = 0
. And then you proceed to compute amt/ng
, which leads to an exception.
You need to add code to avoid that operation if ng = 0
.
You can add
if ( ng == 0 )
{
continue;
}
just before the line
gift = amt/ng;
You are also using uninitialized variables, which leads to undefined behavior.
Here's an updated main
.
int main()
{
char inputFile[] = "D:\\Leo\\C Programs\\gift-giving\\gift.txt";
FILE *file = fopen(inputFile, "r");
int n, i, j, k, amt, ng, gift, amtgvn, diff;
char temp[11];
char temp2[11];
ppl friends[10];
fscanf(file, "%d", &n);
for(i=0; i<n; i++)
{
fscanf(file, "%s", temp);
strcpy(friends[i].name, temp);
friends[i].received = 0;
friends[i].given = 0;
}
for(i=0; i<n; i++)
{
fscanf(file, "%s", temp);
fscanf(file, "%d %d", &amt, &ng);
printf("name: %s\n", temp);
printf("amount: %d, ng: %d\n", amt, ng);
if ( ng == 0 )
{
continue;
}
gift = amt/ng;
for(j=0; j<ng; j++)
{
fscanf(file, "%s", temp2);
for(k=0; k<n; k++)
{
if(strcmp(friends[k].name, temp2)==0)
{
friends[k].received += gift;
}
}
}
for(j=0; j<n; j++)
{
amtgvn = gift*ng;
if(strcmp(friends[j].name, temp)==0)
{
friends[j].given = amtgvn;
}
}
}
for(i=0; i<n; i++)
{
diff = friends[i].received - friends[i].given;
printf("name: %s, received: %d, given: %d, diff: %d\n",
friends[i].name,
friends[i].received,
friends[i].given,
diff);
}
fclose(file);
return 0;
}
Upvotes: 1