Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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++)
    {
        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;
}
Posted
Updated 16-Nov-17 23:52pm
v3

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.
Quote:
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.
 
Share this answer
 
Comments
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.
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);
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);
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
C++
scanf("%s", arr[i].name);
or
use gets() for all inputs:
C++
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);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900