2

My code doesn't display array values that I input, instead it only prints zero. It supposed to print the values if I choose case 2 after completing the case 1. My code doesn't display array values that I input, instead it only prints zero. It supposed to print the values if I choose case 2 after completing the case 1.

#include <iostream>
using namespace std; 

int main(){
    string candidate[4];
    int a[5], b[5], c[5], d[5];//precint
    int total;
    int choice;
    char yesNo;
    int i;
    
    do{ 
    cout<<"[1] Enter candidates names and votes"<<endl;
    cout<<"[2] Show Tally"<<endl;
    cout<<"[3] Exit"<<endl;
    cout<<"Enter a choice: ";
    cin>>choice;
    switch(choice){
        case(1):
    
        for(int i=0; i<4; i++){
        cout<<"Enter candidate names: ";
        cin>>candidate[i];}
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[0]<<" precinct "<<i+1<<" votes: ";
        cin>>a[i];
        }
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[1]<<" precinct "<<i+1<<" votes: ";
        cin>>b[i];
        }
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[2]<<" precinct "<<i+1<<" votes: ";
        cin>>c[i];
        }
        
        for(int i=0; i<5; i++){
        cout<<"Candidate "<<candidate[3]<<" precinct "<<i+1<<" votes: ";
        cin>>d[i];
        }
        
        break;
        case(2):
            cout<<"Precinct\tCandidate: "<<candidate[0]<<"\tCandidate: "<<candidate[1]<<"\tCandidate: "<<candidate[2]<<"\tCandidate: "<<candidate[3]<<endl;
            for(int j=1; j<6;j++)
                cout<<j<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<"\t\t"<<c[i]<<"\t\t"<<d[i]<<endl;
        break;
        }
        cout<<"Do you want to continue(Y/N): ";
        cin>>yesNo;
    }while (yesNo == 'y' || yesNo == 'Y');
        cout<<"Thank You!";
    

    
    
    return 0;
}
2
  • Read your compiler warnings. i is uninitialized when you print a/b/c/d Commented Jun 4, 2022 at 10:09
  • if i initialized it in a for loop it display the values but 5 times each, if i initialized it normally it still doesn't display the values, still 0. Commented Jun 4, 2022 at 10:15

3 Answers 3

1

There is a mismatch between the variable used in the for loop vs variable used to print the values.

case(2):
    cout<<"Precinct\tCandidate: "<<candidate[0]<<"\tCandidate: "<<candidate[1]<<"\tCandidate: "<<candidate[2]<<"\tCandidate: "<<candidate[3]<<endl;
    
    for(int i=0; i<5;i++)
        cout << i+1 << "\t\t" << a[i] << "\t\t" << b[i] << "\t\t" << c[i] << "\t\t"<< d[i] << endl;
Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem with the i. before this line:

 for(int i=0; i<5;i++)
            {
            cout<<(i+1)<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<"\t\t"<<c[i]<<"\t\t"<<d[i] 
           <<endl;
            }
 


[1] Enter candidates names and votes
[2] Show Tally
[3] Exit
Enter a choice: 1
Enter candidate names: A
Enter candidate names: B
Enter candidate names: C
Enter candidate names: D
.......
Do you want to continue(Y/N): Y
[1] Enter candidates names and votes
[2] Show Tally
[3] Exit
Enter a choice: 2
Precinct    Candidate: A    Candidate: B    Candidate: C    Candidate: D
1       5       7       4       54
2       4       56      15      85
3       5       1       9       15
4       1       23      4       8
5       6       522     2       5
Do you want to continue(Y/N): 

Comments

0

The Error is only in case 2, just check you are using variable j in the loop and I in printing the values which is wrong. Just replace the case 2 with following code and your problem is solved

 case(2):
            cout<<"Precinct\tCandidate: "<<candidate[0]<<"\tCandidate: "<<candidate[1]<<"\tCandidate: "<<candidate[2]<<"\tCandidate: "<<candidate[3]<<endl;
            for(int i=0; i<5;i++)
                cout<<i<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<"\t\t"<<c[i]<<"\t\t"<<d[i]<<endl;
        break;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.