c++ project

hi everyone, I'm still new to the programming world and I'm taking classes right now but I'm having some difficulty. I have a project that I need help creating and it requires to use functions. Up until now, we have been using pre-set functions but now we have to create them and I've been having issues trying to understand it. any help would be gratefully appreciated. I have what the final output is suppose to look like and I know how it should look, but when it comes to converting everything into a function (void, bool, etc...) I just end up confusing myself.


/* The program polls the user and collects the data regarding to their favorite 2008 presidency candidate. Once the user has stopped voting, the statistics on the topics are written to the screen. The user is given the option of taking the poll again, if he or she wishes.

This program should print your name, the program title, and the program objective to the screen. The output should be placed in a separate function. */

so this is what I have so far and im stuck...

#include <iostream>
#include <iomanip>
using namespace std;


void displayMenu()
{
cout << "Opinion Polls with Functions: \n";
cout << "*****************Opinion Polls***************** \n";
cout << "1. Hillary Clinton \n";
cout << "2. Barrack Obama \n";
cout << "3. John McCain \n";
cout << "4. Rallf Nader \n";
cout << "5. None of these are my favorite \n";
}


int main()
{
int selection;
char answer;

do
{
displayMenu();

cout << "Please choose your favorite candidate base on it corresponding => ";
cin >> selection;



while (selection < 1 || selection > 5)
{
cout << "Please select a valid option. \n";
cin >> selection;
}

if (selection <= 5 && selection >= 1)
{
cout << "Would you like to do this again? (y/n) ";
cin >> answer;
cout << endl;
}

if (answer == 'n' || answer == 'N')
{
cout << " OPINION POLL RESULTS \n";
cout << "Candidate \t Votes \t Percentage \n";
cout << "--------------------------------------------------------------------------- \n";

}


}
while (answer == 'y' || answer == 'Y');

return 0;
}


I need to have the table display the 5 choices and the number of times each option was selected and then show the percentage of each candidate. I haven't started building all the functions yet, I'm just trying build the program so it will function properly before I start creating the functions.
Last edited on
What specifically are you stuck on?
i'm currently doing the if statements to calculate the number of times each candidate was selected but somewhere along the lines, I can't get the calculations right
ok so far ive added this and it seems to be working for the number of times each candidate was selected



#include <iostream>
#include <iomanip>
using namespace std;


void displayMenu()
{
cout << "Opinion Polls with Functions: \n";
cout << "*****************Opinion Polls***************** \n";
cout << "1. Hillary Clinton \n";
cout << "2. Barrack Obama \n";
cout << "3. John McCain \n";
cout << "4. Rallf Nader \n";
cout << "5. None of these are my favorite \n";
}



int main()
{
int selection, percentage, votes, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0;
char answer;

do
{
displayMenu();

cout << "Please choose your favorite candidate base on it corresponding => ";
cin >> selection;



while (selection < 1 || selection > 5)
{
sum6++;
cout << "Please select a valid option. \n";
cin >> selection;
}

if (selection <= 5 && selection >= 1)
{
cout << "Would you like to do this again? (y/n) ";
cin >> answer;
cout << endl;
}

if (selection == 1)
{
sum1++;
}

else if (selection == 2)
{
sum2++;
}

else if (selection == 3)
{
sum3++;
}

else if (selection == 4)
{
sum4++;
}

else if (selection == 5)
{
sum5++;
}



if (answer == 'n' || answer == 'N')
{
cout << " OPINION POLL RESULTS \n";
cout << "Candidate \t Votes \t Percentage \n";
cout << "--------------------------------------------------------------------------- \n";
cout << "Hillary Clinton \t" << sum1 <<endl;
cout << "Barrack Obama \t" << sum2 <<endl;
cout << "John McCain \t" << sum3 <<endl;
cout << "Rallf Nader \t" << sum4 <<endl;
cout << "Not offered \t" << sum5 <<endl;
cout << "Invalid Votes \t" << sum6 <<endl;
}


}
while (answer == 'y' || answer == 'Y');

return 0;
}

ok so this seems to keep count of each option to choose from including invalid options, now I need to calculate the percentage of each choice. (opinions appreciated)

sorry for all the back to back posts, its helping me think.
Last edited on
Add up all your sums, cast to double, and divide each sum by the total.
/********************************************************************
* Programmer: John Johnson
* Date: 8/19/2014
* Course: COMP 122
* Assignment: Final Project
* Description: The program polls the user and collects the data
* regarding to their favorite 2008 presidency candidate.
*
* Input: keyboard - choose your favorite candidate.
* Output: Displays the percentage of each choosen candidate
*******************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;


void displayMenu()
{
cout << "Opinion Polls with Functions: \n";
cout << "*****************Opinion Polls***************** \n";
cout << "1. Hillary Clinton \n";
cout << "2. Barrack Obama \n";
cout << "3. John McCain \n";
cout << "4. Rallf Nader \n";
cout << "5. None of these are my favorite \n";
}



int main()
{
int selection, percentage, votes, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0;
char answer;

do
{
displayMenu();

cout << "Please choose your favorite candidate base on it corresponding => ";
cin >> selection;



while (selection < 1 || selection > 5)
{
sum6++;
cout << "Please select a valid option. \n";
cin >> selection;
}

if (selection <= 5 && selection >= 1)
{
cout << "Would you like to do this again? (y/n) ";
cin >> answer;
cout << endl;
}

if (selection == 1)
{
sum1++;
}

else if (selection == 2)
{
sum2++;
}

else if (selection == 3)
{
sum3++;
}

else if (selection == 4)
{
sum4++;
}

else if (selection == 5)
{
sum5++;
}


double total, perc1, perc2, perc3, perc4, perc5, perc6;
total = sum1 + sum2+ sum3 + sum4 + sum5 + sum6;
perc1 = (sum1 / total) * 100;
perc2 = (sum2 / total) * 100;
perc3 = (sum3 / total) * 100;
perc4 = (sum4 / total) * 100;
perc5 = (sum5 / total) * 100;
perc6 = (sum6 / total) * 100;


if (answer == 'n' || answer == 'N')
{
cout << fixed<< setprecision(0) << setw(2);
cout << " OPINION POLL RESULTS \n";
cout << "Candidate \t Votes \t Percentage \n";
cout << "--------------------------------------------------------------------------- \n";
cout << "Hillary Clinton \t" << sum1 << " \t" << perc1 << "% \n";
cout << "Barrack Obama \t" << sum2 << " \t" << perc2 << "% \n";
cout << "John McCain \t" << sum3 << " \t" << perc3 << "% \n";
cout << "Rallf Nader \t" << sum4 << " \t" << perc4 << "% \n";
cout << "Not offered \t" << sum5 << " \t" << perc5 << "% \n";
cout << "Invalid Votes \t" << sum6 << " \t" << perc6 << "% \n";
}



}
while (answer == 'y' || answer == 'Y');

return 0;
}



so everything appears to work the way it is suppose to, now the only thing left is to make it more efficient by creating another function. Any suggestions?
ok so ive been trying off and on all day today to figure out a way to convert some of this program into a function but I cant seem to get it right :/
Topic archived. No new replies allowed.