473,387 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

using Struct-student assignment

I learned how to use struct when I try to write a program i face some misstakes which I cant correct

This is the program:
-------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. main()
  5. {int nu,i;  /* nu is number of student */
  6.  
  7. struct record{    char id[8];
  8.         char name[35];
  9.         float gpa;          }student;
  10.  
  11.  
  12. printf("Enter the number of students");
  13. scanf("%d",&nu);
  14. for(i=0;i<nu;i++)
  15. { printf("Enter the ID of the student");
  16.   scanf("%d",%student[i].id);
  17.   printf("Enter the name of student");
  18.   gets(student[i].name);
  19.   printf("Enter the GPA");
  20.   scan("%f",student[i].gpa);}
  21.  
  22.  
----------------------------------------
It cannot be compiled completly.

May any one plz help me and see where is the mistakes??
Dec 29 '07 #1
4 4062
Laharl
849 Expert 512MB
You can't define the struct inside the main function. It has to be done before the main.

Also, in C/C++, the main function returns an int, so you need to declare it as int main(), and return a value at the end of the function, conventionally 0.

Eg:
Expand|Select|Wrap|Line Numbers
  1. struct a{
  2.  int b;
  3. }
  4.  
  5. int main(){
  6.  ...
  7.  return 0;
  8. }
Dec 29 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
It's OK to declare the struct inside main().

The big problem is you have one student and you are using it like it was an array of students.
Dec 30 '07 #3
I did not get the editor u are doing. For better values it is recommended to do it in DOS mode but not in WINDOWS mode.
U can make the structure either inside or outside of the main().But u need to make the declaration as
1.
struct {
----------------;
}student;
(OR)
2.
struct {
-----------------;
};
now inside the main(),u call the structure as the given code.
struct record student;

This is the program:
-------------------------------------------------------
[code=c]
#include<stdio.h>
#include<string.h>

main()
{int nu,i; /* nu is number of student */

struct record
{ char id[8];
char name[35];
float gpa;
}student;/* here student is the member of the structure 'record' .
Even u can make an array for the member list also ,but u need to make another loop for it
*/

printf("Enter the number of students");
scanf("%d",&nu);
for(i=0;i<nu;i++)
{ printf("Enter the ID of the student");
/*scanf("%d",%student[i].id);*/
scanf("%d",%student.id[i]);
/* now u can make the difference because in the
above line u have given looping to the member of the structure.Don't do like that.that's why u might be getting problem in the compiling process */

printf("Enter the name of student");
gets(student.name[i]);
printf("Enter the GPA");
scan("%f",student.gpa[i]);
}
}

Now u can get this program compiled.Any problem
contact me: kalavnagarjuna@yahoo.co.in
Jan 1 '08 #4
[quote=kalavnagarjuna] I did not get the editor u are doing. For better values it is recommended to do it in DOS mode but not in WINDOWS mode.
U can make the structure either inside or outside of the main().But u need to make the declaration as
1.
struct {
----------------;
}student;
(OR)
2.
struct {
-----------------;
};
now inside the main(),u call the structure as the given code.
struct record student;

This
Jan 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
7
by: elmafiacs | last post by:
I need some help with an error I'm getting... Sorry if this is basic stuff, but I'm still a newbie on C programming (working up, though). Also, I'm using gcc as my compiler ('gcc t.c -o t', no...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
15
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member...
1
by: Marquee | last post by:
Hello, This is my first program c#, my background is c++. One thing I would like to do is put binary data (e.g. a record from disk, or network packet) in a struct. In C++ I would create the...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
5
by: Chris Lieb | last post by:
I am trying to create a "pseudo-class" using structs. Member variables seem easy enough to do. However, trying to implement member functions has led to nothing but frustration. I have tried...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.