#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
const int n = 20;
Defining the structs & list
typedef struct pole{
int year;
char county[n];
char ID_election[n];
char ID_electionUser[n];
int num_candidates;
char name_candidate[n];
int limit_votes;
}election;
typedef struct polelist{
election voting;
struct polelist *next_pole;
}node_pole;
Funtions
void createElection(node_pole *head_pole, election *info);
void insertElection(node_pole **head_pole, election *info);
void printElection(node_pole *head_pole);
Main
int main (){
node_pole *head_pole = NULL;
election *info = (election*)malloc(sizeof(election));
int i = 0;
I create the election and then I insert the node. I print the election just created and it works (I can read the name of the candidates).
createElection(head_pole, info);
insertElection(&head_pole, info);
printf("\nDo you want to visualize your election? Y/N: ");
printf("\n----- Last election created -----\n ");
printf("\nID Election:%s \nID Elect_user:%s \nYear:%d\nCounty:%s \nMax voters:%d", info->ID_election, info->ID_electionUser, info->year, info->county, info->limit_votes);
printf("\nCandidates' number:%d", info->num_candidates);
for(i = 0; i < info->num_candidates; i++){
printf("\nCandidates' name [%d]: %s", i+1, ((info + i)->name_candidate));
}
This is the result on TERMINAL
ID Election:D1 ID Elect_user:S1 Year:2023 County:USA Max voters:5 Candidates' number:3 Candidates' name [1]: A1 Candidates' name [2]: B2 Candidates' name [3]: C3
I print the election again but using the fuction printElection. Here I have the problem. I can't read the name of the candidates but only the first one.
printf("\n\t\t\t-ALL-\n\n");
printElection(head_pole);
return 0;
}
Election printed through function in TERMINAL:
ID Election:D1 ID Elect_User: S1 Year:2023 County:USA Max voters:5 Candidates' number:3 Candidates' name [1]: A1 Candidates' name [2]: Candidates' name [3]:
Functions definiton
void createElection(node_pole *head_pole, election *info){
election *pole = info;
int i = 0;
printf("\nInsert data: \n Year: ");
scanf("%d", &pole->year);
printf(" County:");
scanf("%s", pole->county);
printf(" Voters' limit:");
scanf("%d", &pole->limit_votes);
printf("ID_Election: ");
scanf("%s", pole->ID_election);
printf("ID_Election_User: ");
scanf("%s", pole->ID_electionUser);
printf("Candidates' number: ");
scanf("%d", &pole->num_candidates);
for(i = 0; i < pole->num_candidates; i++){
printf("\n Candidates' names [%d]:", i+1);
scanf("%s", (pole+i)->name_candidate);
}
return;
}
void insertElection(node_pole **head_pole, election *info){
node_pole *newnode = (node_pole*)malloc(sizeof(node_pole));
newnode->voting = *info;
newnode->next_pole = *head_pole;
*head_pole = newnode;
return;
}
void printElection(node_pole *head_pole){
int i;
node_pole *tmp = head_pole;
if(tmp == NULL){
printf("\nNo poll in the database.");
return;
}
while(tmp != NULL){
printf("\n----- Your election ----- ");
printf("\nID Election:%s \nID Elect_User: %s \nYear:%d \nCounty:%s \nMax voters:%d", tmp->voting.ID_election, tmp->voting.ID_electionUser, tmp->voting.year, tmp->voting.county, tmp->voting.limit_votes);
printf("\nCandidates' number:%d", tmp->voting.num_candidates);
for(i = 0; i < tmp->voting.num_candidates; i++){
printf("\nCandidates' name [%d]: %s", i + 1, (tmp+i)->voting.name_candidate);
}
tmp = tmp->next_pole;
}
return;
}
As pointed out in the comments, there's currently room for only one candidate name (20 - 1 characters long) in an election.
This is wrong:
printf("\nCandidates' name [%d]: %s", i+1, ((info + i)->name_candidate));
References like the above treat info
as if it was an array and are equivalent to info[i]->name_candidate
.
info
is not an array.
Small changes to move this forward (and, hopefully, increase the OP's understanding):
#define MAX_CANDIDATES 8 // an arbitrary number
typedef struct pole{
int year;
char county[n];
char ID_election[n];
char ID_electionUser[n];
int num_candidates;
char name_candidate[MAX_CANDIDATES][n]; // 2D array to hold up to 8 x 20 characters
int limit_votes;
} election;
and
printf("# of Candidates: ");
scanf("%d", &pole->num_candidates);
if( pole->num_candidates > MAX_CANDIDATES )
// handle erroneous entry
for(i = 0; i < pole->num_candidates; i++){
printf("\n Candidate's name [%d]:", i+1);
scanf("%19s", pole->name_candidate[i] );
}
and fixing up the print statement:
printf("Candidate name [%d]: %s\n", i+1, info->name_candidate[i]);
These changes should allow the OP to have some success and move the project forward. It would be worth revisiting this to neither use excessive space for fewer candidates nor impose a limit of 8 candidates. This is left as an exercise for the reader.
Finally: each invocation of scanf()
returns a value that is currently being ignored. Getting user input is fraught with challenges. Another exercise for the reader to increase the robustness of this code.
Minor: typedef struct pole {
and election *pole = info;
... Hmmm... Naming a variable with the same name as a datatype is bad practice and bound to lead to confusion. Get things clear in your own mind so that the reader of the code can have it clear in theirs.