Files, structs, arrays.. misunderstanding

Reading a file into a program using an array and printing it out using

Page 1 of 1

5 Replies - 876 Views - Last Post: 11 December 2009 - 05:53 PM Rate Topic: -----

#1 specify  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 09

Files, structs, arrays.. misunderstanding

Posted 11 December 2009 - 04:21 PM

This is the file i have to open using my ifstream object fin, .. fin.open("exam1.txt");
My problem is i have to take the names, starting with Sam First and put them into an array along with the students answers;

and then i need to put the array in a for loop to print them out after the files have been read in using an array properly, i dont understand how to.


4002-210
Rayno Niemi
AAAAAAAAAB
Sam First
AAAAABAAAA
Mary Thompson
BCAAAAAAAA
Tom Miller
AAACCCAAAA
Joe Johnson
AAAAAAAAAB
Swen Anderson
AAAAAAAAAA
Betty Last
AAACAAAABA


	ifstream fin;		 //Declare an ifstream object, namely 'fin'
	ExamInfo open;	//Declare the variable for struct type ExamInfo as 'open'
	Student use;		 //Declare the variable for struct type Student as 'use'
	//string fileName;  //Declare string to enter file
	fin.open("exam1.txt");
	
	getline(fin, open.course);
	getline(fin, open.instructor);
	getline(fin, open.answerKey);
	getline(fin, use.studentName);
	getline(fin, use.studentAns);
 



Is This A Good Question/Topic? 0
  • +

Replies To: Files, structs, arrays.. misunderstanding

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Files, structs, arrays.. misunderstanding

Posted 11 December 2009 - 04:53 PM

You need an array of Student objects, not just one. Then, after you read the course, instructor and answerKey data, you want a loop that reads the Student data into the Students in the array, one by one.
Was This Post Helpful? 0
  • +
  • -

#3 specify  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 09

Re: Files, structs, arrays.. misunderstanding

Posted 11 December 2009 - 05:07 PM

View Postr.stiltskin, on 11 Dec, 2009 - 03:53 PM, said:

You need an array of Student objects, not just one. Then, after you read the course, instructor and answerKey data, you want a loop that reads the Student data into the Students in the array, one by one.



ok, i understand that, if i were to declare for say, Student studentNameAns[ ]; as an array to read in the names and answers i get an error saying that i didnt enter a storage amount, and if i do enter a storage amount, for example like 6, then it doesnt print out the files properly that it was supposed to read in and print out it gives me garbage numbers, i guess is what i dont get is how to create a string array without initializing the storage amount because i know you have to intalize it with like { whateverrrr } braces, since im declaring the array as a Student type though i figured it wouldn't matter that i'm trying to read in array of strings, but i get the wrong output bc i can't compile the prgram without entering a storage amount :-(
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Files, structs, arrays.. misunderstanding

Posted 11 December 2009 - 05:37 PM

It isn't a string array, it's a Student array, and each Student (each element of the array) consists of, I assume, 2 strings and maybe some other fields? So, for example, to read a name into the name field of the first student in the array, the string that you pass to getline is studentNameAns[0].name.
Was This Post Helpful? 1
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Files, structs, arrays.. misunderstanding

Posted 11 December 2009 - 05:41 PM

If you want to have an array type data store that doesn't need to be provided with a size at compile time you would need to look at using a vector instead.
http://www.cplusplus...nce/stl/vector/

If you do declare an array size then the chances are that the memory addresses you are claiming for the array are not empty. They have garbage stored in there from the last time that memory was used. If you want to ensure they are empty you should run through a loop filling all the elements of the array with nulls or zeros.
Make sense?

It's hard to know what you mean by "doesnt print out the files properly" without you supplying an example contrasting what you want with what you are getting.

The stuff about strings in your question is a mess that doesn't really make sense as you seem to start out saying it's important then by the end it isn't important.

Basically you need to pause for 5 minutes and really think about your next question so you word it as clearly as you are able and then you'll maximise the chance of getting a good answer.
Was This Post Helpful? 1
  • +
  • -

#6 specify  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 09

Re: Files, structs, arrays.. misunderstanding

Posted 11 December 2009 - 05:53 PM

Thanks for the help stiltskin, and janotte
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1