here is my code
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class prospective
{
public:
prospective();
~prospective();
int get_Points() {return itsPoints;}
void set_Points(int number){itsPoints += number;}
int get_P_Number() {return itsP_Number;}
void set_P_Number(int number){itsP_Number=number;}
char* get_P_Name() {return itsP_Name;}
private:
char itsP_Name[15];
int itsP_Number, itsPoints;
};
prospective::prospective()
{
cout<<"Enter the name of the candidate"<<endl;
cin>>itsP_Name;
cin.clear();
cout<<"Enter the code number of the candidate"<<endl;
cin>>itsP_Number;
itsPoints=0;
}
prospective::~prospective()
{
}
class voter
{
public:
voter();
~voter();
int get_V_Number() const {return itsV_Number;}
void set_V_Number(int number){itsV_Number=number;}
private:
char itsV_Name[15];
int itsV_Number;
};
voter::voter()
{
cout<<"Enter the name of the voter"<<endl;
cin>>itsV_Name;
cout<<"Enter the code number of the voter"<<endl;
cin>>itsV_Number;
}
voter::~voter()
{
}
int vote(prospective *array,int x,int z)
{
int i,num,counter=x,*revotes,revote_counter_checker;
bool flag=false;
if(z!=0){
revotes=(int*)malloc(sizeof(int)*x);
if (revotes==NULL){
free(revotes);
cout<<"Error allocating memory"<<endl;
return 1;
}
for(i=0;i<x;i++){
revotes[i]=-1;
}
cout<<"Enter the code number of the candidate or"<<endl;
cout<<"if you want to stop voting press -1"<<endl;
cin>>num;
do{
while(flag==false){
if(num==-1){
cout<<"Moving to the next voter"<<endl;
flag=true;
}
i=0;
while(flag==false && i<x){
if(revotes[i]==num){
cout<<"I'm sorry but you have alread voted"<<endl;
cout<<"for this candidate"<<endl;
flag=true;
} else{
i++;
}
}
i=0;
while (flag==false && i<x){
if (array[i].get_P_Number()==num){
array[i].set_Points(counter);
revotes[i]=num;
counter--;
flag=true;
} else {
i++;
}
}
if (counter==0){
cout<<"You have voted all the candidates"<<endl;
flag=true;
}
if(flag==false){
cout<<"Enter the code number of the candidate or"<<endl;
cout<<"if you want to stop voting press -1"<<endl;
cin>>num;
}
}
} while(num!=-1);
revote_counter_checker=1;
}else{
cout<<"You have already voted."<<endl;
cout<<"You can't do that again."<<endl;
revote_counter_checker=0;
}
return revote_counter_checker;
}
int main()
{ int i,j,N_can,N_vot,num_for_ver,counter_for_g_flag=0;
bool flag,g_flag;
prospective *can_array;
voter *vot_array;
cout<<"Enter the number of the candidates"<<endl;
cin>>N_can;
cout<<"Enter the number of the voters"<<endl;
cin>>N_vot;
can_array=(prospective*)malloc(N_can*sizeof(prospective));
if (can_array==NULL){
free(can_array);
cout<<"Error allocating memory"<<endl;
return 1;
}
vot_array=(voter*)malloc(N_vot*sizeof(voter));
if (vot_array==NULL){
free(vot_array);
cout<<"Error allocating memory"<<endl;
return 1;
}
for(i=0; i<N_can; i++){
can_array[i]=prospective();
}
for(i=0; i<N_vot; i++){
vot_array[i]=voter();
}
while(g_flag==false){
cout<<"Enter the code number of the voter for varification"<<endl;
cin>>num_for_ver;
flag=false;
j=0;
while(j<N_vot && flag==false){
if(vot_array[j].get_V_Number()==num_for_ver){
counter_for_g_flag += vote(can_array,N_can,vot_array[j].get_V_Number());
vot_array[j].set_V_Number(0);
flag=true;
} else{
j++;
}
}
if(flag==false){
cout<<"There isn't any voter with this code"<<endl;
cout<<"or the voter with this code has already voted"<<endl;
}
if(counter_for_g_flag==N_vot){
g_flag=true;
}
}
cout<<endl;
cout<<"The list of the candidates and their points"<<endl;
for (i=0;i<N_can;i++){
cout<<can_array[i].get_P_Name()<<"\t"<<can_array[i].get_Points()<<endl;
}
free(can_array);
free(vot_array);
return 0;
}
the problem is that when i run it after a few seconds it crushes
BUT if i delete the do while here
Code:
do{
while(flag==false){
if(num==-1){
cout<<"Moving to the next voter"<<endl;
flag=true;
}
i=0;
while(flag==false && i<x){
if(revotes[i]==num){
cout<<"I'm sorry but you have alread voted"<<endl;
cout<<"for this candidate"<<endl;
flag=true;
} else{
i++;
}
}
i=0;
while (flag==false && i<x){
if (array[i].get_P_Number()==num){
array[i].set_Points(counter);
revotes[i]=num;
counter--;
flag=true;
} else {
i++;
}
}
if (counter==0){
cout<<"You have voted all the candidates"<<endl;
flag=true;
}
if(flag==false){
cout<<"Enter the code number of the candidate or"<<endl;
cout<<"if you want to stop voting press -1"<<endl;
cin>>num;
}
}
} while(num!=-1);
the program runs fine till the end
the only problem is that it does what i want to do only one time and not until num=-1
can someone please help me?
ps i know that in c++ it's best to use new and not malloc but my professor said that we have to use malloc in this project