小编Kry*_*ick的帖子

访问struct数组元素

该程序应该捕获用户输入到结构中的输入,然后打印出给定信息的直方图.到目前为止一切正常,除了当我尝试打印直方图时,无论学生的成绩如何,所有'*'字符都属于F级.我认为正在发生的是学生的数组索引被传递而不是实际变量本身,但我很困惑,因为在直方图打印之前的输出中它显示正确的值.有什么建议?

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 28
#define MAXGRADE 100

struct studentData{
    char studentID[MAXSIZE];
    char studentName[MAXSIZE];
    int examPercent;
} studentRecords[MAXSIZE];

// function prototype for histogram
void displayHist(struct studentData *records, int classSize);

int main()
{
    int i, students = -1;
    //struct studentData *studentRecords[MAXSIZE];
    while(students < 0 || students > MAXSIZE)
    {
        printf("Please enter the number of students in your class:\n");
        scanf("%d", &students);

        if(students > MAXSIZE || students <= 0)
        {
            printf("Try again..\n");
            scanf("%d", &students);
        }

    }

 for(i=0;i<students;i++) {

        printf("Please enter …
Run Code Online (Sandbox Code Playgroud)

c arrays struct

5
推荐指数
1
解决办法
8598
查看次数

创建atoi功能

我正在尝试创建自己的atoi功能.使用以下内容我得到的返回值为0.无论我改变函数中的数字变量,我得到的是返回值.有关修改代码的任何建议吗?

//my atoi function
int atoi_me(char *numstring)
{
    int number = 0;
    while((*numstring >= '0') && (*numstring <= '9'))
    {
        number = (number * 10) + (*numstring - '0');
        numstring++;
    }

    return number;
}

int main()
{
    char *number[MAXSIZE];
    int num;

    printf("Please enter a number:\n");
    scanf("%c", &number);
    num = atoi_me(*number);
    printf("%d", num);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c function atoi

4
推荐指数
1
解决办法
2746
查看次数

fscanf()读错误

我有一个文本文件,我在第一个和最后的名字读取大小为20的字符数组.我正在创建一个结构数组,其中包含"人民"的信息.文本文件如下所示:

John Robbins
Teresa Jones 
Run Code Online (Sandbox Code Playgroud)

我的结构定义如下:

struct people {

char name[20];
};
Run Code Online (Sandbox Code Playgroud)

人员宣言结构:

 struct people *persons[2];
Run Code Online (Sandbox Code Playgroud)

声明新结构后,我使用以下代码读取名称:

for(i=0; i<2; i++)
{  
  fscanf(dp, "%[^\n]s", &persons[i].name[20]);
}
Run Code Online (Sandbox Code Playgroud)

但是,一旦我将名称输出到控制台,我收到以下内容:

hn Robbins
sa Jones
Run Code Online (Sandbox Code Playgroud)

我做了大量研究,找不到解决这个问题的方法.有没有人遇到过这个问题?

c file-io scanf

0
推荐指数
1
解决办法
148
查看次数

标签 统计

c ×3

arrays ×1

atoi ×1

file-io ×1

function ×1

scanf ×1

struct ×1