Click here to Skip to main content
13,829,868 members
Rate this:
Please Sign up or sign in to vote.
See more:
** 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++)
int main()
    int n,i;
    printf("enter number of students:\n");
    student array[n];//array of structs of size n(number of students)
    for(i=0; i<n; i++)
    return 0;
Updated 17-Nov-17 0:52am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[]

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.
Member 13476370 17-Nov-17 5:45am
I already know the concept of a "debugger" .. surely I posted my question AFTER the debugger didn't help a lot .. btw the debugger doesn't help you find where does the logic goes wrong at EVERY program ..
Patrice T 17-Nov-17 5:52am
You didn't tell that you used the debugger.
Are you sure you used the debugger correctly?
As it show you what the program does, you should be able to spot where it start to go wrong.
Member 13476370 17-Nov-17 6:07am
It's a basic elementary step to use it :D
although your solution to EVERY problem here is the same"debugger"
, thnx anyway

Patrice T 17-Nov-17 6:39am
The debugger is the solution I use.
If I had used the debugger on your code, I would have spoted more or less what Jochen have reported in solution 2.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
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
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);
use gets() for all inputs:
char buf[32];
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):

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190114.1 | Last Updated 17 Nov 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100