将结构数组传递给读取其成员的函数 [英] Passing array of structs to function that reads its members

查看:96
本文介绍了将结构数组传递给读取其成员的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

** 定义一个表示学生数据的结构,包括GPA,学生ID和学生姓名。

** 写一个读取N个学生数据的函数。

我的问题是:程序不打印名称而是打印(垃圾)和(-1。#QUANO)

它也只读取数组的第一个元素。



我尝试过:



** Define a structure to represent a student data including
the GPA, student ID, and student name.
** Write a function that reads the data of N students.
My problem is: the program doesn't print the name instead it prints(garbage)and(-1.#QUANO)
also it reads the first element of array only.

What I have tried:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct
{
    float GPA;
    int ID;
    char name[10];
} student;
void read(student arr[],int n)//function that reads student data
{
    int i;
    for(i=0; i<n; i++)
    {
        gets(arr[i].name);
        scanf("%d",&arr[i].ID);
        scanf("%f",&arr[i].GPA);
    }
    return;
}
int main()
{
    int n,i;
    printf("enter number of students:\n");
    scanf("%d",&n);
    student array[n];//array of structs of size n(number of students)
    read(array,n);
    for(i=0; i<n; i++)
    {
        puts(array[i].name);
        printf("/n%d",array[i].ID);
        printf("/n%.2f",array[i].GPA);
    }
    return 0;
}

推荐答案

Quote:

我找不到出错的原因因为它汇编得很好!

I can't find what went wrong cause it compiled well!



'猫在天空飞得很高'是正确的英文单词和正确的英语语法,但是没有意义。 />
程序是一样的,正确的语法是不够的。


'the cat flies high in the sky' is correct English word and correct English grammar, but is meaningless.
It is the same with programs, correct syntax is not enough.

引用:

用户输入的数据未正确存储在数组[]

the data entered by the user is not properly stored in array[]



这对我们来说几乎没有信息。

我有一个DIY解决方案,使用调试器看看你的代码如何表现以及它的真实作用,它将帮助你找出问题的原因和原因。


有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


This is almost not informative to us.
I have a DIY solution, use the debugger and see how your code behave and what it really does, it will help you to spot where is the problem and why.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


从不混合 scanf 获取调用输入。



获取 - C ++参考 [ ^ ]将读取(包括)新行字符,但不会将新行存储在缓冲区中。



scanf - 非C ++参考 [ ^ ] -character格式忽略前导空格但在读取请求类型后停止,让输入缓冲区中的新行。



您的第一个输入处理是

Never mix scanf and gets calls for input.

gets - C++ Reference[^] will read until (and inclusive) a new line character but does not store the new line in the buffer.

scanf - C++ Reference[^] for non-character formats ignores leading white spaces but stops after reading the requested type letting the new line in the input buffer.

Your first input handling is
scanf("%d",&n);

这将在按RETURN键时读取数字,但会将新行字符放入输入缓冲区。下一个输入处理调用是

This will read the number when pressing the RETURN key but will let the new line character in the input buffer. The next input handling call is

gets(arr[i].name);

因为输入缓冲区中仍然有新行字符,将被读取并且 gets()立即返回使传递的缓冲区为空字符串!



所以要么替换获取()调用

Because there is still the new line character in the input buffer, that will be read and gets() returns immediately making the passed buffer an empty string!

So either replace the gets() call with

scanf("%s", arr[i].name);



使用 get()所有输入:

or
use gets() for all inputs:

char buf[32];
gets(buf);
n = atoi(buf);
/* ... */
arr[i].ID = atoi(buf);
arr[i].GPA = atof(buf);



另请注意打印语句中的正斜杠错误。它应该是(跟随新行):


Note also the wrong forward slash in your print statements. It should be probably (with trailing new lines):

printf("%d\n",array[i].ID);
printf("%f\n",array[i].GPA);


这篇关于将结构数组传递给读取其成员的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆