C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 03:01 AM

Hello everyone, it's been a while since I've done anything in C++ so please excuse the errors, modularity, etc. For this homework assignment we basically have to write a program that reads certain data from a file into an array, (My professor says we don't have to use the array if we can "analyze the data stream as it comes in" but I can't figure that out and I haven't been able to find out how to do it online unless you all can help me.), count how many different numbers are in the file, use a mergesort to put the numbers in order, etc.
The problem so far is how to get the second row of data out of the file and put it in an array.

For example if my file look like this:

1. 20
2. 15
3. 77
4. 15
5. 29
6. 77

How would I get the 20, 15, etc. into an array without reading the 1. , 2. , 3. ?
I have more questions but I need to figure this out first before I get into that. Thanks in advance!!!

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 04:34 PM

Ok so I was able to figure out how to put the information in the array but when I prompt the user for the name of the file to be used and type it in, it can't find the file. Is my code for opening the file wrong or what, because I typed the name of the file correctly and should have been able to open it. Can someone please help me figure this out?

create vector for integers
get file name
open file
begin loop
get data from file
add int to vector
end loop
print vector contents
close file

Thank you so much! I've added a quicksort to my program to sort the array. However we have to use a O(n log n) to calculate the time to do the sort. For example the user inputs 0.3 which is O. The count of how many times the number changes = n. How would I put this in code form, because I don't know anything about algorithms.

#define, on 28 January 2013 - 03:25 AM, said:

Yes, you could get rid of line 33.

Are you just reading from one file?

Then I don't think you should be opening the file in the while loop.

Should the program follow a plan like this?

create vector for integers
get file name
open file
begin loop
get data from file
add int to vector
end loop
print vector contents
close file

Thank you so much! I've added a quicksort to my program to sort the array. However we have to use a O(n log n) to calculate the time to do the sort. For example the user inputs 0.3 which is O. The count of how many times the number changes = n. How would I put this in code form, because I don't know anything about algorithms.