Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My problem is that I have a program with global variables but I need to find a w

ID: 3633408 • Letter: M

Question

My problem is that I have a program with global variables but I need to find a way where its not using global variables.i think i need to either rewrite the definitons or maybe could i pass the struct using pointers??? or do i need to destruct my structure?? Im stuck in quick sand
but here is the program:

#include <iostream>
#include <iomanip>
using namespace std;
void addNodes();
void displayList();
struct vote

{
char candidate[7];
int votes;
double percent;
vote *votePtr;
};
vote *startPtr = NULL;
vote *current;
int i,j;
int main()
{
vote *startPtr = NULL;
cout<<fixed<<setprecision(0);
cout<<"Enter the number of candidates: ";
cin>>j;
addNodes();
displayList();
return 0;
}
void addNodes() {
vote *temp, *temp2;
for(i=0;i<j;i++)
{
system("cls");
temp = new vote;
cout<<"Enter the candidate's name: ";
cin>>temp->candidate;
cout<<"Enter the votes received: ";
cin>>temp->votes;
cout<<"Enter the percentage: ";
cin>>temp->percent;
temp->votePtr = NULL;
if (startPtr == NULL)
{
startPtr = temp;
current = startPtr;
}
else
{
temp2 = startPtr;
while(temp2->votePtr != NULL)
temp2=temp2->votePtr;
temp2->votePtr = temp;
}
}
return;
}
void displayList() {
vote *temp;
temp = startPtr; system("cls");
while(temp!=NULL) {
cout<<" Candidate: "<<temp->candidate<<endl;
cout<<"Votes Received: "<<temp->votes<<endl;
cout<<" Percentage: "<<temp->percent<<"%"<<endl; temp = temp->votePtr;
cout<<endl;
}
return ;
}

Explanation / Answer

#include <iostream>
#include <iomanip>
using namespace std;

struct vote
{
   char candidate[7];
   int votes;
   double percent;
   vote *votePtr;
};

vote* addNodes(int, int);
void displayList(vote*);

int main()
{
   vote* startPtr=NULL;

   int i=0,j=0;
   cout<<fixed;
   cout<<"Enter the number of candidates: ";
   cin>>j;
   startPtr = addNodes( i, j);
   displayList(startPtr);
   return 0;
}
vote* addNodes(int i, int j)
{
   vote* startPtr=NULL;
   vote* current=NULL;
   vote *temp, *temp2;
   for(i=0;i<j;i++)
   {
       system("cls");
       temp = new vote;
       cout<<"Enter the candidate's name: ";
       cin>>temp->candidate;
       cout<<"Enter the votes received: ";
       cin>>temp->votes;
       cout<<"Enter the percentage: ";
       cin>>temp->percent;
       temp->votePtr = NULL;
        cout<<"Test1";
       if (startPtr == NULL)
       {
           startPtr = temp;
           current = startPtr;
       }
       else
       {
           temp2 = startPtr;
           while(temp2->votePtr != NULL)
               temp2=temp2->votePtr;
           temp2->votePtr = temp;
       }
   }
   return startPtr;
}

void displayList(vote* startPtr)
{
   vote *temp;
   temp = startPtr;
   system("cls");
   while(temp!=NULL)
   {
       cout<<" Candidate: "<<temp->candidate<<endl;
       cout<<"Votes Received: "<<temp->votes<<endl;
       cout<<" Percentage: "<<temp->percent<<"%"<<endl; temp = temp->votePtr;
       cout<<endl;
   }
   return ;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote