C++ 不显示数组值
C++ doesn't display array values
我的代码不显示我输入的数组值,而是只打印零。如果我在完成案例 1 后选择案例 2,它应该打印值。我的代码不显示我输入的数组值,而是只打印零。如果我在完成案例 1 后选择案例 2,它应该会打印值。
#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;
}
for 循环中使用的变量与用于打印值的变量不匹配。
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;
我认为是 i 的问题。
在此行之前:
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):
错误仅出现在情况 2 中,只需检查您在循环中使用变量 j 并在打印错误值时使用 I。只需将案例 2 替换为以下代码即可解决您的问题
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;
}
我的代码不显示我输入的数组值,而是只打印零。如果我在完成案例 1 后选择案例 2,它应该打印值。我的代码不显示我输入的数组值,而是只打印零。如果我在完成案例 1 后选择案例 2,它应该会打印值。
#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;
}
for 循环中使用的变量与用于打印值的变量不匹配。
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;
我认为是 i 的问题。 在此行之前:
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):
错误仅出现在情况 2 中,只需检查您在循环中使用变量 j 并在打印错误值时使用 I。只需将案例 2 替换为以下代码即可解决您的问题
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;
}