Does anybody know how to this question....

An election is contested by 5 candidates.They are numbered 1-5 and the voting is done by marking the candidate no on the ballot paper. Writea program to read the ballots and count the votes cast for each candidate using an array variable count.In case ano is read outside the range 1-5 the ballot is considered as aspoilt ballot.and also count the no of spoilt ballots..

Recommended Answers

All 31 Replies

we don't give answers to homework here, if you have a problem with some code then we can help

this is wat i did...but it just snt working..can u spot the error then?????????

#include<iostream.h>
#include<conio.h>


void main()
{
int vote;
int n;

cout<<"\n\n\tEnter the no of candidates(max-50)  ";
 cin>>n;

int num[50];
int big=0;
for (int i=0;i<n;i++)


  {

cout<<" Whos your favourite candidate(1-5)";
cin>>vote;

vote=vote++; }


for (int i=0;i<10;i++)
{
if (num[i]>big)
big=num[i];
}
cout<<"\n\n\t The biggest is "<<big;
getch();
}

i did not understand what u meant...thanks anywayz...

its supposed to be int main() not void main() and its alot easier to read if you declare your variables at the beginning of the function

my error is L value not defined ...could you tell me where the error is and how to correct it?

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
int vote;
const int limit=50;
int num[50];
int big=0;
char candidate[limit];



cout<<"\n\n\tEnter the no of candidates(max-50)  ";
 cin>>n;


  for (int i=1;i<n;i++)
{

cout<<" Whos your favourite candidate(1-5)";
cin>>vote;

vote=vote++;


if (vote=1)
candidate='cnd1';
else if (vote=2)
candidate='cnd2';
else if (vote=3)
candidate='cnd3';
else if  (vote=4)
candidate='cnd4';
else if (vote=5)
candidate='cnd5';
else cout<<"SPOILT BALLOT";
            }

for (int i=1;i<n;i++)
{
if (num[i]>big)
big=num[i];
}
cout<<"\n\n\t The biggest is "<<big;
getch();
}

could you xplain that?

#include<iostream>
#include<conio.h>
using namespace std;


int main()
{
int n;
int vote = 0;
//const int limit=50;
int num[50];
int big=0;
char candidate[50];



cout<<"\n\n\tEnter the no of candidates(max-50) ";
cin>>n;


for (int i=1;i<n;i++)
{

cout<<" Whos your favourite candidate(1-5)";
cin>>vote;

vote++;

//remeber == is normal equals in programming
if (vote == 1){
//you use strcpy to copy a string to a string
    strcpy(candidate, "cnd1");
}
else if (vote == 2){
    strcpy(candidate, "cnd2");
}
else if (vote == 3){
    strcpy(candidate, "cnd3");
}
else if (vote == 4){
    strcpy(candidate, "cnd4");
}
else if (vote == 5){
    strcpy(candidate, "cnd5"); 
}
else { 
    cout<<"SPOILT BALLOT";
}
}
          

for (int i=1;i<n;i++)
{
if (num[i]>big)
big=num[i];
}
cout<<"\n\n\t The biggest is "<<big;
getch();
}

......................................but this programme you sent doesnt work..what do i do????????

#include<iostream>
#include<conio.h>
using namespace std;


int main()
{
int n;
int vote = 0;
//const int limit=50;
int num[50];
int big=0;
char candidate[50];



cout<<"\n\n\tEnter the no of candidates(max-50) ";
cin>>n;


for (int i=1;i<n;i++)
{

cout<<" Whos your favourite candidate(1-5)";
cin>>vote;

vote++;

//remeber == is normal equals in programming
if (vote == 1){
//you use strcpy to copy a string to a string
    strcpy(candidate, "cnd1");
}
else if (vote == 2){
    strcpy(candidate, "cnd2");
}
else if (vote == 3){
    strcpy(candidate, "cnd3");
}
else if (vote == 4){
    strcpy(candidate, "cnd4");
}
else if (vote == 5){
    strcpy(candidate, "cnd5"); 
}
else { 
    cout<<"SPOILT BALLOT";
}
}
          

for (int i=1;i<n;i++)
{
if (num[i]>big)
big=num[i];
}
cout<<"\n\n\t The biggest is "<<big;
getch();
}

What do you mean it doesn't work?, you are going to have to be more specific

i meant that when i run the program and i enter 5..it comes spoilt ballot but what i actually want is if it isnt from 1-5 its supposedto be spoilt...n moreover it comes as biggest=48097456...i dont know why!!

and strcpy...i havnt learnt that code ...is there any substitute for that?

THIS IS MY PROGRAMME......PLZ FIND OUT THE ERROR BECAUSE IAM NOT ABLE TO..........................

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
int vote=0;
const int limit=50;
int num[50];
int big=0;
char candidate[limit];



cout<<"\n\n\n\n\n\tEnter the no of candidates(max-50)  ";
 cin>>n;

  for (int i=1;i<=n;i++)

{

cout<<"\n\n\n\t Who is your favourite candidate(1-5)";
cin>>vote;

vote=vote++;



if (vote=1)
candidate[i]='cnd1';
else if (vote=2)
candidate[i]='cnd2';
else if (vote=3)
candidate[i]='cnd3';
else if  (vote=4)
candidate[i]='cnd4';
else if (vote=5)
candidate[i]='cnd5';
else{
cout<<"SPOILT BALLOT";
}

            }


for (int i=1;i<=n;i++)
{
if (num[i]>big)
big=num[i];

}         int i;

cout<<"\n\n\t The person with the most no of votes is "<<candidate[i];

getch();

}
commented: Whiny "leet speek" void main'er who still can't use code tags -4
commented: Totally agree with Salem.. +0

THIS IS MY PROGRAMME......PLZ FIND OUT THE ERROR BECAUSE IAM NOT ABLE TO..........................

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
int vote=0;
const int limit=50;
int num[50];
int big=0;
char candidate[limit];



cout<<"\n\n\n\n\n\tEnter the no of candidates(max-50)  ";
 cin>>n;

  for (int i=1;i<=n;i++)

{

cout<<"\n\n\n\t Who is your favourite candidate(1-5)";
cin>>vote;

vote=vote++;



if (vote=1)
candidate[i]='cnd1';
else if (vote=2)
candidate[i]='cnd2';
else if (vote=3)
candidate[i]='cnd3';
else if  (vote=4)
candidate[i]='cnd4';
else if (vote=5)
candidate[i]='cnd5';
else{
cout<<"SPOILT BALLOT";
}

            }


for (int i=1;i<=n;i++)
{
if (num[i]>big)
big=num[i];

}         int i;

cout<<"\n\n\t The person with the most no of votes is "<<candidate[i];

getch();

}

...
you're not going to finish it unless someone does it for you. but nobody will.

and strcpy...i havnt learnt that code ...is there any substitute for that?

not that I know of

Get rid of iostream.h and conio.h. They're outdated. Use iostream instead of iostream.h. Don't use getch(). Use cin.get() instead and you won't need conio.h. Change void main() to int main() and have main return 0 .

Repost your updated code using proper code tags:

[code=cplusplus] // paste code here

[/code]

and indent/format correctly please. Please give a more precise explanation of what you've tried, what doesn't work, what does, and a much more precise question. Here's a link for strcpy.

http://www.cplusplus.com/reference/clibrary/cstring/strcpy.html

commented: yup +6

I have not gone thru the total prog. but it seems that
1. you have not initialised num[] array and at the end of the program you are comparing it with big.
2. You hv filled array candidate and not used it again.

Is this the only code

May be I am wrong let me go thru this again and revert

And there's three problems with your if-statements:

1: == is the comparison operator, not =
2: "cnd1" is not a char, it's a string. canidates[] is a char array.
3: if canidates[] was a char string array, you'd need to use strcpy(), not the assignment operator.

i dont understand why this isnt working....
can anyone explain.............


* i havnt studied all the complicated codes...so i have to do with the codes i have with me....
*so strcpy() and cin.get() --i havnt studied....plz do respond

#include<iostream.h>
#include<conio.h>


int main()
{
int n;
int vote=0;
const int limit=50;
int num[50];
int big=0;
char candidate[limit];



cout<<"\n\n\n\n\n\tEnter the no of candidates(max-50)  ";
 cin>>n;

  for (int i=1;i<=n;i++)

{

cout<<"\n\n\n\t Enter ur favourite candidates no (1-5)  ";
cin>>vote;

vote=vote++;



if (vote=1)
candidate[i]='cnd1';
else if (vote=2)
candidate[i]='cnd2';
else if (vote=3)
candidate[i]='cnd3';
else if  (vote=4)
candidate[i]='cnd4';
else if (vote=5)
candidate[i]='cnd5';
else{
cout<<"SPOILT BALLOT";
}

            }


for (int i=1;i<=n;i++)
{
if (vote>big)
big=vote;

}

cout<<"\n\n\t The person with the most no of votes is  candidate "<<big;

getch();
return(0)
}

Why didn't you read the replies you already got? This code still has the same errors in it as mentioned before.

line 35: if (vote=1) should be if (vote==1) Same goes for lines 37-44

line 36: candidate[i]='cnd1'; As mentioned before: you can't assign values to a char-array this way.
use strcpy instead: strcpy(candidate, "cnd1"); // note the double quotes or better, use std::strings


And without spoiling the suprise:

for (int i=1;i<=n;i++)
    {
        if (vote>big)
            big=vote;

    }

That won't work. Write it out on paper and see where your logic-error is.

Why didn't you read the replies you already got?

He obviously doesn't want to learn and can't google. :P

why wont this one work???????....

#include<iostream.h>
#include<conio.h>
void main()
{

int candidate[0]=0;
int candidate[1]=0;
int candidate[2] = 0
int candidate[3]=0;
int candidate[4] = 0
int candidate[5] = 0



for (int i=0;i<=5;i++)

{

cout<<"\n\n\n\t Enter ur favourite candidates no (1-5)  ";
cin>>vote;

if (vote==1)
candidate[vote]++;
else if (vote==2)
candidate[vote]++;
else if (vote==3)
candidate[vote]++;
else if  (vote==4)
candidate[vote]++;
else if (vote==5)
candidate[vote]++;
else{
cout<<"SPOILT BALLOT";
}

            }

for (int i=0;i<=5;i++)

{
 if (vote[i]>big)
 big=vote[i];

  }

    getch();
    }

...
USE. CODE. TAGS.

CHANGE IT TO int canidate[5];

...
USE. CODE. TAGS.

Yep

CHANGE IT TO int canidate[5];

Actually, I think he wants: int candidate[5] = {0,0,0,0,0};

why wont this one work???????....

Be more specific about what is not working, spend some time trying to describe the exact problem(s), instead of just posting slightly modified code time after time (without code tags).

Then about arrays and indexes, assuming that you eventually choose to have an array of 5 integers, as suggested by niek_e, i.e.

int candidate[5] = {0,0,0,0,0};

Now to iterate through all the 5 indexes using a for loop, goes from 0 to 4

for (int i = 0; i < 5; i++)
{
     cout << "candidate[" << i << "] is: " << candidate[i] << '\n';
}

The problem actually is i dont get the corect no of spolit ballots...i get it as 4325678................

this is my final program...yet there the spoilt ballot doesnt come properly

#include<iostream.h>
#include<conio.h>
void main()
{

char choice;
int candidate[50],vote,waste,big,who;



for (int i=1;i<=5;i++)
{candidate[i]=0;}     //doing this to give starting value of count as zero

//VOTING PROCESS

do{

  cout<<"Voting complete? (y/n)? ";
  cin>>choice;
  cout<<"\n\n\n\t Enter ur favourite candidates no (1-5)  ";
  cin>>vote;
  if(vote>5)
   {cout<<"Invalid vote";
    waste++;
        }
  else
  candidate[vote]++;

}while((choice=='n')||(choice==
'N'));



big=0;
who=0;
for (int i=1;i<6;i++)
  {
    if (candidate[i]>big)
       { big=candidate[i];     //big is largest number of votes
         who=i;                 // 'who'  gets the 'big' number of votes
       }
  }

cout<<"\n The candidate with largest number of votes is Candidate "<<who;
cout<<"\n Number of invalid votes="<<waste;
getch();
}

now this one is in the correct format n everything ...but i dont get the spoli ballots right....y????

#include<iostream.h>
#include<conio.h>


void main()
{

char ans;

int vote,big,candidate[50],invalid,cand;



for (int i=1;i<=5;i++)
{candidate[i]=0;}   //starting value of count is 0



do
{    //do while

           cout<<"\n\n\n\t Enter ur favourite candidates no (1-5)  ";
           cin>>vote;
                 if (vote>5)
                     { cout<<"Spoilt Ballot";
                        invalid=invalid+1;
                       }//if statement
                 else candidate[vote]++;
     cout<<"\n\tContinue voting(y/n) ?";
         cin>>ans;
   }while( ans=='y');


   //Counting votes to see who the most ;



   big=0;
   cand=0;

  for(int i=1;i<=5;i++)

  {

     if (candidate[i]>big)
        {big=candidate[i];   //big is the most no of votes
       cand=i;             //cand gets the big no of votes
        }
  }

  clrscr();
  cout<<"\n\n\t The candidate with largest number of votes is Candidate "<<cand;
cout<<"\n\n\t Number of spolit ballots ="<<invalid;
getch();
}

if (vote>5) You mean if (vote>4) 0 to 4 is 5 candidates.
0 to 5 is 6 candidates.

Also read this about code formatting. This code is almost unreadable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.