C++ 计算错误,不显示百分比
C++ wrong calculation and no percentage showing
只有a和c计算正确,b计算错误,c差1。而且百分比只显示0。虽然公式是一样的,但我不明白为什么这两个是错误的,为什么百分比只显示零并且不符合我的公式。
这里是输出
enter image description here
这是我的代码
#include <iostream>
using namespace std;
int main(){
string candidate[4];
int a[5], b[5], c[5], d[5];//precint
int at,bt,ct,dt;
int ap, bp, cp, dp;
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\t"<<candidate[0]<<"\t\t"<<candidate[1]<<"\t\t"<<candidate[2]<<"\t\t"<<candidate[3]<<endl;
for(int i=0; i<5; i++){ //for displaying the tally
cout<<i+1<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<"\t\t"<<c[i]<<"\t\t"<<d[i]<<endl;
}
cout<<endl;
for(int i=0; i<5; i++){ //for the total votes
at=at+a[i];
bt=bt+b[i];
ct=ct+c[i];
dt=dt+d[i];
}
total=at+bt+ct+dt; //for the percent
ap=(at/total)*100;
bp=(bt/total)*100;
cp=(ct/total)*100;
dp=(dt/total)*100;
cout<<"Candidate "<<candidate[0]<<" total votes and percentage: "<<at<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[1]<<" total votes and percentage: "<<bt<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[2]<<" total votes and percentage: "<<ct<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[3]<<" total votes and percentage: "<<dt<<" votes. "<<ap<<"%"<<endl;
break;
}
cout<<"Do you want to continue(Y/N): ";
cin>>yesNo;
}while (yesNo == 'y' || yesNo == 'Y');
cout<<"Thank You!";
return 0;
}
使用 double 而不是 int 来表示用于存储百分比的变量。
初始化用于存储总计的变量。
您可以在此处找到有关双重转换的更多信息。 How to convert integer to double implicitly?
并且您正在为所有候选人打印相同的百分比值,即 ap。
int at = 0, bt = 0, ct = 0, dt = 0;
double ap, bp, cp, dp;
//Rest of the code
ap = ((double)at / total) * 100;
bp = ((double)bt / total) * 100;
cp = ((double)ct / total) * 100;
dp = ((double)dt / total) * 100;
只有a和c计算正确,b计算错误,c差1。而且百分比只显示0。虽然公式是一样的,但我不明白为什么这两个是错误的,为什么百分比只显示零并且不符合我的公式。
这里是输出 enter image description here
这是我的代码
#include <iostream>
using namespace std;
int main(){
string candidate[4];
int a[5], b[5], c[5], d[5];//precint
int at,bt,ct,dt;
int ap, bp, cp, dp;
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\t"<<candidate[0]<<"\t\t"<<candidate[1]<<"\t\t"<<candidate[2]<<"\t\t"<<candidate[3]<<endl;
for(int i=0; i<5; i++){ //for displaying the tally
cout<<i+1<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<"\t\t"<<c[i]<<"\t\t"<<d[i]<<endl;
}
cout<<endl;
for(int i=0; i<5; i++){ //for the total votes
at=at+a[i];
bt=bt+b[i];
ct=ct+c[i];
dt=dt+d[i];
}
total=at+bt+ct+dt; //for the percent
ap=(at/total)*100;
bp=(bt/total)*100;
cp=(ct/total)*100;
dp=(dt/total)*100;
cout<<"Candidate "<<candidate[0]<<" total votes and percentage: "<<at<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[1]<<" total votes and percentage: "<<bt<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[2]<<" total votes and percentage: "<<ct<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[3]<<" total votes and percentage: "<<dt<<" votes. "<<ap<<"%"<<endl;
break;
}
cout<<"Do you want to continue(Y/N): ";
cin>>yesNo;
}while (yesNo == 'y' || yesNo == 'Y');
cout<<"Thank You!";
return 0;
}
使用 double 而不是 int 来表示用于存储百分比的变量。 初始化用于存储总计的变量。
您可以在此处找到有关双重转换的更多信息。 How to convert integer to double implicitly?
并且您正在为所有候选人打印相同的百分比值,即 ap。
int at = 0, bt = 0, ct = 0, dt = 0;
double ap, bp, cp, dp;
//Rest of the code
ap = ((double)at / total) * 100;
bp = ((double)bt / total) * 100;
cp = ((double)ct / total) * 100;
dp = ((double)dt / total) * 100;