对于家庭作业,我必须编写一个读入人物的代码" ID"并将他们的投票添加到四个选项之一。我认为我写的是代码,但每当我在代码块中运行它时它会崩溃,我不知道为什么。 这就是我写的:
#include<stdio.h>
int main (){
//create array for IDs to see if number was repeated?
int number;
int ID[number];
//start counts for each voting option
int Acount=0;
int Bcount=0;
int Ccount=0;
int Dcount=0;
char choice;
while(number != -1){
printf("What is your ID?\n");
//read in size of array
int i;
for (i=0; i<number; i++){
scanf("%d",&number);
for (i=0;i<number;i++){
ID[number]=1;
printf("You have already voted. You cannot vote again.");
continue;
//make array to look for repeating IDs?
//scan that array to see if number is repeated
continue;
}
printf("Welcome %d, which vote would you like to place?\n", number);
//if valid char is said add +1 to char count
scanf("%c", &choice);
if(choice == 'A'){
Acount + 1;
printf("You have successfully voted for A.\n");
}
else if(choice == 'B'){
Bcount + 1;
printf("You have successfully voted for B.\n");
}
else if(choice == 'C'){
Ccount + 1;
printf("You have successfully voted for C.\n");
}
else if(choice == 'D'){
Dcount + 1;
printf("You have successfully voted for D.\n");
}
continue;
}
}
// -1 must have been entered so
//print winner and end program
if(Acount>Bcount && Acount>Ccount && Acount>Dcount){
printf("A wins with %d votes!\n", Acount);
}
else if(Bcount>Acount && Bcount>Ccount && Bcount>Dcount){
printf("B wins with %d votes!\n", Bcount);
}
else if(Ccount>Acount && Ccount>Bcount && Ccount>Dcount){
printf("C wins with %d votes!\n", Ccount);
}
else if(Dcount>Acount && Dcount>Bcount && Dcount>Ccount){
printf("D wins with %d votes!\n", Dcount);
}
return 0;
}
答案 0 :(得分:0)
号码未定义。你有两种方法。定义一个宏:
#define NUMBER 20 /* using capitals is better */
int ID[NUMBER];
或者从stdin获取数字:
#include <stdio.h>
int number;
scanf("%d",&number);