what am i missing in this i/o file?

Discussion in 'C++' started by coryD, Mar 16, 2007.

  1. coryD

    coryD New Member

    Joined:
    Mar 16, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main ()
    {
    	ifstream in_file;
    	
    	in_file.open("Summation.dat",ios::in);
    	int i;
    	int A[20];
    	
    	float sumPos;
    	float sumNeg;
    	
    	in_file >> i;
    
    	while (!in_file.eof())
    	{
    	for (i = 0; i < 20; i++)
    	{
    		if (i < 0)
    		sumNeg += A[i];
    		
    		else sumPos += A[i];
    	}
    	in_file >> i;
    	}
    		
    	cout << "Sum of Positive Integers = " << sumPos << endl;
    	cout << "Sum of Negative integers = " << sumNeg << endl;
    	
    	in_file.close();
    	return 0;
    }
     
    Last edited by a moderator: Mar 16, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    if (i < 0)
    sumNeg += A;

    This is unnecessary and will never will be executed inside the for loop.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Furthermore, you have nothing in A[] to work with. You will tend to get better responses if you explain how your code is failing to meet your expectations. You're making us guess, is that nice? Fortunately you have some obvious blunders.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice