1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with strings & doubles using Dynamic Arrays in C++

Discussion in 'C++' started by tdeyoung, Apr 25, 2010.

  1. tdeyoung

    tdeyoung New Member

    Joined:
    Apr 25, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am new to the programming community and I am having difficulty with one function in my program. We are suppose to have the user enter the number of candidates which will set my array size. They then enter the candidates name and the number of votes the candidate received. My program then takes that information and adds up the percentages and prints them out with the corresponding information. My program is also suppose to declare the winner. I can get the largest number of votes, but I can't get the name to print out with it. That is where I am lost. Can anyone help me with this? Here is my program so far:

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum);
    void printElectionTable(string *candidate, int arraySize, double *votes, double *percent, double sum);
    void maxVotes (int arraySize, string *candidate, double *votes);
    int main()
    {
        string *candidate;     //the * takes the place of the array bracket on the end
        int arraySize;         //which makes it a pointer.  Pointers are arrays and arrays are pointers.
        double *votes;
        double sum = 0;
        double *percent;
        
        cout << "Please enter the Number of Candidates. ";
        cin >> arraySize;
        cout << endl;
        
        candidate = new string[arraySize];  //builds the array we asked for
        votes = new double[arraySize];      //assigns the arraySize we asked for to our variables.
        percent = new double[arraySize];
        
        
        for (int index = 0; index < arraySize; index++)
        {
           cout << "Enter the Last Name of the Candidate: ";
            cin >> candidate[index];
            
            cout << "Enter the Votes " << candidate[index] << " " << "received: ";
            cin >> votes[index];
            
            sum = sum + votes[index]; 
            cout << endl;
        }
        
        percentOfVotes(candidate, arraySize, votes, percent, sum);
        maxVotes (arraySize, candidate, votes);
                 
    system("pause");
    return 0;
    }
    void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum)
    {
         
         
         for (int index = 0; index < arraySize; index++)
         {
             percent[index] = ((votes[index] / sum) * 100);
             
             cout << fixed << showpoint << setprecision(2);
             cout << candidate[index] << " " << votes[index] << " ";
             cout << "% Received " << percent[index] << endl;
         }      
    }
    void maxVotes (int arraySize, string *candidate, double *votes)
    {
         
         int maxIndex = 0;
         double maxIndex;
    
         for (int index = 0; index < arraySize; index++)
         {
             if (votes[maxIndex] < votes[index])
                 maxIndex = index;
             maxVotes = votes[maxIndex];
         }
    
         //These are some of the things I tried
    //     for (int index = 0; index < arraySize; index++)
    //     {
    //         if (candidate[index][votes])
    //             maxVotes = index;
    //         candidate[index] = maxVotes;
    //     }
    //     for (int index = 0; index < arraySize; index++)
    //     {
    //       votes[maxIndex] = candidate[index];
    //     if (candidate[index] = maxVotes)
             
              cout << "The Winner of the Election is:  " << *candidate << votes[maxIndex] << endl;
    }     
             
    //void printElectionTable (string *candidate, int arraySize, double *votes, double *percent, double sum)
     
    Last edited by a moderator: Apr 25, 2010
  2. ThorAsgard

    ThorAsgard New Member

    Joined:
    Feb 16, 2010
    Messages:
    9
    Likes Received:
    3
    Trophy Points:
    0
    Location:
    Somerset,UK
    Hi tdeyoung,

    I am new here and to C++ as well
    I looked at your code and found this works for me


    Code:
     
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
     
    void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum);
    void maxVotes (int arraySize, string *candidate, double *votes);
     
    int main()
    {
    string *candidate; //the * takes the place of the array bracket on the end
    int arraySize; //which makes it a pointer. Pointers are arrays and arrays are pointers.
    double *votes;
    double sum = 0;
    double *percent;
     
    cout << "Please enter the Number of Candidates. ";
    cin >> arraySize;
    cout << endl;
     
    candidate = new string[arraySize]; //builds the array we asked for
    votes = new double[arraySize]; //assigns the arraySize we asked for to our variables.
    percent = new double[arraySize];
     
     
    for (int index = 0; index < arraySize; index++)
    {
    cout << "Enter the Fisrt Name of the Candidate: ";
    cin >> candidate[index];
     
    cout << "Enter the Votes " << candidate[index] << " " << "received: ";
    cin >> votes[index];
     
    sum = sum + votes[index]; 
    cout << endl;
    }
     
    percentOfVotes(candidate, arraySize, votes, percent, sum);
    maxVotes (arraySize, candidate, votes);
     
    return 0;
    }
    void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum)
    {
     
     
    for (int index = 0; index < arraySize; index++)
    {
    percent[index] = ((votes[index] / sum) * 100);
     
    cout << fixed << showpoint << setprecision(2);
    cout << candidate[index] << " " << votes[index] << " ";
    cout << "% Received " << percent[index] << endl;
    } 
    }
    void maxVotes (int arraySize, string *candidate, double *votes)
    {
     
    int maxIndex = 0;
    double maxVotes;
    for (int index = 0; index < arraySize; index++)
    {
    if (votes[maxIndex] < votes[index])
    maxIndex = index;
    maxVotes = votes[maxIndex];
    }
     
    cout << "The Winner of the Election is: " << candidate[maxIndex] << " with " << votes[maxIndex] << " votes" << endl;
    } 
     
    
    The only change i have made is in the maxVotes routine
    This is my first time helping some one so i hope i have got it correct

    Thor :)
     
  3. tdeyoung

    tdeyoung New Member

    Joined:
    Apr 25, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thor

    I did get it and I appreciate your help. I realized after I sent it, that I was one word off which is usually the case. Sometimes you just need an extra pair of eyes:)
     

Share This Page