Home > Back-end >  C doesn't display array values
C doesn't display array values

Time:06-05

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

CodePudding user response:

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;

CodePudding user response:

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): 

CodePudding user response:

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;
        }
  •  Tags:  
  • c
  • Related