2 Replies - 1137 Views - Last Post: 28 October 2008 - 10:15 PM Rate Topic: -----

#1 srnxfalcon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-October 08

Help with preventing infinite loops.

Post icon  Posted 28 October 2008 - 09:32 PM

Hey, I'm not all that good with C++, The program below works fine, as long as the user makes valid inputs, but if the user does something wrong, it usually ends up with my console getting massed with the main menu over and over. Below are the beginning of my program, and the functions where the user inputs something, which is where I would guess, stuff goes wrong.

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.
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with preventing infinite loops.

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Help with preventing infinite loops.

Posted 28 October 2008 - 10:08 PM

A cheesy way that I prevent infinite loops is to make a loop counter.

int loop_counter=0;
while(i!=100) {
 ...
 loop_counter++;
 if(loop_counter==1000) i=100;
}



This will safeguard the loop from running away.
Was This Post Helpful? 0
  • +
  • -

#3 srnxfalcon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-October 08

Re: Help with preventing infinite loops.

Posted 28 October 2008 - 10:15 PM

Actually, looking into it. Even if I stop the main menu from being displayed a million times, it shows error messages from other functions.

Anyone have anything that I can do?

I'm thinking of slapping a bunch of cin.clear/cin.ignore, but that just doesn't seem that good...

This post has been edited by srnxfalcon: 28 October 2008 - 10:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1