C++ 投票程序中的计数器出现问题
我的代码有问题,计数器不断返回错误的答案(我知道候选人的名字应该是用户输入的),但我如何才能解决这个问题!(我在图片中附上了问题的规格)注意,我必须在不使用环路的情况下制作投票系统 新的计分制得到了错误的结果C++ 投票程序中的计数器出现问题,c++,function,loops,counter,C++,Function,Loops,Counter,我的代码有问题,计数器不断返回错误的答案(我知道候选人的名字应该是用户输入的),但我如何才能解决这个问题!(我在图片中附上了问题的规格)注意,我必须在不使用环路的情况下制作投票系统 新的计分制得到了错误的结果 #include <bits/stdc++.h> using namespace std; string a, b, c; int voters, counter1 = 0, counter2 = 0, counter3 = 0; void winner() { cout
#include <bits/stdc++.h>
using namespace std;
string a, b, c;
int voters, counter1 = 0, counter2 = 0, counter3 = 0;
void winner() {
cout << "adham"
<< " " << counter1 << endl;
cout << "bilal"
<< " " << counter2 << endl;
cout << "catherine"
<< " " << counter3 << endl;
if (counter1 > counter2 and counter1 > counter3) {
cout << "Adham is the winner" << endl;
} else if (counter2 > counter1 and counter2 > counter3) {
cout << "Bilal is the winner" << endl;
} else if (counter3 > counter1 and counter3 > counter2) {
cout << "catherine is the winner" << endl;
}
}
void enterCandidates() {
cout << "Names of candidates" << endl;
cin >> a >> b >> c;
}
int oldElection() {
for (int i = 0; i < voters; ++i) {
if (a == "adham") {
counter1 = counter1 + 1;
} else if (a == "bilal") {
counter2 = counter2 + 1;
} else if (a == "catherine") {
counter3 = counter3 + 1;
}
}
return 0;
}
int newElection() {
cout << "votes:" << endl;
for (int i = 0; i < voters; ++i) {
cout << "voter" << i + 1 << endl;
cin >> a;
cin >> b;
cin >> c;
if (a == "adham" and b == "bilal" and c == "catherine") {
counter1 = counter1 + 3;
counter2 = counter2 + 2;
counter3 = counter3 + 1;
} else if (a == "bilal" and b == "adham" and c == "catherine") {
counter2 = counter2 + 3;
counter1 = counter1 + 2;
counter3 = counter3 + 1;
} else if (a == "catherine" and b == "adham" and c == "bilal") {
counter3 = counter3 + 3;
counter1 = counter1 + 2;
counter2 = counter2 + 1;
} else if (a == "catherine" and b == "bilal" and c == "adham") {
counter3 = counter3 + 3;
counter2 = counter2 + 2;
counter1 = counter1 + 1;
} else if (a == "adham" and b == "catherine" and c == "bilal") {
counter1 = counter1 + 3;
counter3 = counter3 + 2;
counter2 = counter2 + 1;
} else if (a == "bilal" and b == "catherine" and c == "adham") {
counter2 = counter2 + 3;
counter3 = counter3 + 2;
counter1 = counter1 + 1;
}
}
return 0;
}
int main() {
cout << "Number of voters: " << endl;
cin >> voters;
enterCandidates();
newElection();
oldElection();
winner();
}
#包括
使用名称空间std;
字符串a、b、c;
int投票者,计数器1=0,计数器2=0,计数器3=0;
无效赢家(){
cout您的答案代码应如下所示:
#include<iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;
#include<string>
int a, b, c;
int voters, counter1 = 0, counter2 = 0, counter3 = 0;
vector <char> vote_count;
vector<char> take_vote(){
char vote;
cout<<"\nCandidates are Adam,Bilal,Catherine.Enter A,B,C according to your choice."<<endl;
for(int i=1;i<=3;i++){
cout<<"Enter candidates "<<i<<": "<<endl;
cin>>vote;
vote_count.push_back(vote);
}
return vote_count;
}
int add_point(vector<char>add){
int sum=3;
for(int i=0;i<3;i++){
sum = sum-i;
switch(add[i]){
case 'A':
a+=sum;
break;
case 'B':
b+=sum;
break;
case 'C':
c+=sum;
break;
default :
cout<<"your input in incorrect,please try again"<<endl;
break;
}
return a;
return b;
return c;
}
}
void winner(int a,int b,int c){
if(a>b && a>c){
cout<<"Adham is the winner"<<endl;
}
else if(b>a && b>c){
cout<<"Bilal is the winner"<<endl;
}
else {
cout<<"Catherine is the winner"<<endl;
}
}
int main(){
cout<<"See the winner of election:"<<endl;
int i = 0;
vector <int>point_calc;
vector<char>add;
while(i != 1){
cout<<"Enter candidates list here"<<endl;
vector<char>add;
add = take_vote();
add_point(add);
cout<<"If want to break;press 1 otherwise press 0:"<<endl;
cin>>i;
}
winner(a,b,c);
return 0;
}
#包括
#包括
#包括
使用名称空间std;
#包括
INTA、b、c;
int投票者,计数器1=0,计数器2=0,计数器3=0;
向量投票计数;
向量take_vote(){
字符投票;
您可能希望使用调试器逐行检查代码,以了解它在做什么。听起来您可能需要学习如何使用调试器逐行检查代码。有了一个好的调试器,您可以逐行执行程序,并查看程序偏离预期的位置。如果需要,这是一个必不可少的工具继续阅读:和