Stuck in a loop

This is a discussion on Stuck in a loop within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a code that runs through 2 different districts with 10 voters each. They must vote in ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    38

    Stuck in a loop

    I'm trying to write a code that runs through 2 different districts with 10 voters each. They must vote in order and it must start with district 1.

    The goal is to get through all of this and report the data (hard numbers, winners, losers, etc. for candidate A or B.

    My program runs through the district1() function, but won't call district2... I think there's a problem with my loop or my approach.

    Thanks

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
    
    	int voterNum=1;		//voter number must start at 1
    	int count1,count2;
    	int district;
    	int candidateA=0, candidateB=0;	//these must start at 0 because they can't start with votes
    	char choice;
    	int total1,total2;	
    
    	cout<<"Welcome to the election gizmo\n\n";
    	district1();				//calls function district1
    	district2();				//calls function district2
    	return 0;				
    
    }
    
    void district1()
    {
    	cout<<"District 1 is first\n";
    	do (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=candidateA+1;	//adds 1 vote if picked
    				}
    				
    			if (choice=='B'||choice=='b')
    				{
    					candidateB=candidateB+1;	//adds 1 vote if picked
    				}
    
    			voterNum=voterNum+1;		//increases voter number so it knows when "who" is voting
    		
    
    		cout<<endl;
    		cout<<voterNum<<" "<<candidateA<<" "<<candidateB;	//running number count****tracking while i write
    		cout<<endl;
    
    		}
    }
    	
    void district2()
    {
    		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=candidateA+1;
    				}
    				
    				if (choice=='B'||choice=='b')
    				{
    					candidateB=candidateB+1;
    				}
    
    		voterNum=voterNum+1;
    		cout<<endl;
    		cout<<voterNum<<" "<<candidateA<<" "<<candidateB;
    		cout<<endl;
    		}
    		
    }

  2. #2
    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.

  3. #3
    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

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    I am a beginner after all. Do you really think I'd be on the boards if I were GOOD at this?

    But seriously, I am new to functions and I see where the second part of your argument makes sense. I'll try it that way. The program does seem pretty straight forward, I just get hung on little things.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I didn't mean to sound insulting, i was just showing you what you
    did wrong, and i wouldn't be answering questions on the boards
    if i wasn't interested in being helpful.

    You should try implementing one function to do the job, obviously
    if you are just starting out, classes are a little bit down the road.
    When you do come to classes though, remember what i said,
    as that would be a good basic class to get you comfortable
    with them.

    Also, one last thing, if you want to be able to return both values
    from a function, there are two ways you could do it in this
    program:

    1) use a macro to define the maximum number of voters in the
    district, and let the function return the results for one particular
    candidate - then subtract the two to find the result for the other

    2) or the more typical way of using pass by reference - c++ has
    features which simplify this significantly as opposed to using
    pointers - i assume though that you have not encountered either
    pointers or references yet, so i put this little program together
    to show how to get multiple values returned from a function:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void square2numbers (int &num1, int &num2);
    //notice the void return type - not a
    //requirement, just for demonstration
    
    //also notice the ampersands (&) this part is important!
    
    int main (void)
    {
    	int a, b;
    
    	cout << "Enter number a: ";
    	cin >> a;
    
    	cout << endl << "Enter number b: ";
    	cin >> b;
    
    	square2numbers (a, b);	//pass parameters as normal
    
    	cout << "The value of a-squared is: " << a <<  endl;	//However, the parameters
    	cout << "The value of b-squared is: " << b <<  endl;	//have been modified
    
    	return 0;
    }
    
    
    void square2numbers (int &num1, int &num2)
    {
    	num1 *= num1;	//squaring first parameter
    	num2 *= num2;	//squaring second parameter
    }
    that should help you avoid a roadblock down the line
    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

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Making more sense now. I appreciate the help. And don't think I was mad. I guess it's hard to read tone through letters and numbers.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Update:

    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++;
    		}
    		
    		
    		voterNum++;
    	}
    	cout<<"District 1 totals: \n\n";
    	cout<<"Candidate A: "<<candidateA<<endl;
    	cout<<"Candidate B: "<<candidateB<<"\n\n";
    	
    	if (candidateA>candidateB)
    	{
    		cout<<"Candidate A is the winner of this District\n\n";
    	}
    		else
    			{
    			if (candidateA<candidateB)
    				{
    				cout<<"Candidate B is the winnder of this District\n\n";
    				}
    			else
    				{				
    					cout<<"Tie in this district\n\n";
    				
    				}
    		}
    
    }
    	
    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++;
    		}
    		voterNum++;
    
    	}
    	cout<<"District 2 totals: \n\n";
    	cout<<"Candidate A: "<<candidateA<<endl;
    	cout<<"Candidate B: "<<candidateB<<"\n\n";
    	if (candidateA>candidateB)
    	{
    		cout<<"Candidate A is the winner of this District\n\n";
    	}
    		else
    			{
    			if (candidateA<candidateB)
    				{
    				cout<<"Candidate B is the winnder of this District\n\n";
    				}
    			else
    				{				
    					cout<<"Tie in this district\n\n";
    				
    				}
    		}
    		
    
    }
    I now need to bring the totals in each district to "Main" somehow... I need to declare a winner, loser, or tie.

    Do I need another function or can I pull them straight from each one? But if each function is independent, I can't just declare candidate a= 0, right?

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    (Ignoring the fact that you really should eliminate the huge amount of redundant code of 2 identical functions)

    The simplest way is probably to have your function return a value (maybe a number or a string) containing the winning candidate's info. eg,

    Code:
    int election()
    {
       // Voting & counting code here
       if (candidate1 > candidate2)
          return 1;
       else if (candidate2 > candidate1)
          return 2;
       else
          return 0; // Election drawn
    }
    Then, from the main() function in your program
    Code:
    int winner = election();
    There's actually alot of ways to do this, this perhaps isn't the best, but it's probably the easiest to put into your program (since you already have the if/else conditions in your functions)

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    I've just revised this... I did have redundant code. I didn't realize how functions worked. I was trying for District 1, 2... whatever...

    anyway, I've eliminated one of them and created 1 function
    Code:
    void vote();
    And it does essentially the same thing. However, I have to call it twice... does that make sense? Or is there another way?
    Code:
    #include <iostream>
    
    using namespace std;
    
    void vote();
    
    int main()
    {
    	cout<<"Welcome to the election gizmo\n\n";
    	void vote();
    	void vote();
    	return 0;				
    
    }
    
    void vote();
    {
    	int voterNum = 1;
    	int district = 1;
    	int candidateA = 0, candidateB = 0;
    	char choice;
    	
    	cout<<"District "<<district<<" 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++;
    		}
    		voterNum++;
    
    	}
    	cout<<"District "<<district<<" totals: \n\n";
    	cout<<"Candidate A: "<<candidateA<<endl;
    	cout<<"Candidate B: "<<candidateB<<"\n\n";
    	district++
    	if (candidateA>candidateB)
    	{
    		cout<<"Candidate A is the winner of this District\n\n";
    	}
    		else
    			{
    			if (candidateA<candidateB)
    				{
    				cout<<"Candidate B is the winnder of this District\n\n";
    				}
    			else
    				{				
    					cout<<"Tie in this district\n\n";
    				
    				}
    		}
    		
    
    }

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Quote Originally Posted by Bench82

    Code:
    int election()
    {
       // Voting & counting code here
       if (candidate1 > candidate2)
          return 1;
       else if (candidate2 > candidate1)
          return 2;
       else
          return 0; // Election drawn
    }
    Then, from the main() function in your program
    Code:
    int winner = election();
    There's actually alot of ways to do this, this perhaps isn't the best, but it's probably the easiest to put into your program (since you already have the if/else conditions in your functions)
    So would this piece of code go in "main" or imbedded within my function?

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    hey tu return the totals to main
    why not use some global variables

    here is an easy example of how to return a number from a function to main

    Code:
    #include <iostream.h>
    int z=0;
    
    void addition(int a, int b)
    {
        z+= a+b;   
    }
    
    int main()
    {
        addition(2,4);
        addition(2,2);
        cout << z;
        cin >> z;
        return 0;
    }
    hoe this helps just make one for A and B and then you can compare them in main

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    here's what i'm thinking - how about doing both districts votes
    in the same function? i've taken your last post and moved
    some things around, introduced a couple of loops and it now
    takes bot elections separately, and calculates the total result
    of both as well, and outputs the winner (or draw) to the screen

    Code:
    void vote()
    {
    	int voterNum;
    	int district;
    	int candidateA, candidateB;
    	int totalA = 0, totalB = 0;
    	char choice;
    
    	for (district = 1; district <= 2; district++)
    	{
    		candidateA = 0;
    		candidateB = 0;
    
    		for (voterNum = 1; voterNum <= 10; voterNum++)
    		{
    			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++;
    			}
    			voterNum++;
    		}
    
    		totalA += candidateA;
    		totalB += candidateB;
    
    		cout<<"District "<<district<<" totals: \n\n";
    		cout<<"Candidate A: "<<candidateA<<endl;
    		cout<<"Candidate B: "<<candidateB<<"\n\n";
    		
    		if (candidateA>candidateB)
    		{
    			cout<<"Candidate A is the winner of this District\n\n";
    		}
    		
    		else
    		{
    			if (candidateA<candidateB)
    			{
    			cout<<"Candidate B is the winnder of this District\n\n";
    			}
    			
    			else
    			{				
    				cout<<"Tie in this district\n\n";
    			}
    		}
    	}
    
    	if (totalA>totalB)
    	{
    		cout<<"Candidate A is the winner of this Election\n\n";
    	}
    	
    	else
    	{
    		if (totalA<totalB)
    		{
    		cout<<"Candidate B is the winner of this Election\n\n";
    		}
    		
    		else
    		{				
    			cout<<"Election tie!\n\n";
    		}
    	}
    }
    also avoid global variables like the previous poster said - i showed
    you a better way earlier using references
    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

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    That's starting to look better already!

    So would this piece of code go in "main" or imbedded within my function?
    Which bit?

    The part with the if/else statement was intended to be a modification for your function. You've already got a structure which outputs something to cout declaring the winner - You can expand it to return the result at the same time.

    Just to be clear - your function currently returns void. In real terms, that means there's no data output to the rest of your program from your function.
    You can alter the function to send a number out to your program after the function finishes, using a return statement.
    You'd have to change that void to some other type, such as int.


    The second bit of code in my post is an example creating a new variable, winner, to store the data output by the function

    This would be declared wherever you need to manipulate the result (most likely your main function, where you currently call vote() on its own )
    The example I gave is equivalent to
    Code:
    int winner;
    winner = vote();
    Assuming you alter vote() to return an int

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by yuri322
    hey tu return the totals to main
    why not use some global variables
    because.... Global Variables Are Evil!!!

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    only sometimes....

Page 1 of 2 12 LastLast
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, 03: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, 10:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21