Thread: Stuck in a loop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Long story short, it gets through the 10th voter and stops... it should reset to District 2 and start again with voter 1. I'll take voter #11, but having it start at 1 again for District 2 would be ideal.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    This can't be your latest code - this fails to compile quite severely!
    I've gone through it and here's what I have: you have no
    function prototypes, the variables referenced in both functions
    are declared in main so they cant be used, in district1 function
    you have the word "do" where there should be a "while" and
    lastly your code formatting is horrible!

    Code:
    #include <iostream>
    
    using namespace std;
    
    void district1();	//Need prototypes
    void district2();
    
    
    int main()
    {
    	cout<<"Welcome to the election gizmo\n\n";
    	district1();
    	district2();
    	return 0;				
    
    }
    
    void district1()
    {
    	int voterNum = 1;
    	int candidateA = 0, candidateB = 0;
    	char choice;
    
    	cout<<"District 1 is first\n";
    
    	while (voterNum <= 10)			
    	{
    		cout<<"Voter Number "<<voterNum<<" please vote now.\n";
    		cout<<"Your choices are candidate 'A' and candidate 'B'\n";
    		cout<<"Please enter your choice now: ";
    		cin>>choice;
    		
    		if (choice=='A'||choice=='a')
    		{
    			candidateA++;
    		}
    			
    		if (choice=='B'||choice=='b')
    		{
    			candidateB++;
    		}
    
    		cout<<endl<<voterNum<<" "<<candidateA<<" "<<candidateB<<endl;
    		voterNum++;
    	}
    }
    	
    void district2()
    {
    	int voterNum = 1;
    	int candidateA = 0, candidateB = 0;
    	char choice;
    
    	cout<<"District 2 is next\n";
    
    	while (voterNum <= 10)			
    	{
    		cout<<"Voter Number "<<voterNum<<" please vote now.\n";
    		cout<<"Your choices are candidate 'A' and candidate 'B'\n";
    		cout<<"Please enter your choice now: ";
    		cin>>choice;
    		
    		if (choice=='A'||choice=='a')
    		{
    			candidateA++;
    		}
    			
    		if (choice=='B'||choice=='b')
    		{
    			candidateB++;
    		}
    
    		cout<<endl<<voterNum<<" "<<candidateA<<" "<<candidateB<<endl;
    		voterNum++;
    	}
    }
    thsi compiles but i fail to see the reason to have two functions
    that perform identical tasks. Since this is c++ i'd make
    an election class with district numbers, number of voters and
    number of votes per candidate as private variables, then have
    an election member function that performs the task that your
    district functions perform, and then if main needs the results
    of the election (which i assume it does, since it would be a fairly
    insignificant program otherwise), write functions that return
    the results. Just a suggestion
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM