-
Vectors and strings
I need help as soon as possible. I am writing a program that lets you enter 5 names, the number of votes for each person, and it outputs the names, votes, and percentage of votes each person has to the total number of votes. I use 3 vectors, one for names, one votes received, and one for the percentages. My only problem is with the names. I try to use vector<string>names(5,0); and it will not compile. I am using c++ builder 5. My code is as follows:
#include <iostream>
#include <iomanip.h>
#include "vector.h"
#include <string.h>
#pragma hdrstop
//---------------------------------------------------------------------------
int main()
{
vector <string> names(5,0);
vector <float> votes(5,0);
vector <float> percentage(5,0);
int totvotes;
int loopcount;
for (loopcount=0; loopcount <=4; loopcount++)
{
cout << "Enter candidate name" << (loopcount+1) << ":";
cin >> names[loopcount];
}
for (loopcount=0; loopcount <=4; loopcount++)
{
cout << "Enter number of votes" << (loopcount+1) << ":";
cin >> votes[loopcount];
}
for (loopcount=0; loopcount <=4; loopcount++)
{
loopcount+1;
totvotes=votes[0]+votes[1]+votes[2]+votes[3]+votes[4];
setprecision(2);percentage[loopcount]=votes[loopcount]/totvotes*100;
}
for (loopcount=0; loopcount <=4; loopcount++)
{
cout.setf(ios::fixed);
cout << setprecision(0) << "Candidate " << names[loopcount] << " had " << votes[loopcount];
cout << " votes and ";
cout.setf(ios::fixed);
cout << setprecision(2) << percentage[loopcount]<< "% of total votes." << endl;
}
cin.get();
cin.get();
return(0);
}
//---------------------------------------------------------------------------
Any help anyone can off is greatly appreciated!!!!
-
#include <vector> for change number one.
Just took a cursory look at your code, but you can't initialize a string with zero.
Try
Code:
vector<string>names(5, " ");
-Skipper
P.S. Ran a couple of tests on your code - nothing rigorous - but it seems to work quite nicely. :)
-
thanks
Thank you so much!!! I really appreciate the info!!!! It really helped.