fstream dilemma

This is a discussion on fstream dilemma within the C++ Programming forums, part of the General Programming Boards category; Well I thought I had this all figured out. Something is wrong with my logic and I can't figure it ...

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Question fstream dilemma

    Well I thought I had this all figured out. Something is wrong with my logic and I can't figure it out. I am supposed to find the median of a group of numbers in a file that I have created (theData.dat), but obviously if there are an even number of integers in the file, it gets a little tricky because you have to pull the two middle numbers from the file and divide them by 2. AND I am just getting used to using while loops. Can someone give me some tips?
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	ifstream In;
    	In.open("theData.dat");   // opens file
    	int aNumber;   
    	int count = 0;
    	int median, m1, m2;
    	int countAgain = 0;
    	while (In >> aNumber)
    	{
    		count++;        // counts the number of int in the file
    	}
    	In.close();             // allows me to run through the file from the beginning
    	In.clear();
    	In.open("theData.dat");
    	while (In >> aNumber)
    	{
    		countAgain++;      
    		if ((count % 2 != 0) && (countAgain == (count + 1 ) / 2))
    		{
    			median = aNumber;
    		}
    		else
    		{
    			while (countAgain == count / 2)
    			{
    				m1 = aNumber;
    			}
    			while (countAgain == (count + 2) / 2)
    			{
    				m2 = aNumber;
    			}
    			median = (m1 + m2) / 2.0;
    		}
    		
    	}
    	cout << "The median of this group of numbers is " << median << "." << endl;
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    while (countAgain == count / 2)
    			{
    				m1 = aNumber;
    			}
    			while (countAgain == (count + 2) / 2)
    			{
    				m2 = aNumber;
    while doesn't check if something is true; "if" checks if something is true.

  3. #3
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32
    oh HO HO! Thank you!

  4. #4
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32
    I just checked it and that worked. Thank god for this forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 11:21 AM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21