Changing Struct Array from Function (Reading from file) - c

I have the following struct
struct candidates
{
char name[20];
int votes;
};
struct candidates electionCandidates[];
I need to read in from a file and update the names electionCandidates[0] through 7.
I can do so with the following
for (i = 0; i < 7; i++)
{
fgets(electionCandidates[i].name, 20, (FILE*)fp);
}
But I need to do this in a function.
I have tried
void Initialize(struct candidates* EC[]);
Initialize(&electionCandidates);
void Initialize(struct candidates* EC[])
{
int i;
FILE *fp;
fp = fopen("elections.txt", "r");
for (i = 0; i < 7; i++)
{
fgets(EC[i].name, 20, (FILE*)fp);
}
fclose(fp);
}
Keeps saying it doesn't see name. Or the whole thing could be wrong. I am not sure.
Any help would be appreciated.

You need to look at your Initialize() function signature, and turn on your compiler warnings (or pay attention to them if they're already on). You are declaring Initialize() to take an array of pointers to struct candidates, which is not what you are passing - an array of struct candidates. The array decays to a pointer, so struct candidates *EC is what your argument should look like (or, alternately, struct candidates EC[], which is equivalent in this case), not struct candidates *EC[]. And then call your function without the &...

Given
struct candidates
{
char name[20];
int votes;
};
struct candidates electionCandidates[];
You want
void Initialize(struct candidates EC[])
{
/* ... */
}
Initialize(electionCandidates);
When passed as a function argument, the array of structs decays to a pointer to its first element.

I got it loading the names and everything but I guess I need to point to SPOIL in this? It is not reading or doing the votes/spoiled votes, giving 0 for both.
#include <stdio.h>
struct candidates
{
char name[20];
int votes;
};
struct candidates electionCandidates[7];
void Initialize(struct candidates EC[]);
void Processvotes(struct candidates EC[], int BadVote);
void printResults(struct candidates EC[], int BadVote);
int main()
{
int i, SPOIL = 0;
Initialize(electionCandidates);
Processvotes(electionCandidates, SPOIL);
printResults(electionCandidates, SPOIL);
}
void Initialize(struct candidates EC[])
{
int i;
FILE *fp;
fp = fopen("elections.txt", "r");
for (i = 0; i < 7; i++)
{
fgets(EC[i].name, 20, (FILE*)fp);
}
fclose(fp);
}
void Processvotes(struct candidates EC[], int BadVote)
{
int TVOTE, i;
FILE *fp;
fp = fopen("elections.txt", "r");
fscanf(fp, "%d", &TVOTE);
for (i = 0; i < 365; i++)
{
if (TVOTE == 1)
EC[0].votes++;
if (TVOTE == 2)
EC[1].votes++;
if (TVOTE == 3)
EC[2].votes++;
if (TVOTE == 4)
EC[3].votes++;
if (TVOTE == 5)
EC[4].votes++;
if (TVOTE == 6)
EC[5].votes++;
if (TVOTE == 7)
EC[6].votes++;
if (TVOTE < 1 || TVOTE > 7)
BadVote++;
}
fclose(fp);
}
void printResults(struct candidates EC[], int BadVote)
{
int i, Win = 0, WinSCORE = 0, Runner = 0, RunnerSCORE = 0;
for (i = 0; i < 7; i++)
{
if (EC[i].votes > WinSCORE)
{
WinSCORE = EC[i].votes;
Win = i;
}
if (EC[i].votes == WinSCORE)
{
RunnerSCORE = EC[i].votes;
Runner = i;
}
}
if (WinSCORE == RunnerSCORE)
{
printf("There was a tie between %s and %s who both got a total of %d votes each. There were %d spoiled votes\n", EC[Win].name, EC[Runner].name, WinSCORE, BadVote);
}
else
printf("%s won the election with a total of %d votes. There was a total of %d spoiled votes.\n", EC[Win].name, WinSCORE, BadVote);
}

Related

C Problem with counting elements in the list of names

I have made one program, where you enter a few characters (10 max). It makes you a list, count average length of surnames, tell about how much different names. But the problem is, when I enter the last number (10) - it sorts me it incorrectly (like 39399349349, 3443993). Beneath I will present my code. I am newbie in C, so please don't shut on me) I am convinced that sorting function is incorrect, but don't know what exactly(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct people {
int num[10];
char surname[20];
char name[10];
} peoples[10], c;
int compare_people_num(const void *a, const void *b);
int main()
{
int i, j, k = 0, l = 0, m = 0, n = 0;
float s = 0;
char str[100];
system("chcp 1251 > nul");
for (i = 0, j = 0; i < 10; i++, j++)
{
printf("Enter number, surname, name %d of your human: ", i + 1);
fgets(str, sizeof str, stdin);
sscanf(str, "%d %s %s", &peoples[j].num, &peoples[j].name, &peoples[j].name);
while (str[n] != '\n')
{
if (str[n] != ' ')
{
peoples[j].num[k] = str[n];
}
else
break;
n++;
k++;
}
n++;
k = 0;
while (str[n] != '\n')
{
if (str[n] != ' ')
{
peoples[j].surname[k] = str[n];
}
else
break;
n++;
k++;
}
n++;
k = 0;
while (str[n] != '\n')
{
if (str[n] != '\0')
{
peoples[j].name[k] = str[n];
}
else
break;
n++;
k++;
}
n = 0;
k = 0;
}
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10; j++)
{
if (!strcmp(peoples[i].name, peoples[j].name))
m = 1;
}
if (m == 0)
l++;
m = 0;
s = s + strlen(peoples[i].surname);
}
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
if (strcmp(peoples[j].num, peoples[j+1].num) > 0)
{
qsort(peoples, 10, sizeof(struct people), &compare_people_num);
}
for (i = 0; i < 10; i++)
{
printf("%d ", peoples[i].num);
printf("%s ", peoples[i].name);
printf("%s ", peoples[i].surname);
printf("\n");
}
printf("\nYou have %d different names\n", l);
printf("Avarege lenght of surname is = %f\n", s / 10);
}
int compare_people_num(const void *a, const void *b)
{
const struct people *p1 = a;
const struct people *p2 = b;
return p1->num - p2->num; // Change order to reverse sort
}
I went through your code and removed things that weren't needed. In both your input and sorting, it seemed like you were doing things twice. I tried to document the changes I made and explain why they should be made.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// It's generally a good idea to use #define when you have some global constants
// I made some of the constants larger than what you showed to prevent issues
#define MAX_NAME_LEN 40
#define MAX_SURNAME_LEN 40
#define NUM_PEOPLE 10
#define BUFF_LEN 100
// Separate your struct...
struct person {
int num;
char name[MAX_NAME_LEN];
char surname [MAX_SURNAME_LEN];
};
// ... and array decleration
static struct person people[NUM_PEOPLE];
// I added this function, to make it easier to display a person
void print_person (const struct person * p) {
printf("Person %d %s %s\n", p->num, p->name, p->surname);
}
// This function will print out every person in the people array
void print_people (void) {
for (int i=0; i<NUM_PEOPLE; i++) {
print_person(&people[i]);
}
}
// Compares two people by number
int compare_people_num (const void * a, const void * b) {
struct person * p0 = (struct person *) a;
struct person * p1 = (struct person *) b;
return p0->num - p1->num;
}
// Compares two people by name
int compare_people_name (const void * a, const void * b) {
struct person * p0 = (struct person *) a;
struct person * p1 = (struct person *) b;
return strcmp(p0->name, p1->name);
}
int main (void) {
int i;
char buffer[BUFF_LEN];
for (i=0; i<NUM_PEOPLE; i++) {
printf("Enter number, surname, and name of person %d: ", i+1);
fflush(stdout); // fflush makes sure that our text is shown to the user
fgets(buffer, BUFF_LEN, stdin); // Read user input in to buffer
// It's unclear what you were doing here
// This sscanf line takes a line of text, and splits it into a number and two words
// It then stores that number in people[i].num, and stores the words in name and surname
// However, right after this, you have several while loops that appear to be manually doing the same
// thing all over again. If you want to read all of the input in, just the line below is enough
sscanf(buffer, "%d %s %s", &people[i].num, people[i].name, people[i].surname);
}
// We've read all of the people in now
// Uncomment the next line to check out the output at this state:
// print_people();
// To count names, we first need to sort the people by their name
// We do this using a qsort call
qsort(people, NUM_PEOPLE, sizeof(struct person), compare_people_name);
// Once the names are sorted, we'll calculate how many different names there are
// We start the count at 1, and start checking from the second person (index 1)
// This is because the first person will always be unqiue, and we can't compare to
// person negative 1
int n_names = 1;
for (i=1; i<NUM_PEOPLE; i++) {
char * current = people[i].name;
char * previous = people[i-1].name;
if (!strcmp(current, previous)) {
n_names ++;
}
}
// Now we have to sort the people based on their num field
// Again, in your code, it looked like you were doing this more than nessecary
// We just have to call qsort once, as such
qsort(people, NUM_PEOPLE, sizeof(struct person), compare_people_num);
// We will also do a loop through to calculate the average surname length
float avg_surname_len = 0;
for (i=0; i<NUM_PEOPLE; i++) {
avg_surname_len += (float)strlen(people[i].surname);
}
avg_surname_len /= (float)NUM_PEOPLE;
// We're all done! The people are sorted by number.
print_people();
printf("There are %d unique names\n", n_names);
printf("The average surnames is %f characters\n", avg_surname_len);
}

Selection sort not working as expected (C language)

I have to sort an array of structs with selection sort, after I read them from a file.txt.
My algorithm is not working as expected, but it always avoid to sort them and it print the struct in decreasing order.
Example of file.txt :
P0 "ANTONIO" 2000 4
P1 "BARTOLOMEO" 1995 6
P2 "CARLO" 2020 1
P3 "DEMETRIO" 1960 2
P4 "ETTORE" 1920 3
P5 "FRANCESCO" 1950 5
Input: 2 5 3 1 6 4
Output: 4 6 1 3 5 2
What am I doing wrong?
Code below:
#include <stdio.h>
#include <stdlib.h>
#define N 7
struct persona
{
char codice[10];
char nome[30];
int anno[10];
int reddito[10];
};
int main()
{
FILE* fp;
fp = fopen("Testo.txt", "r");
struct persona* persona = malloc(sizeof(struct persona) * N);
int i = 0;
int j = 0;
if (fp != NULL)
{
while (i < N-1)
{
fscanf(fp, "%s %s %s %s",
persona[i].codice,
persona[i].nome,
persona[i].anno,
persona[i].reddito);
i++;
}
}
else
{
perror("Errore");
}
fclose(fp);
for(i=0; i<N-2; i++)
{
int min = persona[i].reddito;
for(j=i+1; j<N-1; j++)
{
if(persona[j].reddito < persona[i].reddito)
{
min = persona[j].reddito;
}
persona[N] = persona[j];
persona[j] = persona[i];
persona[i] = persona[N];
}
}
for(i=0; i<N-1;i++)
{
printf("%s\t %s\t %s\t %s\n",
persona[i].codice,
persona[i].nome,
persona[i].anno,
persona[i].reddito);
}
}
You have 6 lines in the file, but you have defined N as 7. It should be updated to 6.
while (i < N-1)
You are reading the first N-2, that is, 5 lines from the file. The 6th line is not being read. Same goes for other loops in the code using N.
int anno[10];
int reddito[10];
You do not need integer array to read the numeric fields in file, an integer should suffice.
As mentioned by #WhozCraig in the comments, the selection sort algorithm is incorrect. Here's the updated code for reference:
#include <stdio.h>
#include <stdlib.h>
#define N 6
typedef struct persona
{
char codice[10];
char nome[30];
int anno;
int reddito;
} PERSONA;
int main()
{
FILE *fp;
if ((fp = fopen("file.txt", "r")) == NULL)
{
perror("\nFile not found");
exit(1);
}
PERSONA *persona = malloc(sizeof(struct persona) * N);
int i = 0, j;
while (i < N)
{ if (fscanf(fp, "%s %s %d %d", persona[i].codice, persona[i].nome, &persona[i].anno, &persona[i].reddito) == EOF) {
break;
}
i++;
}
fclose(fp);
PERSONA temp;
/* selection sort */
for (i = 0; i < N - 1; i++)
{
int jMin = persona[i].reddito;
for (j = i + 1; j < N; j++)
{
if (persona[j].reddito < persona[jMin].reddito)
{
jMin = j;
}
}
if (jMin != i)
{
temp = persona[i];
persona[i] = persona[jMin];
persona[jMin] = temp;
}
}
for (i = 0; i < N; i++)
{
printf("\n%s\t %s\t %d\t %d", persona[i].codice, persona[i].nome, persona[i].anno, persona[i].reddito);
}
}
Further improvements:
You could also calculate the number of lines in the file in the code instead of relying on a hardcoded value N.
The statement to check sets only the min only if this statement is true
if(persona[j].reddito < persona[i].reddito)
try using the qsort function
int cmpfunc (const void * a, const void * b) {
struct persona *p1 = (struct persona *)a;
struct persona *p2 = (struct persona *)b;
if(p1->reddito < p2->reddito) return -1;
if(p1->reddito > p2->reddito) return 1;
return 0;
}
and instead of the for loop, use
qsort(persona, N, sizeof(struct persona), cmpfunc);

C strcmp segmentation fault

#include <string.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct bank
{
char an;
char name;
char type;
int bal;
};
int main()
{
int i=0,n;
printf("Enter the number of accounts\n");
scanf("%d",&n);
struct bank a[n];
printf("Enter the details of the users\n");
for(i=0;i<n;i++)
{
scanf("%s%s%s%d",a[i].an,a[i].name,a[i].type,&a[i].bal);
}
printf("The details of the users are\n");
for(i=0;i<n;i++)
{printf("%s\n%s\n%s\n%d\n\n",a[i].an,a[i].name,a[i].type,a[i].bal);}
char atype[10];
printf("Enter the type of account you want to search\n");
scanf("%s",atype);
char typ[10];
char s[]="savings";
char c[]="current";
int result,res1,res2;
result = strcmp(atype,s);
if(result == 0)
{
for(i=0;i<n;i++)
{
typ[10] = a[i].type;
res1 = strcmp(typ,s);
if(res1 == 0)
{
printf("%s\n%s\n%s\n%d\n\n",
a[i].an,a[i].name,a[i].type,a[i].bal);
}
printf("\n");
}
} else
{
for(i=0;i<n;i++)
{
typ[10] = a[i].type;
res2 = strcmp(typ,c);
if(res2 == 0)
{
printf("%s\n%s\n%s\n%d\n\n",
a[i].an,a[i].name,a[i].type,a[i].bal);
}
printf("\n");
}
}
}
so basically ik its my homework but i did everythimg and i still cannot resolve the segmentation fault.please help
i think its something to do with strcmp() function but oh well
i checked all the sources but couldnt really find any fix.
any help would be appreciated.
For starters:
This
typ[10] = ...
accesses typ one past its valid memory. This invokes undefined behaviour, so anything can happen from then on.
In C array indexing is 0-based. So for char[10] the highest allowed index would be 9. Access the 1st element would be done by using 0.
You have made 2 mistakes here .
First your struct bank declaration was wrong. You forgot to declare name an and type as string. You declared it as just character(char).It should be like :-
struct bank
{
char an[100]; // assuming 100 is max size of input strings
char name[100];
char type[100];
int bal;
};
second you cannot do typ[10] = a[i].type; you should use strcpy() Something like this :-
strcpy(typ,a[i].type);
So this corrected code will work :-
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct bank // change made 1
{
char an[100];
char name[100];
char type[100];
int bal;
};
int main()
{
int i = 0, n;
printf("Enter the number of accounts\n");
scanf("%d", &n);
struct bank a[n];
printf("Enter the details of the users\n");
for (i = 0; i < n; i++)
{
scanf("%s%s%s%d", a[i].an, a[i].name, a[i].type, &a[i].bal);
}
printf("The details of the users are\n");
for (i = 0; i < n; i++)
{
printf("%s\n%s\n%s\n%d\n\n", a[i].an, a[i].name, a[i].type, a[i].bal);
}
char atype[10];
printf("Enter the type of account you want to search\n");
scanf("%s", atype);
char typ[10];
char s[] = "savings";
char c[] = "current";
int result, res1, res2;
result = strcmp(atype, s);
if (result == 0)
{
for (i = 0; i < n; i++)
{
strcpy(typ,a[i].type); // change made 2
res1 = strcmp(typ, s);
if (res1 == 0)
{
printf("%s\n%s\n%s\n%d\n\n",
a[i].an, a[i].name, a[i].type, a[i].bal);
}
printf("\n");
}
}
else
{
for (i = 0; i < n; i++)
{
strcpy(typ,a[i].type); // change made 3
res2 = strcmp(typ, c);
if (res2 == 0)
{
printf("%s\n%s\n%s\n%d\n\n",
a[i].an, a[i].name, a[i].type, a[i].bal);
}
printf("\n");
}
}
}
So your mistake was not with strcmp()

Function not updating an int field

Everything seems to working fine except for my SPOILED votes int is staying at 0 and not being updates by the function. I have tried adding a pointer thinking it would point to the object outside of the function which it does but the ++ function is not updating
#include <stdio.h>
struct candidates
{
char name[20];
int votes;
};
struct candidates electionCandidates[7];
void Initialize(struct candidates EC[]);
void Processvotes(struct candidates EC[], int *BadVote);
void printResults(struct candidates EC[], int *BadVote);
int main()
{
int i, SPOIL = 0;
Initialize(electionCandidates);
Processvotes(electionCandidates, &SPOIL);
printResults(electionCandidates, &SPOIL);
for(i = 0; i < 7; i++)
{
printf("%s",electionCandidates[i].name);
printf("%d\n\n",electionCandidates[i].votes);
}
printf("%d", SPOIL);
}
void Initialize(struct candidates EC[])
{
int i;
FILE *fp;
fp = fopen("elections.txt", "r");
for (i = 0; i < 7; i++)
{
fgets(EC[i].name, 20, (FILE*)fp);
}
fclose(fp);
}
void Processvotes(struct candidates EC[], int *BadVote)
{
int TVOTE, i;
FILE *fp;
fp = fopen("elections.txt", "r");
for (i = 0; i < 7; i++)
{
fgets(EC[i].name, 20, (FILE*)fp);
}
for (i = 0; i < 365; i++)
{
fscanf(fp, "%d", &TVOTE);
if (TVOTE == 1)
EC[0].votes++;
if (TVOTE == 2)
EC[1].votes++;
if (TVOTE == 3)
EC[2].votes++;
if (TVOTE == 4)
EC[3].votes++;
if (TVOTE == 5)
EC[4].votes++;
if (TVOTE == 6)
EC[5].votes++;
if (TVOTE == 7)
EC[6].votes++;
if (TVOTE < 1 || TVOTE > 7)
*BadVote++;
}
fclose(fp);
}
void printResults(struct candidates EC[], int *BadVote)
{
int i, Win = 0, WinSCORE = 0, Runner = 0, RunnerSCORE = 0;
for (i = 0; i < 7; i++)
{
if (EC[i].votes > WinSCORE)
{
WinSCORE = EC[i].votes;
Win = i;
}
if (EC[i].votes == WinSCORE)
{
RunnerSCORE = EC[i].votes;
Runner = i;
}
}
if (WinSCORE == RunnerSCORE)
{
printf("There was a tie between %s and %s who both got a total of %d votes each. There were %d spoiled votes\n", EC[Win].name, EC[Runner].name, WinSCORE, *BadVote);
}
else
printf("%s won the election with a total of %d votes. There was a total of %d spoiled votes.\n", EC[Win].name, WinSCORE, *BadVote);
}
Any help will be appreciated.
The postfix ++ operator takes precedence over the *; when you write *BadVote++, you're actually incrementing the pointer, not the value being pointed to. You should instead write (*BadVote)++. See this question for more.
I'm pretty sure it's a problem with the precedence of the operators. Try this:
if (TVOTE < 1 || TVOTE > 7)
(*BadVote)++;

Structures and input from files

My text file is as following:
Random Words //this only appears once at the top
Occupation1
1 2 3 4 5
6 7 8 9 10
Occupation2
11 12 13 14 15
16 17 18 19 20
I am having some trouble with the input and was wondering if you could take a look at my code.
typedef struct foo
{
char occupation[256];
int numbers[limita][limitb];
}FOO;
void function(FOO input[]);
int main()
{
FOO input[limit];
function(input);
return 0;
}
void function(FOO input[])
{
FILE* file;
file = fopen("textfile.txt","r");
int a=0; int b =0;
char temp[81];
char randomwords[81];
while(fgets(temp,sizeof(temp)-1,file))
{
sscanf(temp, "%[^\n] %[^\n] %d", randomwords,&input[a].occupation[a], &input[a].numbers[a][b]);
a++;
}
}
So I tried printing out (with printf) the random words and the occupation but to no avail. I don't think i'm using numbers correctly at all as it has to be a 2D array and don't seem to be changing the columns.
I would really appreciate a step in the right direction/some help. Please explain simply. Thank you very much.
EDIT:
technomage brought up an interesting point regarding what i'm doing vs what I want. I'm not sure how to change in reflection to what he suggested though.
You probably want to do something like this. This implementation assumes that there are no blank lines in the input file. There are lot of hard coding here, you may want to make it better. I hope this helps you...
typedef struct foo
{
char occupation[256];
int numbers[2][5];
}FOO;
void function(FOO input[]);
void function(FOO input[])
{
FILE* file;
file = fopen("textfile.txt","r");
int a = 0, count, i;
char temp[81];
char randomwords[81];
if (file == NULL)
{
return;
}
fscanf(file, "%[^\n]\n", randomwords);
printf("%s\n", randomwords);
while (feof(file) != EOF)
{
i = 0;
i = fscanf(file, "%[^\n]\n", input[a].occupation);
if (i == -1)
break;
for (count=0; count < 5; count++)
{
i = 0;
i = fscanf(file, "%d", &(input[a].numbers[0][count]));
}
for (count=0; count < 5; count++)
{
i = 0;
i = fscanf(file, "%d", &(input[a].numbers[1][count]));
}
a++;
i = 0;
i = fscanf(file, "\n");
if (i == -1)
break;
};
fclose(file);
}
int main(int argc, char *argv[], char *envp[])
{
int i, j, count;
FOO input[10];
function(input);
for (count = 0; count < 2; count++)
{
printf("%s\n", input[count].occupation);
for (i = 0; i < 2 ; i++)
{
for (j = 0 ; j < 5 ; j ++)
{
printf ("%d ", input[count].numbers[i][j]);
}
printf ("\n");
}
printf ("\n");
}
}

Resources