Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ doesn't display array values

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading