C 打印出isn';当我输入值退出程序时不工作

C 打印出isn';当我输入值退出程序时不工作,c,C,我仍然是C编程新手,需要弄清楚为什么当我输入“C”选项时,程序没有打印出程序中输入的分数。我没有看到我丢失的东西,如果有人看到我丢失的东西,能告诉我吗 #include <stdio.h> #include <stdlib.h> int main() { //Add all of the variables and the array for the grades I want to enter. char choice; int gradeS

我仍然是C编程新手,需要弄清楚为什么当我输入“C”选项时,程序没有打印出程序中输入的分数。我没有看到我丢失的东西,如果有人看到我丢失的东西,能告诉我吗

#include <stdio.h>
#include <stdlib.h>


int main()
{
    //Add all of the variables and the array for the grades I want to enter.
    char choice;
    int gradeScore = 0;//percentage
    //int gradeArray[100];//percentArrray //Comment Out
    int gCount = 0,i;//count

    //Allocate dynamic memory point using gradeArray.
    int *gradeArray = (int*)malloc(sizeof(int));

    /*The for loop is set to enable the user to enter no more than 100 grades.  This is because the gradeArray variable
    limit is set to 100.  This will then loop through until the user has entered up to 100 grades to ensure there
    is no buffering issue.*/

    for (gCount = 0; gCount < 100;)

        {
        /*This prompts the user for a choice that enables them to either enter another grade or exit the program and
        print the grades.  It also reads the choice entered by the user.*/
        printf("******************Enter Choice Selection in Parenthesis******************");
        printf("\n\n To add grades, enter choice (a)");
        printf("\n When finished entering grades, enter choice (c) \n \nEnter Choice: ");
        scanf(" %c", &choice);  //space is entered to ensure the compiler does not read whitespaces

        /* Then I use an if with the condition set to a valid choice of 'a'.  Then I prompt the user
        to enter a grade, read it and move on to the next if statement.*/
        if(choice == 'a')
        {
            printf("\nEnter grade: ");
            scanf(" %d", &gradeScore);

            /*If the grade entered does meet the if condition statement below it is added to the gCount
            of grades entered. This will allow all of the grades entered to be printed with the exit condition.*/
            if(gradeScore <= 100 && gradeScore >= 0)
            {
                gradeArray = realloc(gradeArray, sizeof(int) * gCount);
            }

        }


        //The last if statement prints out each grade on a new line when the user choice is c.
        if(choice == 'c')
        {
            break;
        }
    }
    printf("Grades are:\n");
    for(i = 0; i < gCount ; i++)
    {
        printf("  %d\%%\n", gradeArray[i]);

    }
    free(gradeArray);
    return 0;
}
#包括
#包括
int main()
{
//为我要输入的成绩添加所有变量和数组。
字符选择;
int gradeScore=0;//百分比
//int gradeArray[100];//百分比数组//注释掉
int gCount=0,i;//计数
//使用gradeArray分配动态内存点。
int*gradeArray=(int*)malloc(sizeof(int));
/*for循环设置为允许用户输入不超过100个等级。这是因为GRADESARRAY变量
限制设置为100。然后,这将循环执行,直到用户输入多达100个等级,以确保存在
没有缓冲问题*/
对于(gCount=0;gCount<100;)
{
/*这将提示用户进行选择,使他们能够输入另一个等级或退出程序并
打印成绩。它还读取用户输入的选项*/

printf(“*******************在括号中输入选项选择*******************”; printf(“\n\n要添加等级,请输入选项(a)”); printf(“\n输入成绩后,输入选项(c)\n\n输入选项:”); scanf(“%c”,&choice);//输入空格以确保编译器不读取空格 /*然后使用条件设置为有效选项“a”的if。然后提示用户 要输入等级,请阅读该等级,然后转到下一个if语句*/ 如果(选项=='a') { printf(“\n输入等级:”); scanf(“%d”、&gradeScore); /*如果输入的等级不符合下面的If条件语句,则将其添加到gCount中 输入的分数。这将允许使用退出条件打印所有输入的分数*/ 如果(成绩分数=0) { gradeArray=realloc(gradeArray,sizeof(int)*gCount); } } //当用户选择c时,最后一个if语句将在新行上打印每个等级。 如果(选项=='c') { 打破 } } printf(“等级为:\n”); 对于(i=0;i
谢谢,,
Annette

根据您的程序,您在for循环内部使用了错误的变量。您正在将for循环中的gCount重新初始化为0,而不是递增它。稍后,您将使用相同的gCount打印成绩。但由于它的值是0,所以不打印分数。

问题是,您从未增加
gCount
变量的值,并且
for()
循环也错误,我建议在这里使用
while()
,而且您也从未将gradeScore添加到gradeArray。你可以这样写:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //Add all of the variables and the array for the grades I want to enter.
    char choice;
    int gradeScore = 0;//percentage
    //int gradeArray[100];//percentArrray //Comment Out
    int gCount = 0,i;//count
 
    //Allocate dynamic memory point using gradeArray.
    int arr_size = 20;
    int *gradeArray = malloc(sizeof(int)*arr_size);

    /*The for loop is set to enable the user to enter no more than 100 grades.  This is because the gradeArray variable
    limit is set to 100.  This will then loop through until the user has entered up to 100 grades to ensure there
    is no buffering issue.*/
    
    while(gCount < 100)
    {
        /*This prompts the user for a choice that enables them to either enter another grade or exit the program and
        print the grades.  It also reads the choice entered by the user.*/
        printf("******************Enter Choice Selection in Parenthesis******************");
        printf("\n\n To add grades, enter choice (a)");
        printf("\n When finished entering grades, enter choice (c) \n \nEnter Choice: ");
        scanf(" %c", &choice);  //space is entered to ensure the compiler does not read whitespaces

        /* Then I use an if with the condition set to a valid choice of 'a'.  Then I prompt the user
        to enter a grade, read it and move on to the next if statement.*/
        if(choice == 'a')
        {
            printf("\nEnter grade: ");
            scanf(" %d", &gradeArray[gCount]);
            
            /*If the grade entered does meet the if condition statement below it is added to the gCount
            of grades entered. This will allow all of the grades entered to be printed with the exit condition.*/
            if((gCount+1) == arr_size)
            {
                arr_size += 20;
                gradeArray = realloc(gradeArray, sizeof(int) * arr_size);
            }
            
            gCount++;
        }


        //The last if statement prints out each grade on a new line when the user choice is c.
        if(choice == 'c')
        {
            break;
        }
    }
    
    printf("Grades are:\n");
    for(i = 0; i < gCount ; i++)
    {
        printf("  %d\%%\n", gradeArray[i]);

    }
    
    free(gradeArray);
    return 0;
}
#包括
#包括
int main()
{
//为我要输入的成绩添加所有变量和数组。
字符选择;
int gradeScore=0;//百分比
//int gradeArray[100];//百分比数组//注释掉
int gCount=0,i;//计数
//使用gradeArray分配动态内存点。
int arr_尺寸=20;
int*gradeArray=malloc(sizeof(int)*arr_size);
/*for循环设置为允许用户输入不超过100个等级。这是因为GRADESARRAY变量
限制设置为100。然后,这将循环执行,直到用户输入多达100个等级,以确保存在
没有缓冲问题*/
而(gCount<100)
{
/*这将提示用户进行选择,使他们能够输入另一个等级或退出程序并
打印成绩。它还读取用户输入的选项*/

printf(“*******************在括号中输入选项选择*******************”; printf(“\n\n要添加等级,请输入选项(a)”); printf(“\n输入成绩后,输入选项(c)\n\n输入选项:”); scanf(“%c”,&choice);//输入空格以确保编译器不读取空格 /*然后使用条件设置为有效选项“a”的if。然后提示用户 要输入等级,请阅读该等级,然后转到下一个if语句*/ 如果(选项=='a') { printf(“\n输入等级:”); scanf(“%d”、&gradeArray[gCount]); /*如果输入的等级不符合下面的If条件语句,则将其添加到gCount中 输入的分数。这将允许使用退出条件打印所有输入的分数*/ 如果((gCount+1)=阵列大小) { arr_尺寸+=20; gradeArray=realloc(gradeArray,sizeof(int)*arr_size); } gCount++; } //当用户选择c时,最后一个if语句将在新行上打印每个等级。 如果(选项=='c') { 打破 } } printf(“等级为:\n”); 对于(i=0;i
另外,我想指出的是,您不应该只分配一个元素的数组,然后不断地重新分配它。这是一种糟糕的做法,耗时,并且可能会在以后导致一些更大的问题


我建议你检查一下malloc的铸造。

你正在制作一个数组,里面有一个
int
?为什么不从一些合理的默认值开始,并在达到该限制后调整~1.5倍的大小?当gcount==0时,
realloc(gradeArray,sizeof(int)*gcount)
是一个问题。这看起来像是一个一个接一个的错误。您能提供准确的输入和结果吗?请注意
printf(“%d\%%\n”,gradeArray[i])应为
printf(“%1”)