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

my code i have so far: #include using namespace std; int main() { int tally1 = 0

ID: 3820564 • Letter: M

Question

my code i have so far:

#include

using namespace std;

int main() {
  
   int tally1 = 0, tally2 = 0, tally3 = 0, tally4 = 0, x;
  
   do {
  
   cout << "2020 EVOTE SYSTEM" << endl << endl;
   cout << "Electronic Voting System" << endl << endl;
  
   cout << "1) Hillary Clinton" << endl;
   cout << "2) Donald Trump" << endl;
   cout << "3) Michelle Obama" << endl;
   cout << "4) Ivanka Trump" << endl;
  
   cout << "Cast your ballot for? (1,2,3, or 4) " << endl;
   cin >> x;
  
   if (x == 1) {
       tally1++;
   }
   else if(x == 2) {
       tally2++;
   }
   else if(x == 3) {
       tally3++;
   }
   else if(x == 4) {
       tally4++;
   }  
   else if(x==5){
      
       cout << "Hilary clinton has " << tally1 << endl;
       cout << "Donald Trump has " << tally2 << endl;
       cout << "Michelle Obama has " << tally3 << endl;
       cout << "Ivanka Trump has " << tally4 << endl;
  
       }
   else{
       cout << "*******ERROR*******";}
  
   }
while(x >= 1 || x<=5 );
}

QUESTION: How do i make it print out the voting/tally when the user inputs 5 and when the user inputs <1 or >5 make it says ERROR?

my code above works but it keeps looping how do i make it stop? when user puts 5 and <1 or >5

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below

#include <iostream>
using namespace std;
int main() {
  
int tally1 = 0, tally2 = 0, tally3 = 0, tally4 = 0, x;
  
do {
  
cout << "2020 EVOTE SYSTEM" << endl << endl;
cout << "Electronic Voting System" << endl << endl;
  
cout << "1) Hillary Clinton" << endl;
cout << "2) Donald Trump" << endl;
cout << "3) Michelle Obama" << endl;
cout << "4) Ivanka Trump" << endl;
  
cout << "Cast your ballot for? (1,2,3, or 4) " << endl;
cin >> x;
  
if (x == 1) {
tally1++;
}
else if(x == 2) {
tally2++;
}
else if(x == 3) {
tally3++;
}
else if(x == 4) {
tally4++;
}
else if(x==5){
  
cout << "Hilary clinton has " << tally1 << endl;
cout << "Donald Trump has " << tally2 << endl;
cout << "Michelle Obama has " << tally3 << endl;
cout << "Ivanka Trump has " << tally4 << endl;
  
}
else{
cout << "*******ERROR*******"<<endl;}
  
}
while(x >= 1 && x<=5 );
}

oUTPUT:

Electronic Voting System                                                                                                                                                                                                                                                 

                                                                                                                                                                                                                                                                         

1) Hillary Clinton                                                                                                                                                                                                                                                       

2) Donald Trump                                                                                                                                                                                                                                                          

3) Michelle Obama                                                                                                                                                                                                                                                        

4) Ivanka Trump                                                                                                                                                                                                                                                          

Cast your ballot for? (1,2,3, or 4)                                                                                                                                                                                                                                      

1                                                                                                                                                                                                                                                                        

2020 EVOTE SYSTEM                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

Electronic Voting System                                                                                                                                                                                                                                                 

                                                                                                                                                                                                                                                                         

1) Hillary Clinton                                                                                                                                                                                                                                                       

2) Donald Trump                                                                                                                                                                                                                                                          

3) Michelle Obama                                                                                                                                                                                                                                                        

4) Ivanka Trump                                                                                                                                                                                                                                                          

Cast your ballot for? (1,2,3, or 4)                                                                                                                                                                                                                                      

2                                                                                                                                                                                                                                                                        

2020 EVOTE SYSTEM                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

Electronic Voting System

1) Hillary Clinton

2) Donald Trump   

3) Michelle Obama

4) Ivanka Trump   

Cast your ballot for? (1,2,3, or 4)   

5   

Hilary clinton has 1   

Donald Trump has 1

Michelle Obama has 0

Ivanka Trump has 0

2020 EVOTE SYSTEM

Electronic Voting System

1) Hillary Clinton

2) Donald Trump   

3) Michelle Obama

4) Ivanka Trump   

Cast your ballot for? (1,2,3, or 4)   

6

*******ERROR*******