How to get to the next line in a file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to get to the next line in a file

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    How to get to the next line in a file

    Ok so i have a program that takes in information from an input file and sends it to an output file. There are multiple lines in the input file and im trying to loop it to read in each line process the information and put it into the output file but its only reading in one line. So How do i get it to skip to the next line after reading in the information from the first line. Here is my code

    Code:
    #include "BAC.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    
    const int WEIGHT_DIST = 600;
    const int AVG_BLOOD_OZ = 169;
    const float ALC_PER_DRINK = .06f;
    float calcBac(int beersh, int fatty);
    string test(float drunk);
    
    int main()
    {
    	bool index = true;
    	string sentance;
    	string fileName1;
    	string fileName2;
    	string firstName;
    	string lastName;
    	int numBeers;
    	int weight;
    	float bac;
    	ifstream inFile;
    	ofstream outFile; 
    	cout << "What is the name of the input file:";
    		cin >> fileName1;
    	cout << "What is the name of the output file:";
    		cin >> fileName2;
    	inFile.open(fileName1.c_str());//open the file
    	outFile.open(fileName2.c_str());//open the output 
    	int i = 0;
    	while(index)
    	{
    		inFile >> firstName >> lastName >> numBeers >> weight; //read in the information from the file
    		bac = calcBac(numBeers, weight);
    		sentance = test(bac);
    		firstName = firstName.substr(0,1);
    		outFile << lastName << ", " << firstName << " " << numBeers << " " << setprecision(2) << bac << " " << sentance;
    		if(!inFile.eof())
    		index = false;
    	}
    	inFile.close();
    	outFile.close();
    
    }
    float calcBac(int beersh, int fatty)
    {	
    	float answer;
    	answer = (float)(WEIGHT_DIST * beersh)/ (fatty * (float)(AVG_BLOOD_OZ+(ALC_PER_DRINK * beersh)));
    	return answer;
    }
    string test(float drunk)
    {
    	string message;
    	if(drunk < .04 && drunk > .00)
    		message = "Legal, but Dumb.";
    	else if(drunk < .08)
    		message = "Ability impaired.";
    	else if(drunk >= .08)
    		message = "Illegal, should have called a cab.";
    	else if(drunk < .00)
    		message = "Invalid data";	
    	return message;
    }

  2. #2
    Join Date
    Apr 2010
    Posts
    5

    Re: How to get to the next line in a file

    got it working ... eof was in the wrong place

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center