Reputation: 63
As far as I'm concerned I'm passing the variables spoilt and valid through to the function Proccosvotes correctly, but I'm not getting the right answer. Instead of incrementing valid by 1 like I thought I was doing, it's adding random numbers to it. For instance, I should be getting around 200 for valid, but I'm getting 2687468. I can seem to find the error. Does anyone see or know the mistake I'm making?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct election{
char name[20];
int votes;
};
struct election electionCandidates[7];
void Initialize(struct election electionCandidates[], int num, FILE *ifp);
void Processvote(struct election electionCandidates[], int num, FILE *ifp, int *valid1, int *spoilt1);
int main(){
FILE *ifp;
char *winner[20];
int valid = 0;
int spoilt = 0;
int num;
ifp = fopen("elections.txt", "r");
Initialize(electionCandidates, 6, ifp);
Processvote(electionCandidates, 6, ifp, &valid, &spoilt);
system("PAUSE");
printResults(electionCandidates, 6, &valid, &spoilt, *winner);
printf("GO");
system("PAUSE");
fclose(ifp);
system("PAUSE");
return 0;
}
void Initialize(struct election *electionCandidates, int num, FILE *ifp){
int i = 0;
for(i=0; i<7; i++){
fscanf(ifp, "%[^\n]%*c", &electionCandidates[i].name);
printf("%d: %s\n", i, electionCandidates[i].name);
}
}
void Processvote(struct election *electionCandidates, int num, FILE *ifp, int *valid1, int *spoilt1){
int i;
int x=0;
int j;
for(i=0; i<365; i++){
fscanf(ifp, "%d ", &x);
for(j=1; j<=7; j++){
if (j == x){
electionCandidates[j].votes+=1;
valid1++;
}
if (x < 1 || x > 7)
spoilt1++;
}
printf("valid: %d\nspoilt: %d\n", valid1, spoilt1);
}
}
Upvotes: 0
Views: 527
Reputation: 81996
In C, arrays start counting at 0 not 1.
Therefore, the following code will cause undefined behavior, because you call electionCandidates[7]
.
for(j=1; j<=7; j++){
if (j == x){
electionCandidates[j].votes+=1;
spoilt1
and valid1
are pointers to single integers. If you want to use the value that the pointer is pointing to, then you should do things like:
printf("%d", *spoilt1);
*spoilt1 += 1;
You're missing the implementation of printResults()
, so I'm not sure how you're running this at all.
Your compiler should be printing out a few warnings for this code. At the very least, you should fix them. Note that warnings are often indicative of a real problem in your code that you shouldn't ignore. Here's the warnings and errors that my compiler reports:
[11:53am][wlynch@watermelon /tmp] clang foo.c
foo.c:30:5: warning: implicit declaration of function 'printResults' is invalid in C99 [-Wimplicit-function-declaration]
printResults(electionCandidates, 6, &valid, &spoilt, *winner);
^
foo.c:45:34: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
fscanf(ifp, "%[^\n]%*c", &electionCandidates[i].name);
~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~
foo.c:66:43: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
printf("valid: %d\nspoilt: %d\n", valid1, spoilt1);
~~ ^~~~~~
foo.c:66:51: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
printf("valid: %d\nspoilt: %d\n", valid1, spoilt1);
~~ ^~~~~~~
4 warnings generated.
Undefined symbols for architecture x86_64:
"_printResults", referenced from:
_main in foo-1d49c9.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You should check that your calls to fscanf()
actually succeed. If the file is short or otherwise invalid, you might not update the value of your variables.
Upvotes: 2
Reputation: 2528
In Processvote, valid1 has the type pointer to an integer. In you code you write valid1++
, which is incrementing the value of the pointer, not what it is pointing to. Try (*valid1)++
and also (*spoilt1)++
Upvotes: 0