-2

What i am trying to build is a simple program that can count votes during an election.

The program should first ask how many the candidates are and then create a candNUMB list. After, program asks to specify each candidate name, which should be added to the respective list. ex:

candidate n1: Paul
candidate n2: Frank
candidate n3: John

now that we have 3 lists the program should repeatedly ask for your vote (until you say stop), and then add the votes to each list (Paul, Frank, John). Eventually the program prints the result.

Debug shows candidate[candNUMB] not declared in this scope. What am I missing?

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    int candNUMB = 0;
    int candidate[candNUMB] = {0};

    cout << "I can count votes during an election. Try me." << endl << endl;
    cout << "Let's start. Specify the number of candidates: ";
    cin >> candNUMB;
    cout << endl << "There are " << candNUMB << " candidates. Specify each name." << endl;

    for (int i = 0; i < candNUMB; i++)
    {
        cout << "Insert candidate n*" << i + 1 << " name: ";
        cin >> candidate[i];
    }


    return 0;
}

EDIT: added int candNUMB = 0;

now the program won't start at all.

Process Returned -107374ecc
fxJK
  • 19
  • 1
  • 8
  • 4
    where is `candNUMB` declared? – EdChum Feb 08 '18 at 15:40
  • You never set the variable `candNUMB` to anything. – Arnav Borborah Feb 08 '18 at 15:41
  • Downvote, typo error (forgot to declare a variable) – user202729 Feb 08 '18 at 15:41
  • @EdChum isn't int candidate[candNUMB] = {0}; a declaration? – fxJK Feb 08 '18 at 15:42
  • Also if this is C++ `stdio.h` should not be used and `vector` should be used. Dynamic size array is GCC extension IIRC. – user202729 Feb 08 '18 at 15:42
  • @fxJK that's a declaration for `candidate` – PeterT Feb 08 '18 at 15:42
  • no, `candidate` is declared as an array but `candNUMB` is not declared as anything, you need to declare your variables – EdChum Feb 08 '18 at 15:42
  • @fxJK -- `candNUMB` just pops out of nowhere in the code you posted. Thus it is not a [mcve] – PaulMcKenzie Feb 08 '18 at 15:44
  • i added int candNUMB = 0; but the program now won't start at all – fxJK Feb 08 '18 at 15:45
  • There is another problem, candidate is declared as an int array, but you want to enter *names*, so you need a string array. – john Feb 08 '18 at 15:46
  • 2
    How can you input a `string` in an `int` array? Also, what good is an array of zero size (`candNum=0`)? I am not sure and don't mind but I think you are not clear with the basics. – vishal-wadhwa Feb 08 '18 at 15:47
  • And `int candidate[candNUMB];` is not legal C++ unless candNUMB is a compile time constant. It's not legal if candNUMB is a variable like you have. – john Feb 08 '18 at 15:48
  • 1
    @fxJK -- If you added that code, then that array is not legal C++ syntax. Arrays in C++ must be declared using a constant expression, not a variable. Yet another new programmer being burned / fooled by `gcc` 's brain-dead approach to having VLA's to be accepted by default. – PaulMcKenzie Feb 08 '18 at 15:48
  • ok i'm being roasted. calm down boys i'm learning, obviously i don't have things clear, that's the point of writing a post here. – fxJK Feb 08 '18 at 15:52
  • @fxJK - It's a tough crowd here. :-) The next thing is that when you set `candNUMB` to zero, at best you get an array with zero elements. Of course that is not going to work during input, where `candidate[i]` will be out-of-range for *any* `i`. – Bo Persson Feb 08 '18 at 16:14
  • seems more a 'fix my code' or 'do my homework' type question than a SO question. – eric Mar 01 '18 at 19:51

1 Answers1

-2

Try this to get you started

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int candNUMB;
    string candidate[999];

Of course this won't work if you have more than 999 candidates, but I'm guessing you're going to have more significant problems than that to solve.

john
  • 85,011
  • 4
  • 57
  • 81
  • there's no way to CIN the value of 'candidate'? – fxJK Feb 08 '18 at 15:57
  • @fxJK you mean the size of candidate? Not with an array. The usual way is to use a vector. `int main() { int candNUMB; cin >> candNUMB; vector candidate(candNUMB);` That creates a vector with exactly candNUMB entries (whatever that number is). In C++ vectors are normally preferred to arrays. – john Feb 08 '18 at 16:07