Doubt about the use of `for` in voting system

Asked

Viewed 183 times

0

I want to make a program where the user gives the number of candidates and their names, and then the vote is started until the user type order. Help?

#include<stdio.h>
#include<string.h>

int main()
{
    char nome[30][30];
    char vot[30];
    int ponto[30];
    int n,i,j,k,maior=0,aux;
    char fim[] = "fim";

    scanf("%i", &n);

    for(i=0;i<n;i++)
    {
        gets(nome[i]);
    }

    printf("A votacao comecou\n");


    for(i=0;i<=30;i++)
    {
        ponto[i]=0;
    }

    for(;;)
    {
        gets(vot);
        k=strcmp(vot,fim);
        if(k==0)
        {
            printf("A votacao terminou\n");
            break;
        }
        for(i=0;i<n;i++)
        {
            j=stricmp(vot,nome[i]);
            if(j==0)
            {
                ponto[i]=ponto[i]+1;
                break;
            }
        }
    }



    for (i=0; i<n; ++i)
    {
        if (ponto[i]>maior) aux=i;
    }

    printf("O vencedor e:\n");
    puts(nome[aux]);

    return 0;

}
  • After defining ponto as an array with 30 elements, the last element of the array is ponto[29]; the element ponto[30] there is no.

1 answer

2

In the last cycle

    for (i=0; i<n; ++i)
    {
        if (ponto[i]>maior) aux=i;
    }

the variable maior does not change value. Started 0 and continues 0 at the end of the cycle.

  • Thank you very much... it worked fine... When I saw what was wrong I didn’t believe kkkkkk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.