#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    44
    Rep Power
    13

    need help with a small voting program


    Hello,

    Was wondering if anybody had any sample code for a simple voting program?

    Bascially when the program runs - > asks if user would like to vote or not
    - > If Yes
    - > option 1
    - > option 2
    - > option 3
    - > option 4
    - > then show amount of people have voted for options
    - > if no - > loop back to the start

    A very simple sample is all I need as I'm new to c++ any help is appreciated!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0
    Do you need any storage of info? If you do, I'll go ahead and include that later. You said you're new to C++, so I'm assuming no. Here's an example of what I'd do.

    Code:
    #include <iostream.h>
    
    void main(void){
    	//these 4 variables are the user's possible options
    	//they are initialized to zero (they have no default value)
    	int i1=0;
    	int i2=0;
    	int i3=0;
    	int i4=0;
    	
    	char chChoice;	//this variable will store the user's yes or no choice
    	char chVote;	//this variable will store the user's voting choice
    
    	do{	//begins the loop
    		cout << "Would you like to vote? ";	//asks the user if they would like to vote
    		cin >> chChoice;	//inputs their choice into chChoice
    
    		if(chChoice=='y'){	//if they chose yes
    			
    			//output a menu
    			cout << "\nVote for who?";
    			cout << "1.) Person 1";
    			cout << "2.) Person 2";
    			cout << "3.) Person 3";
    			cout << "4.) Person 4";
    
    			cin >> chVote;	//input their choice into chVote
    
    			if(chVote=='1'){	//if they chose person 1
    				i1++;			//increase i1 by 1
    								//could also be written as i1=i1+1;
    			}
    
    			else if(chVote=='2'){	//if they chose person 2
    				i2++;			//increase i2 by 1
    			}
    
    			else if(chVote=='3'){	//if they chose person 2
    				i3++;			//increase i3 by 1
    			}
    
    			else if(chVote=='4'){	//if they chose person 2
    				i4++;			//increase i4 by 1
    			}
    
    			//outputs a display
    			cout << "Voting Results:\n================\n\n";
    		
    			cout << "\nPerson 1: " << i1;	//tells how many votes person 1 had
    			cout << "\nPerson 2: " << i2;	//tells how many votes person 2 had
    			cout << "\nPerson 3: " << i3;	//tells how many votes person 3 had
    			cout << "\nPerson 4: " << i4;	//tells how many votes person 4 had
    			cout << endl;	//outputs another blank line (same as "\n" for our purposes)
    		
    		}	//closes the if statement if their choice was yes to vote
    	}	//closes the loop
    	while(chChoice != 'n')	//if they entered anything but 'n', loop again
    }	//closes the main function

IMN logo majestic logo threadwatch logo seochat tools logo