Thread: fstream

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

    fstream

    I have just learned how to use fstream in my programming class and we are supposed to gather the information from a file we have created which I have named Section51.dat
    The program is supposed to find the largest integer in the file. There is something wrong with my while loop and I can't work it out in my head. It keeps telling me the number of integers in the file instead of telling me the LARGEST integer in the file. Can someone show me how to design this loop in the correct way? I am having trouble seeing how it will compare one integer to the next in a loop .
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	ifstream RED;
    	RED.open("Section51.dat");
    	int aNumber;
    	int highestNumber = 0;
    	while (RED >> aNumber)
    	{
    		if (aNumber >> highestNumber)
    		{
    			highestNumber = aNumber;
    		}
    	}
    	cout << highestNumber;
    	return 0;
    }
    Last edited by Amyaayaa; 03-29-2008 at 09:10 PM. Reason: misspelled words

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    if (aNumber > highestNumber)

    is probably what you want.

  3. #3
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32
    Oh thank you thank you thank you
    what a stupid little mistake that has wasted so much of my time!
    You rock!

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, 10: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, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM