Vectors and strings

This is a discussion on Vectors and strings within the C++ Programming forums, part of the General Programming Boards category; I need help as soon as possible. I am writing a program that lets you enter 5 names, the number ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    2

    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!!!!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    #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.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    2

    thanks

    Thank you so much!!! I really appreciate the info!!!! It really helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Multiple Multi-Part Strings From a Text File
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2009, 11:43 AM
  2. Incrementing Vectors of type int
    By dnguyen1022 in forum C++ Programming
    Replies: 5
    Last Post: 01-12-2009, 11:00 AM
  3. Some questions with strings, vectors, references...
    By samus250 in forum C++ Programming
    Replies: 11
    Last Post: 07-10-2008, 03:31 PM
  4. STL vectors
    By c++.prog.newbie in forum C++ Programming
    Replies: 4
    Last Post: 02-19-2005, 01:31 PM
  5. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 11:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21