gpt4 book ai didi

c - 程序崩溃

转载 作者:行者123 更新时间:2023-11-30 21:29:44 25 4
gpt4 key购买 nike

对于家庭作业,我必须编写一个代码来读取一个人的“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;

}

最佳答案

数字未定义。你有两种方法。定义宏:

#define NUMBER 20 /* using capitals is better */
int ID[NUMBER];

或者从标准输入获取号码:

#include <stdio.h>
int number;
scanf("%d",&number);

关于c - 程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29291607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com