Thursday, 15 August 2013

c - Program crashes when trying to scan strings into array -



c - Program crashes when trying to scan strings into array -

i writing programme intake 5 peoples lastly names , votes. display names of people entered, corresponding votes, , winner.

i need names of people 1 array of strings. programme crashes. not sure if can modify create work or if need redo it.

the malloc function seems recurring prepare type of problem ?

#include <stdio.h> #include <string.h> #include <stdlib.h> int totalvotes(int votearray[],int size) { int sum=0; (int i=0;i<size;i++) { sum+=votearray[i]; } homecoming sum; } int winner(int votearray[],int size) { int max; max=0; if (votearray[1]>votearray[max]) max=1; if (votearray[2]>votearray[max]) max=2; if (votearray[3]>votearray[max]) max=3; if (votearray[4]>votearray[max]) max=4; homecoming max; } void main() { char namearray[5]; int votearray[5],total,winner; (int i=0;i<5;i++) { votearray[i]=0; } (int j=0;j<5;j++) { printf("enter name of candidate number %d\n",j+1); scanf("%s",namearray[j]); printf("enter persons number of votes\n"); scanf("%d",&votearray[j]); } total=totalvotes(votearray,5); winner=winner(votearray,5); printf("%s\t%s\t%s\n","candidate","votes received","% of total votes"); (int y=0;y<5;y++) { printf("%s\t%d\t%0.2f\n",namearray[y],votearray[y],(float)votearray[y]/total); } printf("the winner of election %s\n",namearray[winner]); }

char namearray[5]; should char namearray[5][20];

c

No comments:

Post a Comment