Thread: File output problem using ofstream

  1. #1
    yatta!¿
    Guest

    File output problem using ofstream

    Hi, this code doesn't seem to output the file properly. Can you help? Thanks.
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	// hours for each day
    	double mon;
    	double tue;
    	double wed;
    	double thu;
    	double fri;
    	double sat;
    	double sun;
    
    	double totalhours = 0;
    	double totalpayed = 0;
    	double payrate = 6.27;
    
    	char filename[256];
    	char totalpayed_str[256];
    
    	cout << "Enter the hours worked on the following days:\n\n";
    	
    	cout << "MONDAY: ";
    	cin >> mon;
    	cout << "\nTUESDAY: ";
    	cin >> tue;
    	cout << "\nWEDNESDAY: ";
    	cin >> wed;
    	cout << "\nTHURDAY: ";
    	cin >> thu;
    	cout << "\nFRIDAY: ";
    	cin >> fri;
    	cout << "\nSATURDAY: ";
    	cin >> sat;
    	cout << "\nSUNDAY: ";
    	cin >> sun;
    	cout << "\n";
    	cout << "******************************************************\n";
    
    	totalhours = (mon + tue + wed + thu + fri + sat); // sun not added
    	totalpayed = payrate * totalhours;
    	totalpayed += ( sun * (payrate * 1.5) ); // time and a half on sundays
    	totalhours += sun; // add sun into the total hours becuase it was missed
    
    	cout << "You will be payed $" << totalpayed << " for working " << totalhours << " hours this week.\n";
    
    	// print out file ----------------
    	
    	_gcvt(totalpayed, 5, totalpayed_str);
    	strcpy(filename, "C:\\");
    	strcpy(filename, "Getting paid ");
    	strcpy(filename, totalpayed_str);
    	strcpy(filename, " this week.txt"); 
    	
    	ofstream outfile(filename);
    
    	outfile << " ";
    
    	outfile.close();
    
    	return 0;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What do you want in the file?

    b.t.w. _gcvt is not ANSI C/C++ (use sprintf instead)

    Code:
    ofstream outfile;
    outfile.open ("test.txt", ofstream::out | ofstream::app);
    if(outfile.is_open())
    {
       outfile << "You will be payed $" << totalpayed << " for working " << totalhours << " hours this week.\n";
       outfile.close();
    }

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, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 08:39 PM