Any help would be greatly appreciated.
*/
This program will track US presidential relection results as they become
available. It will prompt the user for a state, and then the state's victor to find out how each state voted, it will then
allow the user to see the results of the election, until someone is declated a winner, or a tie occurs.
*/
#include<iostream>
#include<iomanip> //I always bring this one in, I don't know why
#include<string>
#include<cstdlib>
#include "elections.h"
using namespace std;
//Variables and Arrays
int count; //Counter variable for for loops
int choice; //Menu choice
int obamaVotes; //Votes for Obama
int mccainVotes; //Votes for McCain;
int undecVotes = TOTAL_NUM_VOTES; //Initial number of undecided votes
string stateStatus [NUM_STATES]; //Array involving who was the previous victor in a given state
/*
Variables from elections.h:
------------------------------
int TOTAL_NUM_VOTES = 538, number of votes from all states
int NUM_STATES = 51, Number of states voting + Washington D.C.
Arrays from elections.h:
---------------------------
string POSTAL_CODES[NUM_STATES] - Contains postal codes for states, listed alphabetically
int STATE_NUMVOTES[NUM_STATES] - Holds # of votes per state, listed alphabetically
*/
//Prototypes
int mainMenu ();
void menuSelection();
void updateResults();
void addVotes();
void obamaCheck();
void mccainCheck();
void undecCheck();
void obamaWon();
void mccainWon();
void viewResults();
//Main function. Display main menu, prompt user, then check user's choice, until the user inputs 3.
int main (){
do {
mainMenu();
menuSelection();
} while (choice != 3);
exit(0);
}
//Displays main menu, returns user's choice to see which option they chose,
int mainMenu(){
cout << "************************************\n";
cout << "* US Presidential Election 2008 *\n";
cout << "************************************\n";
cout << "Enter your choice: " << endl;
cout << "1 \t Update results for a state\n";
cout << "2 \t View results\n";
cout << "3 \t Quit" << endl;
cin >> choice;
return choice;
cout << endl;
}
//Function regarding what happens when user inputs from main menu.
void menuSelection (){
if (choice == 1){
updateResults();
}
if (choice == 2){
viewResults();
}
if ((choice != 1) && (choice != 2) && (choice != 3)){
cout << "Error: Invalid input.";
}
}
//If user presses 1, allow user to update results
void updateResults(){
string stateChoice;
cout << "*** UPDATING STATE RESULTS ***" << endl;
cout << "Enter the two-letter postal code for the state you wish to update: ";
cin >> stateChoice;
cout << endl;
for(count = 0; count < NUM_STATES;count++){ //Check user's input to each part of POSTAL_CODES until match is found.
if (stateChoice == POSTAL_CODES[count]){ //If a state matches, do addVotes, and tell next if that the input was legitimate
addVotes();
return;
}
}
cout << "Error: Please ensure your input is a state's code in capitals letters" << endl;
}
//Asks user who won the state, then checks who could have won the state before and updates totals
void addVotes(){
int wonChoice;
cout << POSTAL_CODES[count] << " has " << STATE_NUMVOTES[count] << " votes." << endl;
cout << "Who won " << POSTAL_CODES[count] << "?" << endl;
cout << "1 \t Barack Obama, Democratic Party" << endl;
cout << "2 \t John McCain, Republican Party" << endl;
cout << "3 \t Undecided" << endl;
cin >> wonChoice;
cout << endl;
//Depending on user's choice, checks any previous winners for state, then updates totals and state affiliations
if (wonChoice == 1){
obamaCheck();
stateStatus[count] = "obama";
}
if (wonChoice == 2){
mccainCheck();
stateStatus[count] = "mccain";
}
if (wonChoice == 3){
undecCheck();
stateStatus[count] = "undecided";
}
if ((wonChoice != 1) && (wonChoice != 2) && (wonChoice != 3)){
cout << "Error: Invalid Input." << endl; //Error if user decides that cheesecake was the winner in Georgia, or anything similar.
}
}

New Topic/Question
Reply



MultiQuote


|