Hi there, I'm trying to create a section of code for an election vote counter.
So far I have created a class called candidate and an array of that class called candidates. With the below code, how do I store the users input in the correct location of the array.
Any help would be much appreciated.
#include <iostream> // Prompts the preprocessor to include the input & output standard file.
#include <string.h> // Includes the C++ string class.
#include <stdio.h> // Includes standard I/O options, Cin / Cout . . . etc
using namespace std;
class candidate // Declares a class called candidate
{
public: // Sets the below data members as public, therefore they can be accessed outside the class.
string FirstName; // Creates data member of string type called FirstName.
string SecondName; // Creates variable of string type called SecondName.
string AddressLine1; // Creates variable of string type called AddressLine1.
string AddressLine2; // Creates variable of string type called AddressLine2.
string Postcode; // Creates variable of string type called Postcode.
string Party; // Creates variable of string type called Party.
};
int main(void)
{
candidate candidates[5]; // Creates an array called candidates of type candidate & length 20.
int loop; // Creates integer variable called loop
for (loop = 1; loop<6; loop++) // Loops 20 times
{
cout << "Enter candidate number " << loop << "details: \n";
cout << " First name: \n";
cin >> candidates[loop].FirstName; // Stores the name entered in FirstNamestring in the array at index loop
cout << " Second name: \n";
cin >> candidates[loop].SecondName;
cout << " Address line 1: \n";
cin >> candidates[loop].AddressLine2;
cout << " Address line 2: \n";
cin >> candidates[loop].AddressLine2;
cout << " Postcode: \n";
cin >> candidates[loop].Postcode;
cout << " Party : \n";
cin >> candidates[loop].Party;
}
return(0);
}
arrays start at 0. Your loop is wrong, its
for(loop = 1; loop<6; loop++)
the concept is correct, it stores the users input, the first one in [0], the second in [1] etc. That is the only mistake I see.
This isn't a mistake but its best not to mix C and C++ headers due to namespacing.
its <cstring> and <cstdio>
but, you are using STL strings.
that is just <string> and I am unsure how this compiled without it. /shrug some compilers seem to gloss over the includes for standard items at times, or maybe its in the project elsewhere. <cstring> is for manipulating char[10] type "strings" as are used in C. <string> is for string x ; like you have. Likewise, stdio is for C output, printf statements. If you are using cout, you don't need this. iostream is what pulls in cin/cout.
Thanks for this. I'm still having problems storing the input data in the array.
Is the above code the correct way or should I include the string name (E.g FirstName) inside the square brackets to index the array [loop.FirstName]?
Is the above code the correct way or should I include the string name (E.g FirstName) inside the square brackets to index the array [loop.FirstName]?
What you have is correct. loop is your loop counter (an int). It does not have members such as FirstName.
To expand on jonnin's comment about your comment being broken, it's not a good idea to hard code values in your program. You should use named constants.
This assures that you're always using the correct value and if you want to change the maximum number of candidates, you only have to change a single line.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Honestly I thought what you had would work if you fixed the loop bounds problem. Assuming you did that, and assuming its not working, can you explain what isn't working to us? I just look at this stuff without dumping it into a compiler. I don't catch everything, but if I have symptoms of a problem, I have a good shot.
A stripped down version of what you need to do. The index of the loop identifies the candidate uniquley. If there is some other member number you would input that as part of the particular candidate as a member-no attribute which you can search on after the array is established. There are more complicated and more efficients ways of doing it but an array is probably simplest.
#include <iostream> // Prompts the preprocessor to include the input & output standard file.
#include <string.h> // Includes the C++ string class.
#include <stdio.h> // Includes standard I/O options, Cin / Cout . . . etc
usingnamespace std;
class Candidate
{
public:
Candidate(){};
Candidate(string aFirstName, string aParty)
{
FirstName = aFirstName;
Party = aParty;
}
string getFirstName(){ return FirstName; }
string getPartyName(){ return Party; }
private:
string FirstName;
string Party;
};
constint NO_CANDIDATES = 3;
int main()
{
Candidate ballot_paper[NO_CANDIDATES];
string first_name;
string party_name;
for (int loop = 0; loop < NO_CANDIDATES; loop++)
{
cout << "Enter first name: \n";
cin >> first_name;
cout << "Party name: \n";
cin >> party_name;
ballot_paper[loop] = Candidate(first_name, party_name);
}
for(int i = 0; i < NO_CANDIDATES; ++i)
{
cout << ballot_paper[i].getFirstName() << '\t' << ballot_paper[i].getPartyName() << '\n';
}
return(0);
}
Enter first name:
Bob
Party name:
white
Enter first name:
Bill
Party name:
Blue
Enter first name:
Mary
Party name:
Red
Bob white
Bill Blue
Mary Red
Program ended with exit code: 0