Hello all , i have a small problem , i'm writing now a voting program , and i'm a little bit confused , how can i attribute the votes to a certain person? i mean , i enter the value 1 , and this vote must go to the first person from the array. Here is the code . I'm a newbie , i have started to learn this programming language 1 month ago )

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
	string users[3];
        cout<<"Insert 4 names:"<<endl;
	for(int i=0;i<=4;i++) {
		getline(cin, users[i]);
	return 0;

Edited by Dakot: n/a

5 Years
Discussion Span
Last Post by Narue

Well, arrays are indexed starting at 0, so if you want to map 1 to the first name, it would be users[index - 1] . Then all you need to do is ask the user to enter a number corresponding to the person they want to vote for.

Your loop has an off-by-one error, by the way. It should stop at 3 because that's the last valid index:

for(int i = 0; i < 4; i++) {

well , thanks for the reply , but can you show me the full code? i can't assemble these 2 parts) , i'm trying to do this too ) thanks.


but can you show me the full code?

Nope. The code you posted isn't even close to a voting program, so I can't justify giving you "full code" as it would constitute doing your homework for you.

i can't assemble these 2 parts

It sounds to me like you're overwhelmed and need to start smaller. Here's a good thread on the iterative development process. The only part that's missing is organizing your thoughts beforehand, and having a strong idea of how the program should work (in simple steps, like a flowchart) before writing any code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.