ifstream file not loading data file

Discussion in 'Mac Programming' started by Addiniajo, Feb 23, 2010.

  1. Addiniajo macrumors newbie

    Joined:
    Feb 23, 2010
    Location:
    Orono, Maine
    #1
    I'm using Xcode version 3.1.4 using GCC 4.0 for debugging, on my Mac OSX 10.5.8, to compile a project for an intro to C++ programing class. I've looked at lot of similar posts already, tried the suggestions in most of them, and none work. The interesting thing is that it's not giving me back garbage, but 0 and 0.0 however the check's i've done to see if it's opening fail, so i'm not sure if the files opening or if the data's not reading in correctly. Below I have both the code and data set; if anyone has any suggestions I'd really appreciate an help.


    Program Code
    Code:
    //COS 220 Section 1 Homework #3
    //a listing of different lakes in Maine along with the heaviest, 
    //lightest, and average number of fish collected in each corresponding lake
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    
    int main(){
    	
    //initiates needed variables
    int ID;		        //lake id
    double fishWeight=0;//weight of each fish
    double avgFishWeight=0;//the average fish weight from a lake
    double fishWeightSum=0;//the total weight of the fish at a certain point
    double heaviest=0;//the heaviest fish weight
    double lightest=0;//the lightest fish weight
    double fishCt=0;
    
    ifstream fin;//initiates text file read in variable
    	
    //forces a decimal point in floats, allows specification of the amount of numbers in a 
    //sequence which is displayable and also sets right justification.
    cout.setf(ios::fixed | ios::showpoint | ios::left);
    //sets decimal point to be constrained to 1 place
    cout.precision(1);
    cout.width(8);
    
    //sends output of project to a specified file
    ofstream fout;
    fout.open ("Homework3Data.txt");
    fout.setf (ios::left);
    	
    fout<<"Lake Data: Sean McKee"<< endl<<endl<<endl;
    fout<< "Lake ID "<<setw(8)<<"Lake Name"<<endl;
    fout<<"_______________________"<<endl;
    
    //reads in the data from a text file of the data from data collections
    fin.open ("A3.txt", ifstream::in);
    	
    //will continually read in the data one after another until no more data          
    //is available
    fin >> ID >> fishWeight;	//initial/priming read
    	
    cout<<ID<<"  "<<fishWeight<<endl;   
    
    //personal check for data
    //check to see if file's opened
    if (!fin) 
    cout<<"Couldn't open file"<<endl;
    	
    while(!fin.fail())
    {	fin>>ID>>fishWeight;
    
    //number of fish collected
    fishCt++;
    //average fish weight over all sites
    fishWeightSum = fishWeightSum+fishWeight;
    avgFishWeight= (fishWeightSum)/fishCt;
    			
    //outputs the correct lake name as it corresponds to it ID
    switch (ID)
    {
    case 1000:	fout<<"Chemo";		break;
    case 1010:	fout<<"Eddington";	break;
    case 1050:	fout<<"Hopkins";	break;
    case 1100:	fout<<"Toddy";		break;
    case 1250:	fout<<"Branch";		break;
    case 1300:	fout<<"Phillip";	break;
    case 1350:	fout<<"Green";		break;
    default:	fout<<"error";
    }
    			
    //outputs the weight of the fish collected
    cout<<setw(8)<<fishWeight<<endl;
    
    //determines heaviest fish
    heaviest = fishWeight;
    if(heaviest<fishWeight)
    fishWeight=heaviest;			
    			
    //determines lightest fish
    lightest = fishWeight;
    if(lightest>fishWeight)
    fishWeight=lightest;
    
    				  
    fin>>ID>>fishWeight;//reads for next run
    }
    fin.close();
    
    
    fout<<"Total number of fish collected"<<endl<<fishCt<<endl;
    fout<<"Average fish weight over all sites"<<endl<<avgFishWeight<<endl;
    fout<<"Weight of heaviest fish"<<endl<<heaviest<<endl;
    fout<<"Weight of lightest fish"<<endl<<lightest<<endl;
    fout.close();
    	
    
    return 0;	
    }
    
    and the data set (file name "A3")

    1000 4.0
    1000 2.0
    1000 1.5
    1010 2.0
    1010 2.2
    1010 1.9
    1050 2.8
    1050 4.2
    1100 1.7
    1100 1.6
    1250 2.2
    1250 4.5
    1300 4.0
    1300 5.1
    1300 3.4
    1350 1.1
    1350 1.2
    1350 2.0
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #2
  3. Addiniajo thread starter macrumors newbie

    Joined:
    Feb 23, 2010
    Location:
    Orono, Maine
    #3
    that seems to be a problem that pops up in the newest version of Xcode 3.2.1 for snow leopard on the mac OSX 10.6. I checked the release notes published for the Xcode versions which have been published so far. 3.2 is the first instance that the processor problem arose in, none of the previous versions mentioned the preprocessor names or similar problems. Just to double check I checked it out in mine, (going through the steps need to remove them) and the preprocessors, where they should be were and are currently blank.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #4
    Try compiling and running it in Terminal, using simple command-line.

    Or post a few lines of your expected output.

    Because it compiles and runs in Terminal for me, but I have no way of making sense from the output without analyzing your code, and it's more code than I feel like analyzing right now.

    Here's the stdout output:
    Code:
    1000     4.0
    2.0     
    2.0     
    1.9     
    4.2     
    1.6     
    4.5     
    5.1     
    1.1     
    2.0     
    
    Output file "Homework3Data.txt":
    Code:
    Lake Data: Sean McKee
    
    
    Lake ID Lake Name
    _______________________
    ChemoEddingtonEddingtonHopkinsToddyBranchPhillipGreenGreenTotal number of fish collected
    9
    Average fish weight over all sites
    2.71111
    Weight of heaviest fish
    2
    Weight of lightest fish
    2
    
     

Share This Page