The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Help with Structures
Discuss Help with Structures in the C Programming forum on Dev Shed. Help with Structures C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 26th, 2013, 01:18 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 4
Time spent in forums: 2 h 24 m 55 sec
Reputation Power: 0
|
|
|
Help with Structures
Currently I am working with structures, however I am not quite sure how to use the printf function after the input has been stored. Below is my code. Suggestions? Thanks in advance by the way
Code:
#include <stdio.h>
typedef struct
{
char name[50];
float grade[4];
float aver;
int pos;
}stud;
stud all[15];
stud read();
int a;
int main()
{
for(a=0;a<15;a++)
{
stud[a]=read();
printf("HELP HERE PLEASE");
}
|

January 26th, 2013, 01:21 PM
|
 |
Contributed User
|
|
|
|
|
Something like
stud students[15];
Then
students[a] = read();
|

January 26th, 2013, 02:03 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
You've created a new datatype called stud. You then declared an array, all, whose elements are of type stud.
This line
stud[a]=read();
makes no sense whatsoever. There is no array named "stud". There is, however, an array named "all", so you should be using it instead.
The syntax for accessing a field in a struct is this: <struct variable>.<fieldname>
So to access the aver field of the second struct in the array, you would write this: all[1].aver
|

January 26th, 2013, 08:56 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 4
Time spent in forums: 2 h 24 m 55 sec
Reputation Power: 0
|
|
|
Thanks for the help. I have on final question. I dont want to type in all the data when it comes to doing a test run of my program. Is it possible to read all of the input from a text file?
|

January 27th, 2013, 12:37 AM
|
 |
Contributed User
|
|
|
|
|
Of course you can read input from a file.
scanf("%d", &num);
can be written as
fscanf(stdin,"%d", &num);
From there, it's just a small step to
FILE *fp = fopen("data.txt","r");
fscanf(fp,"%d", &num);
|

January 27th, 2013, 02:58 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by bass123 I dont want to type in all the data when it comes to doing a test run of my program. Is it possible to read all of the input from a text file? |
If your manual input is via stdin (i.e. you are using standardio functions such as getchar() etc.), then you can use redirection to take input from a file rather than manual input. So for example if you have a file grades.dat containing:
Joe Bloggs
14
Sam Spoon
23
Jane Doe
24
John Doe
20
And a program called grades.exe, you can redirect the file to stdin thus:
grades <grades.dat
and the data will be input as if you had typed it from the keyboard.
This approach means that you can write the code for manual input but test with a file rather than specifically writing it for file access as suggested by Salem. The choice would depend on your requirements.
|

January 28th, 2013, 04:33 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 5 h 21 m 37 sec
Reputation Power: 0
|
|
|
structure problem
This is meaningful way:
stud all[15];
all[0].aver=18;
all[1].aver=28;
all[2].aver=45;
all[0].name="Collingam";
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|