Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,000 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,321 people online right now. Registration is fast and FREE... Join Now!




Problem displaying in outfile while looping

 
Reply to this topicStart new topic

Problem displaying in outfile while looping

adrianmty
29 Nov, 2006 - 11:26 PM
Post #1

New D.I.C Head
*

Joined: 29 Nov, 2006
Posts: 1


My Contributions
Hi im new in c++ and im taking an introdution to programming using c++, i have to create a program that will display gas mileage for each employee that drives a company car .... miles driven for each employee, gallons purchased and miles per gallon of each employee and then the average of the miles per gallon, right now im stuck in the display thing because im having troubles trying to save the results to the outfile, the first result is saved but after the loop starts something is wrong with the outfile if someone can helpme please!!
// Adrian Torres
//Lab 3 App.cpp- Displays gas mileage for each employee that drives a company car,
// total miles driven for the company, gallons purchased and average miles per gallon.
CODE

#include <iostream>
#include <fstream>

using namespace std;



int main()

{
    int driverNumber = 0;
    int companyCarNumber = 0;
    int milesDriven = 0;
    int gallonsPurchased = 0;
    int milesPerGallon = 0;
    int totalDrivers = 0;
    float average = 0.0;
    int  totalmilesPerGallon =0;

    ofstream outFile;
    outFile.open("3rdLab.dat",ios::app);

    // Enter input items
    cout<<"Enter the driver number:";
    cin>>driverNumber;
    

    cout<<"Enter the Company Car Number:";
    cin>>companyCarNumber;
    

    cout<<"Enter the miles driven:";
    cin>>milesDriven;

    cout<<"Enter the gallons purchased";
    cin>>gallonsPurchased;

    milesPerGallon= milesDriven/gallonsPurchased;

cout<<"Driver Number:"<<driverNumber<<endl;
outFile<<driverNumber<<endl;

cout<<"Company Car Number:"<<companyCarNumber<<endl;
outFile<<"Company Car Number:"<<companyCarNumber<<endl;
cout<<"Miles Driven:"<<milesDriven<<endl;
outFile<<"Miles Driven:"<<milesDriven<<endl;
cout<<"Gallons Purchased:"<<gallonsPurchased<<endl;
outFile<<"Gallons Purchased:"<<gallonsPurchased<<endl;
    
cout<<"Miles per Gallon:"<<milesPerGallon<<endl;    
outFile<<"Miles per Gallon:"<<milesPerGallon<<endl;

    while (driverNumber<4)
    {    
        totalDrivers= driverNumber + 1;
        totalmilesPerGallon=totalmilesPerGallon+milesPerGallon;
        
        cout<< "Enter the next driver number:";
        
        outFile<<driverNumber<<endl;
        cin>>driverNumber;
    
        cout<<"Enter the next Company Car Number:";
        
        outFile<<"Company Car Number:"<<companyCarNumber<<endl;
        cin>>companyCarNumber;

        cout<<"Enter the miles driven:";
        
        outFile<<"Miles Driven:"<<milesDriven<<endl;
        cin>>milesDriven;
        cout<<"Enter the gallons purchased";
        
        outFile<<"Gallons Purchased:"<<gallonsPurchased<<endl;
        cin>>gallonsPurchased;
milesPerGallon= milesDriven/gallonsPurchased;

cout<<"Miles per Gallon:"<<milesPerGallon<<endl;    
outFile<<"Miles per Gallon:"<<milesPerGallon<<endl;    
outFile.close();
    


    if (totalDrivers!=0)
    {
        average= totalmilesPerGallon/totalDrivers;
        
    }
    
    }

    //end while
    //end if
        
cout<<"Average:"<<average<<endl;
    
return 0;
}



//Display Output Items

CODE

this is the actual saved information in the outfile....................................................
1Driver Number:1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
Driver Number:2
Driver Number:2
Company Car Number:9999
Miles Driven:400
Gallons Purchased:25
Miles per Gallon:16
1Company Car Number:0Miles Driven:0Gallons Purchased:0Driver Number:2Company Car Number:2468Miles Driven:325Gallons Purchased:30Driver Number:2Miles per Gallon:16
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
Driver Number:2Company Car Number:9999Miles Driven:400Gallons Purchased:25Miles per Gallon:16
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
Driver Number:2Miles Driven:325Driver Number:2Company Car Number:9999Miles Driven:400Gallons Purchased:25Miles per Gallon:16
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
2
Miles Driven:400
Driver Number:2
Company Car Number:9999
Miles Driven:400
Gallons Purchased:25
Miles per Gallon:16
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
2
Company Car Number:9999
Miles Driven:400
Gallons Purchased:25
Miles per Gallon:16
2
Company Car Number:9999
Miles Driven:400
Gallons Purchased:25
Miles per Gallon:16
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
1
Company Car Number:2468
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
1
Company Car Number:2468
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:10
1
Company Car Number:2468
Miles Driven:325
Gallons Purchased:30
Miles per Gallon:16


edit: added tags ~ jayman9

This post has been edited by jayman9: 30 Nov, 2006 - 07:29 AM
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Problem Displaying In Outfile While Looping
30 Nov, 2006 - 07:10 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
It breaks becouse you close the output file in the loop.
You should close it after the loop.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 08:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month