473,399 Members | 3,401 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,399 software developers and data experts.

storing input into arrays using a struct

Ok Im trying to use a struct to take in input from a file "grades.txt" which looks like:
Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 96 100 98 92 88 96 92 86 92 94 100 96
Jones 9 8 6 6 8 7.5 8 20 35 40 42.5 40 44 62 72 100 88 86 90 92 92 88 86 88 86 94 88 86 90....etc i don't know how many students their will be.
And I need to store them in a structre like how I have in my code...Im unsure how to get all the input to go into each of these arrays . ANy help will be greatly appreciated..heres wha i have so far.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //Structure for holding all the grades                                        
  8. struct Grades
  9. {
  10.     string student;
  11.     float quizzes[7];
  12.     float projects[6];
  13.     float exams[2];
  14.     float labs[14];
  15. };
  16.  
  17. int main()
  18. {
  19.     ifstream inFile;
  20.     inFile.open("grades.txt");
  21.     if (inFile.fail()) {
  22.         cerr << "unable to open file input.dat for reading" << endl;
  23.         exit(1);
  24.     }
  25.  
  26.  
  27. }
  28.  
the student name comes first then the quizzes (7) then the Projects (6) then exams (2) then labs (14) I dont know how I'm going to get the input to go into the right arrays...Also I don't know how I will be able to take all that input at once (defferent amounts of students and their scores and get them to go into seperate structs so that the one struct doesn't have more than 1 student in it....Im kinda lost and I need some direction any help will be great. In the future of this program im going to need to output a table like this if thats any help to you:
No. –Name-- --Quiz-- -Project- -Exam- -Lab- -Total- Grade
--- ----------- -------- ---------- ------ ----- ------- -----

1. Smith 90.55 94.44 85.00 94.86 91.41 A-
2. Jones 77.50 82.04 67.00 89.57 78.88 C+
.
.
.
--- ----------- -------- ---------- ------ ----- ------- -----
Avg: ?
Apr 26 '07 #1
5 4932
Savage
1,764 Expert 1GB
Hi,lets see:

I think that nested loops are best solution to ur problem.


Outer while loop should loop until eof.(end of file).

Then we read in one line using fgets or any simmilar method.

Inner while loop should go trough that line until reaching specific character that u can specify or until reaching '\n' character in line

now:

if is alpha we put that char in temporary char array and later we asign it to the string in ur structure.
else read in specific amount of numbers and store it the same way to the floats and other variables in ur struct.


Savage
Apr 26 '07 #2
so will this work then...and then just start with some nested loops inside the while loop...
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //Structure for holding all the grades                                        
  8. struct Grades
  9. {
  10.     string student;
  11.     float quizzes[7];
  12.     float projects[6];
  13.     float exams[2];
  14.  
  15.     float labs[14];
  16. };
  17.  
  18. int main()
  19. {
  20.     string line;
  21.     ifstream myfile ("grades.txt");
  22.     if (myfile.is_open())
  23.     {
  24.         while (! myfile.eof() )
  25.         {
  26.             getline (myfile,line);
  27.         }
  28.         myfile.close();
  29.     }
  30.  
  31.     else cout << "Unable to open file";
  32.     }
  33.  
  34.  
  35. }
  36.  
Apr 26 '07 #3
Savage
1,764 Expert 1GB
Yes,that should do it.

Savage
Apr 26 '07 #4
Yes,that should do it.

Savage

how would i do an if is alpha loop for (int i = 0, line[i] == char, i++){
student = student + line[i];}

im not sure about that one ive never done it.
Apr 26 '07 #5
Savage
1,764 Expert 1GB
how would i do an if is alpha loop for (int i = 0, line[i] == char, i++){
student = student + line[i];}

im not sure about that one ive never done it.

This function is builded so that it returns 0 if the char is not a alpha.

So simple while loop like:

i=0;
while(isalpha(line[i])
{
//extract it to ur string in struct

}

should work.
Apr 27 '07 #6

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

Similar topics

2
by: Mark Hannon | last post by:
I am trying to wrap my brain around storing form elements inside variables & arrays before I move on to a more complicated project. I created this simple example to experiment and as far as I can...
19
by: becte | last post by:
I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8) like this 11111122|22223333|33444444 Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1 and the output is int...
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
2
by: Kay | last post by:
A linked list is storing several names. I want to make a queue if I input a name that is same as the linked list. How to make each node of a linked list storing a queue that are different with each...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
14
by: Stainless | last post by:
I have a public class Globals, which obviously holds all my global data. I have an array of 243 items, currently structs of type typedef struct STAR { int x; int y; int stellar_class;...
9
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
17
by: Christoph Scholtes | last post by:
Hi, I have two questions about the following code snippet. I am trying to read in a series of strings and save them to character arrays. Since I dont know how long my string is going to be (and...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.