making a poll

this is the draft syntax

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
  #include <iostream>
#include <string>
         using namespace std;
         int main ()
{
         cout << "THIS IS A SIMPLE POLL" <<endl;
         
         string q1;
         cout << "NAME:" <<endl;
         getline (cin,q1);
         string q2;
         cout << "AGE:" <<endl;
         getline (cin,q2);
         string p1;
         cout << "Do you drink milk at breakfast? Y/N " <<endl;
         getline (cin,p1);
         string p2;
         cout << "Do you eat cereals on your breakfast? Y/N " <<endl;
         getline (cin,p2);
         string p3;
         cout << "Do you take bananas on your breakfast? Y/N " <<endl;
         getline (cin,p3);
         
         cout << "Total number of persons who drink milk at their breakfast: "<<p1<<endl;
         cout << "Total number of persons who eat cereals on their breakfast: "<<p2<<endl;
         cout << "Total number of persons who take bananas on their breakfast: "<<p3<endl;
         
         system("PAUSE");
         return 0;
}


1st of all i got another pending thread.. sorry for multi threading as i dont want to mix stuffs in one thread :) anyways,

i know that in my draft syntax to make a poll it should go that way..

but its only a draft anyways :)

so what my issue is..

for example i need like 5 persons to answer a poll

and on lines 15,18 and 21 they will answer only with Y/N and what do i want to happen is at line 24,25 and 26 i want to see how many people answered Y or N and also..

how can i make the lines 9,12,15,18 and 21 repeat it self (cuz its time for the 2nd person to take the poll) without actually retyping all of them (or copy and paste)

do i need to set an integer/variable value to Y/N so that it displays the number of persons who says either Y/N and if so..

how?

cheers,

xei.
A if statement would work say if you went
1
2
3
4
5
6
7
8
9
10
 
if (p1 == 'Y') 
{
    int countyes++; // <-- note that I am assuming this this will be declared above
}
else
{
    int countno ++;
}
// set int and if statement up for each question  


or you could just build a 1d array to store the tally and perhaps a function for the if statement. As for the loop there is numerous way to do. If you know the amount of people then it could be as simple as
1
2
3
4
5
int counter = 0; 
while (counter < theamountofppl) 
{ 
    // rest of code
}

or you could use recursion or a for loop and so forth. If you don't know the amount of people then array may not be to your best intention especially if you aren't familiar with pointers.
Last edited on
kingkong200, your code is redefining the countyes and countno variables within the if/else block, so any previously defined countyes and countno variables would be left unchanged. This would also mean that you are essentially calling the increment operator on an uninitialized variable, which should force failed compilation.

Your code should look more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int countyes = 0, countno = 0;
string p1;

//code to initilize p1


if(p1 == "Y") 
{
    countyes++;
}
else
{
    countno ++;
}


Run this program and you'll see what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;

int main()
{
	int test = 5;

	if(test == 5) {
		int test = 27; //this is NOT the test in main
		cout << test << '\n'; //27
	}
	cout << test; //5
	cin.get();
}
Last edited on
yea I know what you mean, I did mean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int countyes = 0, countno = 0;
string p1;

//code to initilize p1


if(p1 == "Y") 
{
    countyes++;
}
else
{
    countno ++;
}


but then i stopped thinking while typing and went into auto mode no confusion was meant.
Last edited on
uhhh.. i cant get it to work XD and also if it will work how am i suppose to display it at line 24?
Last edited on
anyone? how can i insert a poll system to my syntax? XD
It depends on how you set it up if you used
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
int countyes = 0, countno = 0;
string p1;

//code to initilize p1


if(p1 == "Y") 
{
    countyes++;
}
else
{
    countno ++;
}


Then you have to make a cout statement saying something like
1
2
 
cout << "The amount of people who said yes to question 1 are" << countyes << endl; 


the out put should be like

The amount of people who said to yes to question 1 are 3


Keep in mind that you need a individual counter for each question and the corresponding cout statement to display it.

Because
 
   cout << "Total number of persons who drink milk at their breakfast: "<<p1<<endl;


Will produce

Total number of persons who drink milk at their breakfast: Y //<-- assuming the person answered Y 
Last edited on
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
41
#include <iostream>
#include <string>
         using namespace std;
         int main ()
        
{
         char name[20];
         int age,Y,N;
         int voters;
         cout<<"\t"<<"\t"<<"\t"<<"#####A voting poll#####";
         cout<<endl;
         cout<<"1.Yes"<<endl;
         cout<<"2.No"<<endl;
         cout<<"How many voters ? : ";
         cin>>voters;
         for(int s=1;s<=voters;s++)

{         
         cout << "Name: "<<endl;
         cin >> name;
         int countyes = 0, countno = 0;

         string p1;
         cout << "Do you drink milk at breakfast? Y/N " <<endl;
         cin >> p1;

         if(p1 == "Y") 
         {
          countyes++;
          }
          else if (p1 == "N")
          {
          countno ++;
          }
         cout << "Number of persons who drink milk at breakfast: " <<countyes<<endl;   
         }
        cout << "The number of persons who take the poll are: "<<voters<<endl;
         system("PAUSE");
         return 0;

}


ok so thats how i did the poll the thing is.. like i set the no. of persons to take the poll to 2 and in the end it doesnt tally all off those who says Y ..

just try to run the syntax and you'll see what im talking about XD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
  int countyes, countno = 0; 
// should be above the for loop because it is essence it is redeclaring those values as being zero.  
// so... 
 char name[20];
         int age,Y,N;
         int voters;
         int countyes = 0; 
         int countno = 0; 
// other code
for (int s = 1; s <=voters; s++)
{
   //code
}

I apologize if that was misleading in the post. Furthermore, I noticed it didn't work when I put in 'y' , but it did work when I put 'Y' so if you want it to be non specific you could go
1
2
3
4
5
6
7
8
9
10
if (p1 == 'Y' || p1 == 'y') 
{
//code
}
else
{
//code
} 

//the else if statement really isn't necessary or you could use a switch function  


The last thing I noticed was the placement of
 
cout << "Number of persons who drink milk at breakfast: " <<countyes<<endl;


Was within the for loop as well and I am not sure if you wanted that as well. Also, while I am not sure it was what you wanted but, you need also a cout statement for the no's as well. It's the same idea but, replace countyes with countno. Hope this helps.
so with the latest code you provided that should count how many people who says yes right?

i'll test it out later as im not on my work desk :)

anyways i got another querry tho this with the syntax above line 35 will appear every time there is another set of voter. i wonder if how can i make it like line 37 who will apear only on the last? uhm.. if not trouble some to you? can you post the whole syntax of what it should be?
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
         using namespace std;
         int main ()
	 {
		 char name[20];
         int age, s; //removed Y and N integers 
         int voters;
		 int q1yes = 0, q2yes = 0, q3yes = 0; 
		 int q1no = 0, q2no = 0, q3no = 0; 
         string p1; // You only need one empty string not multiples
         cout<<"\t"<<"\t"<<"\t"<<"#####A voting poll#####";
         cout<<endl;
         cout<<"1.Yes"<<endl;
         cout<<"2.No"<<endl;
         cout<<"How many voters ? : ";
         cin>>voters;
         for(s=1;s<=voters;s++)
		 {
         cout << "Name: "; 
         cin >> name;
         cout << "Do you drink milk at breakfast? Y/N "; 
         cin >> p1;

         if(p1 == "Y" || p1 == "y") 
         {
			 q1yes++;
          }
          else
          {
			  q1no ++;
          }

cout <<  "do you drink cereal at breakfast?"; 
cin >> p1; 
  if(p1 == "Y" || p1 == "y") 
         {
			 q2yes++;
          }
          else
          {
			 q2no ++;
          }
cout 	<< "do you eat a banana at breakfast?"; 
cin >> p1; 
  if(p1 == "Y" || p1 == "y")
         {
			q3yes++;
          }
          else
          {
			q3no++; 
          }
   system("PAUSE");  // didn't make sense for me to have it at the end of program, but it also depends on your complier so adjust accordingly. 
         }
	cout << "the amount of people who said yes to question 1 was: " << q1yes << endl << "and those who said no was: " << q1no << endl; 
	cout << "the amount of people who said yes to question 2 was: " << q2yes << endl << "and those who said no was: " << q2no << endl; 
	cout << "the amount of people who said yes to question 3 was: " << q3yes << endl << "and those who said no was: " << q3no << endl; 
   cout << "Number of persons who said yes in total in this poll " << q1yes + q2yes + q3yes <<endl;   
   cout << " Total Num of ppl who said no in this quiz: " << q1no + q2no + q3no << endl; 
   cout << "The number of persons who take the poll are: "<<voters<<endl;
   return 0; 
   } 


The output would be:
 
                        #####A voting poll#####
1.Yes
2.No
How many voters ? : 2
Name: Bob
Do you drink milk at breakfast? Y/N y
do you drink cereal at breakfast?n
do you eat a banana at breakfast?Y
Press any key to continue . . .
Name: Help
Do you drink milk at breakfast? Y/N N
do you drink cereal at breakfast?n
do you eat a banana at breakfast?n
Press any key to continue . . .
the amount of people who said yes to question 1 was: 1
and those who said no was: 1
the amount of people who said yes to question 2 was: 0
and those who said no was: 2
the amount of people who said yes to question 3 was: 1
and those who said no was: 1
Number of persons who said yes in total in this poll 2
 Total Num of ppl who said no in this quiz: 4
The number of persons who take the poll are: 2
Press any key to continue . . .


It obvious that you will have to adjust some of the string, but in terms of what you were trying to do I think it does it correctly. Furthermore, it might be in your interest to set up a while loop in order to ensure y or Y or n or N is being placed into the program, but I'll leave that up to you.
Last edited on
thnx for the syntax.. i'll try to learn and understand that cuz im really confused when and where should i add another {} :)
Topic archived. No new replies allowed.