6 Replies - 1414 Views - Last Post: 05 September 2014 - 08:17 PM Rate Topic: -----

#1 elbmw787  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-September 14

how do I assign values from text file to a structure of arrays in C?

Posted 05 September 2014 - 05:38 PM

have to do an election program in C.

There are 7 candidates and 365 votes in total. I need to do this using an array of structures. I need to read from a text file each of the names of the candidate and the number of votes they get. At the end i need to output the winner of the election.

Here is a sample of my code so far
[
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    struct candidates {
        char name[20];
        int votes;
    };


    int main()

     {
        //Counter
        int i = 0;
        //Votes counter
        int v = 0;
        //Sploit Vote
        int spVote = 0;

struct candidates electionCandidate[7];
FILE *fp;
fp = fopen("elections.txt", "r");
for(i=0;i<7;i++)
    {
      char * aNames = fgets(electionCandidate[i].name, 20, fp);
    }

//for testing each candidate gots their name
for(i=0;i<7;i++)
    {
        printf("%d. Candidate is %s\n\n", i+1, electionCandidate[i]);
    }  
 //For 365 Votes
 for(v=0;i<365;v++)
 {

 while (!feof(fp))
        {
            if(electionCandidate[v].votes < 8)
            {
            int iVoteFor = 0;
            fscanf(fp, "%d", &iVoteFor);
            electionCandidate[iVoteFor-1].votes++;
            }

            else 
            {
            spVote++;

            }
        }
    }

        system("pause");

        return 0; 
    }
            //Ideas of what to use
]

Here is a sample of my text file:

Robert Bloom
John Brown
Michelle Dawn
Michael Hall
Sean O’Rielly
Arthur Smith
Carl White

1 2 4 5 1 2 3 4 4 1 2 3 7 4 4 5 3 7 7 7 7 7 7 7 7 7

Each candidate gets +1 vote for their number electionCandidate[0] for each one he gets one vote and so on for the rest. 365 voters in total.

I was able to input the name for each Candidate from the text file. Now the problem is putting each vote to the corresponding candidate. Also any vote that is above 7 is a spoilt vote which i am trying to count in the above code. The code compiles but it crashes.

I am using while(!feof) but it seems that its not working or this is not the correct way. Any suggestions.


This post has been edited by modi123_1: 05 September 2014 - 06:02 PM
Reason for edit:: please use the code tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: how do I assign values from text file to a structure of arrays in C?

#2 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: how do I assign values from text file to a structure of arrays in C?

Posted 05 September 2014 - 06:10 PM

I don't quite get what you need to do. But, that doesn't matter.
I would suggest you make functions with simple goals.
Like ReadFile() which could uses ParseLine() which handle each line, for example.
Or if the file is segmented a first part does ReadNames() and then ReadVotes()
Was This Post Helpful? 0
  • +
  • -

#3 elbmw787  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-September 14

Re: how do I assign values from text file to a structure of arrays in C?

Posted 05 September 2014 - 07:27 PM

View Postinfernorthor, on 05 September 2014 - 06:10 PM, said:

I don't quite get what you need to do. But, that doesn't matter.
I would suggest you make functions with simple goals.
Like ReadFile() which could uses ParseLine() which handle each line, for example.
Or if the file is segmented a first part does ReadNames() and then ReadVotes()


Each candidate from 1-7 each vote from 1-7 goes to the candidate with the corresponding number For example a vote "1" goes to electionCandidate[0].votes. Reading 365 votes from a text file called elections.txt

i am having trouble reading this number to each candidate and finally i need to output the winner of the election.
Was This Post Helpful? 0
  • +
  • -

#4 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: how do I assign values from text file to a structure of arrays in C?

Posted 05 September 2014 - 07:42 PM

Well I still recommend trying functions.
For example have void TallyVotes(FILE *file, struct candidate * electionCandidate);
void PrintWinner(struct candidate * electionCandidate);

But I do notice errors at line 37 and 42.
Was This Post Helpful? 0
  • +
  • -

#5 elbmw787  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-September 14

Re: how do I assign values from text file to a structure of arrays in C?

Posted 05 September 2014 - 07:52 PM

View Postinfernorthor, on 05 September 2014 - 07:42 PM, said:

Well I still recommend trying functions.
For example have void TallyVotes(FILE *file, struct candidate * electionCandidate);
void PrintWinner(struct candidate * electionCandidate);

But I do notice errors at line 37 and 42.

I removed that for loop in line 37 sorry I dont know how to edit my post.

Yes i agree with functions but i feel like if i try functions without having it work in main that i could never get it to work.
The if statement i its right since if <8 execute to give a votes more than 8 its a spoilt vote or is my logic wrong?
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: how do I assign values from text file to a structure of arrays in C?

Posted 05 September 2014 - 08:09 PM

Well you should post changes. Also I was point the problem of using i and v. Not the condition.

I don't see how functions would impede getting main to work. With functions you are turning one assignment into many short ones. Which makes it easier to test. But combine them. So, I see no difference.
Plus you can write main before you make the functions. I prefer making functions first but still.
And in main you can order the tasks. And make easier to understand.

...
GetCandidateNames(...);
TallyVotes(...);
PrintWinner(...);



This post has been edited by infernorthor: 05 September 2014 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon

Reputation: 5336
  • View blog
  • Posts: 16,617
  • Joined: 25-December 09

Re: how do I assign values from text file to a structure of arrays in C?

Posted 05 September 2014 - 08:17 PM

Look at the following snippet:
      printf("%d. Candidate is %s\n\n", i+1, electionCandidate[i]);

What type of variable is electionCandidate[i]? Is it a string? The specifier must match the variable, otherwise you have undefined behavior, meaning anything can happen.

Next look at the following snippet:
   fp = fopen("elections.txt", "r");
   for(i=0; i<7; i++)
   {
      char * aNames = fgets(electionCandidate[i].name, 20, fp);
   }



What happens if the file fails to open properly? You really need to check to insure the file opens correctly. Next what happens if there are less than 7 entries in your file?

Quote

I am using while(!feof) but it seems that its not working or this is not the correct way. Any suggestions.

While while(!feof) can, if used correctly, be used to control a data entry loop it is not normally the recommended way. Normally you use the read operation to control the loop because feof() is not set until after you try to read past the end of the file. Your use of this construct will usually produce one more read than what is actually in the file.

The more common way would be similar to:
while( fscanf(fp, "%d", &iVoteFor) == 1))


Quote

The code compiles but it crashes.

I suggest you run the program with your debugger. The debugger should be able to tell you exactly where the problem is detected and you should be able to view the variables at the time of the crash.

Without knowing the exact layout of your input file it is hard to answer several of the other questions, but you should probably verify that you are reading the file correctly by printing the variables after your reads.

Jim

This post has been edited by jimblumberg: 05 September 2014 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1