Averaging just 2 lines of file of integers

This is a discussion on Averaging just 2 lines of file of integers within the C++ Programming forums, part of the General Programming Boards category; Again, I am stuck! (last part of my program, thank god) I need to read just two lines of my ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Averaging just 2 lines of file of integers

    Again, I am stuck! (last part of my program, thank god) I need to read just two lines of my file of integers. I thought the following code would work, however, I am once again wrong and at a loss. Tell me WHY it doesn't work and if there is a different way I should be approaching it. Please keep in mind that I am trying to learn and a beginner.

    Code:
    inFile.open ("K:\\DATFILE1.TXT");
    	
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	sum = 0;
    	count = 0;
    	numCount = 0;
    	
    	while (inFile && (count < 3))
    	{
    	
    		inFile >> num;
    		numCount++;			
    		sum = sum + num;
    		count++;	
    	}
    
    	average = sum / numCount;
    	
    
    	outFile  << fixed << setprecision(3)
    	    	 << "The average of the first two lines of the file is " << average <<"." <<endl;
    
    
    	inFile.close();
    	inFile.clear();

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If something doesn't work, make sure you explain how it doesn't work. Is it giving you output you don't expect? What input are you giving it? What output are you getting? What output are you expecting? Are there compiler errors? What are they?

    In this case, you have to be able to tell where a line ends. You can't do that with inFile >> num because that will do the same thing on spaces and on newlines. Your code counts how many numbers there are and stops after 2. You want to stop after 2 lines.

    One solution would be to use getline to get the line, then use stringstreams to read the numbers in the lines. Another solution would be to use get() to get the character after each number and it's a new line if the newline character '\n' is found.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by Daved View Post
    If something doesn't work, make sure you explain how it doesn't work. Is it giving you output you don't expect? What input are you giving it? What output are you getting? What output are you expecting? Are there compiler errors? What are they?

    In this case, you have to be able to tell where a line ends. You can't do that with inFile >> num because that will do the same thing on spaces and on newlines. Your code counts how many numbers there are and stops after 2. You want to stop after 2 lines.

    One solution would be to use getline to get the line, then use stringstreams to read the numbers in the lines. Another solution would be to use get() to get the character after each number and it's a new line if the newline character '\n' is found.
    I'm not getting the answer that it should be. The output should be 266.888. The output I am receiving is 193.333. There are NO compiler errors. There are 4 lines of integers and I only want to use the first 2 lines. We have not yet used stringstreams. I tried using the get() and I kept just getting the first integer as my output. If I can stay awake some more I will try that again. If not I will have to try it again tomorrow. I haven't ever used the get(), although we briefly covered it in class.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are probably failing to detect when the last number has been read, resulting in "rubbish" added to sum and count incremented one more than it should.

    I could of course be wrong, but I think you should change your loop to be:
    Code:
    	while (inFile >> num && (count < 3))
    	{
    	
    		inFile >> num;
    		numCount++;			
    		sum = sum + num;
    		count++;	
    	}
    Remove the red, add the green bits. This will make sure that the loop breaks when you reach the last number, rather than "the next time round".

    By the way, why do you have "numCount" and "count" - both appear to have the same value...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I think you should take (count < 3) out of the while loop.
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by matsp View Post
    You are probably failing to detect when the last number has been read, resulting in "rubbish" added to sum and count incremented one more than it should.

    I could of course be wrong, but I think you should change your loop to be:
    Code:
    	while (inFile >> num && (count < 3))
    	{
    	
    		inFile >> num;
    		numCount++;			
    		sum = sum + num;
    		count++;	
    	}
    Remove the red, add the green bits. This will make sure that the loop breaks when you reach the last number, rather than "the next time round".

    By the way, why do you have "numCount" and "count" - both appear to have the same value...

    --
    Mats
    I was trying to count the number of integers read AND the number of times looped because I only want the first two lines. I am at work right now but will try all suggestions when I get home.

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    If there are more than 2 lines and you only want the first 2 of them, you're going to have to do one of the ways that Daved suggested. Here's the way I would use:
    Code:
    // open infile
    
    // for each of the two lines:
    
        // use getline to read the line from infile into a string
    
        // create and istringstream object from the string (need to include <sstream>
    
        // for each number in the line
    
            // read the number from the istringstream object 
            // ex        strstream >> num;
    
            // add it to sum
    
            // increment count
    
    // divide sum by count
    
    // display the result
    This might help: FAQ's Converting a string to an int
    Don't quote me on that... ...seriously

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I haven't ever used the get(), although we briefly covered it in class.

    There has to be something you covered in class that allows you to tell the end of the line. You can't do it with operator >>. Your job is to remember what you've learned that can accomplish the task, because there are many ways to do it and you probably want to use the way that was taught.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by Daved View Post
    >> I haven't ever used the get(), although we briefly covered it in class.

    There has to be something you covered in class that allows you to tell the end of the line. You can't do it with operator >>. Your job is to remember what you've learned that can accomplish the task, because there are many ways to do it and you probably want to use the way that was taught.
    (\n)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Replies: 6
    Last Post: 08-04-2003, 11:57 AM

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