我想要建立的是一个简单的程序,可以在选举期间计票。
该程序应首先询问候选人的数量,然后创建一个candNUMB列表。之后,程序要求指定每个候选名称,这些名称应添加到相应的列表中。例如:
candidate n1: Paul
candidate n2: Frank
candidate n3: John
现在我们有3个列表,程序应该反复询问你的投票(直到你说停止),然后将投票添加到每个列表(Paul,Frank,John)。最终程序打印结果。
Debug显示未在此范围内声明的候选[candNUMB]。我错过了什么?
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int candNUMB = 0;
int candidate[candNUMB] = {0};
cout << "I can count votes during an election. Try me." << endl << endl;
cout << "Let's start. Specify the number of candidates: ";
cin >> candNUMB;
cout << endl << "There are " << candNUMB << " candidates. Specify each name." << endl;
for (int i = 0; i < candNUMB; i++)
{
cout << "Insert candidate n*" << i + 1 << " name: ";
cin >> candidate[i];
}
return 0;
}
编辑:补充说
int candNUMB = 0;
现在程序根本无法启动。
Process Returned -107374ecc
答案 0 :(得分:-2)
试试这个让你入门
#include <iostream>
#include <string>
using namespace std;
int main()
{
int candNUMB;
string candidate[999];
当然,如果你有超过999名候选人,这将无效,但我猜你会遇到比解决问题更重要的问题。