Voting/Poll System

HI :) I'd like to share this, and I'm still confused on how to put an ID number, for ex. There are 20 students with 20 diff ID number only ranging to ID # 2012-10000 ~ 2012-10020.

And the program needs to keep running until those 20 students finished voting and the results will be out.

This is as far as I can code ~.~ I tried so hard but i still don't get to know how.

#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout << "============~~~~~|| Poll / Voting system ||~~~~~============" << endl << endl;
cout << "Survey from 20 students from 2012 CCS enrollees for the best laptop"<<endl;
cout <<"for the year 2012."<<endl <<endl;
char name [50];
char idnum [50];
int a=1,b,c=0,d=0,e=0,f=0,g=0,h=0;

Start:
h=h+c+d+e+f
a=a+1;


if(a==20)
{
c=c/h*100;
d=d/h*100;
e=e/h*100;
f=f/h*100;
g=g/h*100;

cout << "======= Results ========"<<endl
<<endl;
cout << "[1] MSI = "<<c<<endl;
cout << "[2] Razer = "<<d<<endl;
cout << "[3] Acer = "<<e<<endl;
cout << "[4] HP = "<<f<<endl;
cout << "[5] Alienware = "<<endl
<<endl;


}
cout << "Enter your ID number : 2012-";
cin.getline (idnum,49);
cout << "Enter your full name : ";
cin.getline (name,49);
cout <<endl;
cout << "Hi,"<<name<<endl<<endl;
cout << "Which of these brand of laptops do you prefer the most?"<<endl;
cout << "[1] MSI"<<endl;
cout << "[2] Razer"<<endl;
cout << "[3] Acer"<<endl;
cout << "[4] HP"<<endl;
cout << "[5] Alienware"<<endl
<<endl;
error:
cout << "Press any key ranging from 1 ~ 5"<<endl;
cin >>b;
if (b<0 || b>5)
{
cout << "Invalid. Please enter the designated number of the given choices."<<endl<<endl;
goto error;
}
else if (b==1)
c++;
else if (b==2)
d++;
else if (b==3)
e++;
else if (b==4)
f++;
else
g++;


goto Start:
}
!!NEVER EVER USE GOTO!!
This is programming style of the 1960s or before. You'll definitely completely lose track of your code.

Instead you may use control statements like loops. See http://www.cplusplus.com/doc/tutorial/control/ for details.

Some more hints:
- Try to give your symbols some descriptive names. "a", "b", "c", ... will tell the reader of your code only that you know your alphabet - but nothing about your program. A well coded program may be read like a text in natural language.
- Use C-functions to make your program well arranged.
closed account (zb0S216C)
@Blesswind: Please, use code tags with code-formatting.

tcs wrote:
"!!NEVER EVER USE GOTO!!"

"goto" is misused; there's nothing wrong with it at all. Based on what goes on in one's code, the compiler implicitly adds "goto" statements in certain situations. Specifically, "goto" is used extensively and explicitly for error-handling in C. So please, don't judge a construct when you're not actually familiar with its uses. Granted, in the OP's code, "goto" is being misused.

tcs wrote:
"Use C-functions to make your program well arranged."

*Confused beyond comprehension* Why would you suggest to the OP that s/he declare all functions as "extern "C"? C++ functions are not compatible with C functions unless they are declared "extern "C".

Wazzak
Last edited on
I tried to use Loop, But I can't figure it out.

What I want to happen is that If you input a number that is less than 1 or greater than 5.

then it will print "Invalid. Please enter the designated number of the given choices." and go back to the statement "Press any key ranging from 1 ~ 5"
@Blesswind:
F.e. you may use a do-while() loop as follows inside your main() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int more = 1;

cout << "============~~~~~|| Poll / Voting system ||~~~~~============" << endl << endl;

// ...

do
{
    // Enter your code between `Start:´ and `goto Start;´ here
    // (Don't forget removing `Start:´ and `goto Start;´)

    // Also use an inner do-while() loop to ask the user for his/her choice.
    // This loop may repeat until a valid input was given.

    // You may ask the user for `more´ here to get the loop finished.
} while (more != 0);


Sorry, I didn't thought of C versus C++ functions but of C/C++ versus functions in the sense of computer/math science. You could f.e. write a function prompting the user for some input and reading it until some valid input was given. This function could accept two parameters. The first one may be the prompt as string while the second may be a string consisting of all valid keys. It's a little bit primitive but you can use it at least at two different places: When asking the user for his/her laptop choice and when asking for `more´. This function may return a number.

@Framework:
Please don't be angry about my judgment about goto constructs. Believe me, I know what I say.


- I would try something along these lines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int id[20];
string name[20];

int choice[20]

for(int i=0;i<20;i++)
{
do 
{
cout<<"voter "<<(i+1)<<" enter your ID number( has to be between 10000 and 1020"<<endl;
cin>>id[i];
}
while( id<10000  || id> 10020); 

cout<<"voter "<<(i+1)<<" enter your name "<<endl;
getline(cin,name[i]);

do
{

cout<<" which choice of laptop?"<<endl;
cout<<"choices:"<<endl<<"1) "
<<endl<<"2)"
<<endl<<"3)"
<<endl<<"4)"
<<endl<<"5)"<<endl;
cout<<"Please enter a number from 1-5"<<endl;
cin>>choice[i]
}
while(choice[i]<1 || choice[i]>5);

}

// all 20 people should be able to vote

// to print out results you can make 5 int variables(one for each brand), and go 
//through the choice array. when the choice is 1-5, increment the corresponding 
//variable,etc.

Last edited on
Thank you guys, I got it now! :)
Topic archived. No new replies allowed.