| | |
Basic File I/O problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2004
Posts: 1
Reputation:
Solved Threads: 0
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 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.
•
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
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.
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.
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:
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)
#include <fstream> int main() { std::ofstream out("somefile"); int array[] = {0,1,2,3,4,5,6,7,8,9}; if (out.is_open()) { for (int i = 0; i < 10; i++) out << array[i]; } }
![]() |
Similar Threads
- First Assembly file problem (Assembly)
- Editing a file problem (Python)
- File I/O Problem: Incomplete or Missing Content During Writeline (Python)
- Windows Visa Home Basic Major Problem (Windows Vista and Windows 7)
- New member with a system file problem... (Windows NT / 2000 / XP)
- Visual basic why does my exe file need dotnetfx? (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: open source basic like program interperter development
- Next Thread: I need quick advice about my program
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





