943,427 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5323
  • C++ RSS
Feb 16th, 2004
0

Basic File I/O problem

Expand Post »
I'm pretty new at C++ and I have a problem with a program that opens a file with a list of numbers and finds the median. My problem is, no matter what numbers are in the file, it always says the median is 1. I've been able to tell it what position the median is in, but I can't get it to return the number that's in that position. How can I get it to reopen the file, find the median number, and spit it back to me?

Attached is what I've done so far.
Here is what the data file is uses looks like:
file2.dat
1
2
3
4
5
6
7
8


I know the simplicity of it is laughable, but bear with me. I'm a newbie.
Attached Files
File Type: cpp median2.cpp (840 Bytes, 38 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jeannie is offline Offline
1 posts
since Feb 2004
Feb 18th, 2004
0

Re: Basic File I/O problem

Hi, don't worry, it's not laughable, we've all been equally as stumped with problems equally as simple once upon a time.

Having calculated which position the median is at, you then reopen the file, read in the first value from the file, and output this as the median. The first value in the file is 1, so the program always outputs this as the median, even though it's not the value in the median position.

You need to read in values until you've reached the one at the median position, rather than just reading in the first value.

Hope that helps.
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Jun 19th, 2005
0

Re: Basic File I/O problem

how do u save an array into a file?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Big_D is offline Offline
1 posts
since Jun 2005
Jun 19th, 2005
0

Re: Basic File I/O problem

Dude, this thread is well over a year old. Next time, if you have a new question, start a new thread.

To save an array to file, just open the file, then loop over the array and write each element in turn:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2.  
  3. int main()
  4. {
  5. std::ofstream out("somefile");
  6. int array[] = {0,1,2,3,4,5,6,7,8,9};
  7.  
  8. if (out.is_open()) {
  9. for (int i = 0; i < 10; i++)
  10. out << array[i];
  11. }
  12. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: open source basic like program interperter development
Next Thread in C++ Forum Timeline: I need quick advice about my program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC