c - 程序崩溃

标签 c arrays

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

相关文章:

python - 如何在 numpy 中进行 3 向外积?

c - 从函数返回的字符串数组未按预期工作

c - 通过fifo向服务器发送确认消息来区分客户端,客户端不会收到后续消息

python - 有效地确定大型排序的 numpy 数组是否只有唯一值

C 指向函数内部结构数组的指针/引用

java - 递归地打印出数组中等于给定总和的所有子集

php - 在查询结果中将不同的数组合并为一个数组

c - 从指针数组中查找重复项

检查 char array[] 是否包含 int、float 或 double 值并将值存储到相应的数据类型

c - "if(n/10)"在C中是什么意思?