0

I have this habit of first trying out all the exercises of the book then going on to psets. I am on chapter 7, arrays. the question was to make a program of some sort of voting counter and instead of a defined number of entries the program should end only when 999 is entered.

following is my code, I wanna know why its giving me an error of undeclared identifier on the values stored at the array of string name. Seems I can't create an array of strings, but why?

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

int main (void)
{
    int counter[4], vote;

    for (int i = 0; i < 4; i++)
    {
        counter[i] = 0;
    }

    string name[] = {PTI, MQM, PPP, PMLN};

    while (true)
    {
        printf("press 1 to vote for %s.\npress 2 to vote for %s.\npress 3 to vote for %s.\nPress 4 to vote for %s.\n", name[0], name[1], name[2], name[3] );
        vote = GetInt();

        if (vote < 1 || vote > 4)
        {
            printf("Bad response.\n\n\n");
        }

        if (vote == 999)
        {
            break;
        }
        else
        {
            counter[vote - 1]++;
        }
    }

    for (int i =0; i < 4; i++)
    {
        printf("%i votes were casted for %s.\n", counter[i], name[i]);
    }
}
2
  • I haven't looked at the whole code, but are you sure PTI, MQM, PPP, PMLN are string values? don't you wanna surround them with something?
    – kzidane
    May 12 '16 at 11:07
  • lol you know how much I wasted time on this, cant think how i did that and why I couldnt figure it out. Thanks a lot May 12 '16 at 14:03

You must log in to answer this question.

Browse other questions tagged .