Ouputting to a file?

This is a discussion on Ouputting to a file? within the C++ Programming forums, part of the General Programming Boards category; OK heres my second question of the day. I am trying to output my information to the screen and to ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    33

    Ouputting to a file?

    OK heres my second question of the day. I am trying to output my information to the screen and to a file called billing.dat. I have perused the board and I just don't understand the FAQ or other users explaination of how to do it. I do know I have to include fstream (which I did) but from there I am completely lost. I have included my sample code...I don't want it done for me just a laymans explaination of how to output my information to a file. I am a newbie after all =/

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;
    const double BLUE_RUG = 6.99;
    
    
    
    
    
    
    
    int main()
    
    {
    	char filename[15];
        string discount;
    	char customerName[25];
    	char rugColor;
        char paymentMethod;
    	int roomLength;
        int roomWidth;                                            //variables
    	string rugDisplay;
    	string discountPercent;
    	int roomArea;
    	double endDiscount;
    	double paidDiscount;
    	double rugCost;
    	double carpetCost;
    	double trueCost;
     
        cout<<setiosflags(ios::fixed);
    
    	cout<<"Enter file name for billing: ";
    	cin.getline(filename,15);
    
    	
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Phils Floors<<<<"<<endl;                     //business header-output to screen
    	cout<<endl;
    	cout<<setw(36)<<"Our Rugs Rock"<<endl;
    	cout<<endl;
    
    
    	cout<<"If you enter the customers name, type of rug purchased,"<<endl;
        cout<<"room dimensions, and type of payment, this program"<<endl;       //program explaination
        cout<<"will calculate and print a bill to a file, Billing.dat"<<endl;
    	cout<<endl;
    	
    
    	cout<<"Please enter the customers name: ";
    	cin.getline(customerName,25);                                //input customers name
    	cout<<endl;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
        cin>>rugColor;                                               //input rug color
    	cout<<endl;
    
    	if (rugColor == 'R' || rugColor == 'r')
    	{
    		rugDisplay = "RED";
    		rugCost = RED_RUG;
    	}
    	else
    		if (rugColor == 'B' || rugColor == 'b')
    	{
    		rugDisplay = "BLUE";                                     //set rugdisplay variable and cost of rug
    		rugCost = BLUE_RUG;                                          
    	}
    	else
    		if(rugColor == 'G' || rugColor == 'g')
    	{
    		rugDisplay = "GREEN";
    		rugCost = GREEN_RUG;
    	}
        else
    	{
    		rugDisplay = "Default";
    		rugCost = 6.99;
    	}
    	 
        cout<<"Please enter the method of payment"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther: ";
    	cin>>paymentMethod;                                           //input payment method
    	cout<<endl;
     
        if (paymentMethod == 'C' || paymentMethod == 'c')
    	{
    		paidDiscount = .10;
    	}
    
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')        //set paidDiscount variable to use in calculation
    	{
    	    paidDiscount = .05;
    	}
    
    	else 
    	{
    	    paidDiscount = 1;
    	}
    
    
    	if (paymentMethod == 'C' || paymentMethod == 'c')
    	{
    		 discount = "Cash and Carry";
    		 discountPercent = "10% -- Cash and Carry";
    	}
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')
    	{
        	 discount = "Zippy Discount";                             //set discount variables (output messages)
             discountPercent = "5% -- Zippy Discount";
    	}
       	else if (paymentMethod == 'O' || paymentMethod == 'o')
    	{
    	     discount = "No Discount";
    		 discountPercent = "No Discount";
    	}
    	    
    	else 
    	{
    	     discount = "No payment type specified";
    	     discountPercent = "No Discount";
    	}
    
        
       	
    	cout<<"Enter the Length of your room: ";                      //input room length
    	cin>>roomLength;
    	cout<<endl;
    
    	cout<<"Enter the Width of your room: ";
    	cin>>roomWidth;                                               //input room width
        cout<<endl;
    	cout<<endl;
    	cout<<endl;
        cout<<endl;
    	cout<<endl;
    	cout<<endl;
    
    	roomArea = roomWidth * roomLength;                            //calculate room area
    
        carpetCost = roomArea * rugCost;                              //calculate rug cost
    
        endDiscount = roomArea * rugCost * paidDiscount;              //calculate discount
    
        trueCost = carpetCost - endDiscount;                          //calculate carpet cost minus the discount (truecost)
    
         if (paidDiscount == 1)
    	{
    		trueCost = carpetCost;                                    //set no discount variables
            endDiscount = 0;
    	}
    
        
    	cout<<setw(40)<<">>>Phil's Floors<<<<"<<endl;
    	cout<<endl;                                             //output header
    	cout<<setw(36)<<"Our Rugs Rock"<<endl;
    	cout<<endl;
            
    	cout<<"Customer Name:"<<setw(27)<<customerName<<endl;         //output customer name
    	cout<<endl;
    
    	cout<<"Rug Color:"<<setw(31)<<rugDisplay<<endl;               //output rugcolor
    	cout<<endl;
    
    	cout<<"Payment Method:"<<setw(26)<<discount<<endl;            //output payment method
    	cout<<endl;
    
    	cout<<setw(30)<<discountPercent<<endl;                        //output discount percent message
    	cout<<endl;
    
    	
        
        	
        cout<<setprecision(2)<<"Discount: "<<setw(26)<<"$"<<endDiscount<<endl;           //output discount
    	cout<<endl;
        
    	cout<<setprecision(2)<<"Rug Cost: "<<setw(25)<<"$"<<carpetCost<<endl;            //output carpet cost (before discount)
    
    	cout<<endl;
        
    	cout<<setprecision(2)<<"Final Cost:"<<setw(24)<<"$"<<trueCost<<endl;             //output carpet cost(final cost-after discount)
    	cout<<endl;
    
    
    
    	
    	return 0;
    
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    #include <fstream>
    using namespace std;
    
    int main()
    {
      ofstream fout;
      fout.open("some_file_name");
      fout << "Hello World!";
      return 0;
    }
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    Thanks Josh coupled with the FAQ that made sense to me. The problem I have having now is I am prompting for a filename (which needs to be billing.dat) and it's not creating it under that name. I prompt for the filename then send it to a variable (char filename[15] ) in this case. Also where should I stick ofstream.fout? that usually go up with the variable declarations?
    I am also having format problems with the file:

    Customer Name: customer name

    Rug Color: BLUE

    Payment Method: Cash and Carry

    10% -- Cash and Carry

    Discount: $34

    Rug Cost: $3.4e+002

    Final Cost: $3e+002

    I am getting those funky numbers like it doesn't see cout<<setiosflags(ios::fixed);
    Do I need to stick that down after where I open the file up?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    heres my new code



    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;
    const double BLUE_RUG = 6.99;
    
    
    
    
    
    
    
    int main()
    
    {
        
    	char filename[15];
        string discount;
    	char customerName[25];
    	char rugColor;
        char paymentMethod;
    	int roomLength;
        int roomWidth;                                            //variables
    	string rugDisplay;
    	string discountPercent;
    	int roomArea;
    	double endDiscount;
    	double paidDiscount;
    	double rugCost;
    	double carpetCost;
    	double trueCost;
    
        
     
        cout<<setiosflags(ios::fixed);
    
    	cout<<"Enter file name for billing: ";
    	cin.getline(filename,15);
    
    	
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Phils Floors<<<<"<<endl;                     //business header-output to screen
    	cout<<endl;
    	cout<<setw(36)<<"Our Rugs Rock"<<endl;
    	cout<<endl;
    
    
    	cout<<"If you enter the customers name, type of rug purchased,"<<endl;
        cout<<"room dimensions, and type of payment, this program"<<endl;       //program explaination
        cout<<"will calculate and print a bill to a file, Billing.dat"<<endl;
    	cout<<endl;
    	
    
    	cout<<"Please enter the customers name: ";
    	cin.getline(customerName,25);                                //input customers name
    	cout<<endl;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
        cin>>rugColor;                                               //input rug color
    	cout<<endl;
    
    	if (rugColor == 'R' || rugColor == 'r')
    	{
    		rugDisplay = "RED";
    		rugCost = RED_RUG;
    	}
    	else
    		if (rugColor == 'B' || rugColor == 'b')
    	{
    		rugDisplay = "BLUE";                                     //set rugdisplay variable and cost of rug
    		rugCost = BLUE_RUG;                                          
    	}
    	else
    		if(rugColor == 'G' || rugColor == 'g')
    	{
    		rugDisplay = "GREEN";
    		rugCost = GREEN_RUG;
    	}
        else
    	{
    		rugDisplay = "Default";
    		rugCost = 6.99;
    	}
    	 
        cout<<"Please enter the method of payment"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther: ";
    	cin>>paymentMethod;                                           //input payment method
    	cout<<endl;
     
        if (paymentMethod == 'C' || paymentMethod == 'c')
    	{
    		paidDiscount = .10;
    	}
    
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')        //set paidDiscount variable to use in calculation
    	{
    	    paidDiscount = .05;
    	}
    
    	else 
    	{
    	    paidDiscount = 1;
    	}
    
    
    	if (paymentMethod == 'C' || paymentMethod == 'c')
    	{
    		 discount = "Cash and Carry";
    		 discountPercent = "10% -- Cash and Carry";
    	}
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')
    	{
        	 discount = "Zippy Discount";                             //set discount variables (output messages)
             discountPercent = "5% -- Zippy Discount";
    	}
       	else if (paymentMethod == 'O' || paymentMethod == 'o')
    	{
    	     discount = "No Discount";
    		 discountPercent = "No Discount";
    	}
    	    
    	else 
    	{
    	     discount = "No payment type specified";
    	     discountPercent = "No Discount";
    	}
    
        
       	
    	cout<<"Enter the Length of your room: ";                      //input room length
    	cin>>roomLength;
    	cout<<endl;
    
    	cout<<"Enter the Width of your room: ";
    	cin>>roomWidth;                                               //input room width
        cout<<endl;
    	cout<<endl;
    	cout<<endl;
        cout<<endl;
    	cout<<endl;
    	cout<<endl;
    
    	roomArea = roomWidth * roomLength;                            //calculate room area
    
        carpetCost = roomArea * rugCost;                              //calculate rug cost
    
        endDiscount = roomArea * rugCost * paidDiscount;              //calculate discount
    
        trueCost = carpetCost - endDiscount;                          //calculate carpet cost minus the discount (truecost)
    
         if (paidDiscount == 1)
    	{
    		trueCost = carpetCost;                                    //set "no discount" variables
            endDiscount = 0;
    	}
    
    
            
    	cout<<setw(40)<<">>>Phil's Floors<<<<"<<endl;
    	cout<<endl;                                             //output header
    	cout<<setw(36)<<"Our Rugs Rock"<<endl;
    	cout<<endl;
    
        ofstream fout;
    
        fout.open("filename");
           
    	fout<<"Customer Name:"<<setw(27)<<customerName<<endl;         //output customer name
    	fout<<endl;
    
    	fout<<"Rug Color:"<<setw(31)<<rugDisplay<<endl;               //output rugcolor
    	fout<<endl;
    
    	fout<<"Payment Method:"<<setw(26)<<discount<<endl;            //output payment method
    	fout<<endl;
    
    	fout<<setw(30)<<discountPercent<<endl;                        //output discount percent message
    	fout<<endl;
        	
        fout<<setprecision(2)<<"Discount: "<<setw(26)<<"$"<<endDiscount<<endl;           //output discount
    	fout<<endl;
        
    	fout<<setprecision(2)<<"Rug Cost: "<<setw(25)<<"$"<<carpetCost<<endl;            //output carpet cost (before discount)
    
    	fout<<endl;
        
    	fout<<setprecision(2)<<"Final Cost:"<<setw(24)<<"$"<<trueCost<<endl;             //output carpet cost(final cost-after discount)
    	fout<<endl;
        
    	fout.close();
            
    
    	
    	return 0;

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    bleh spoke too soon I figured out how to format the output......still need a hand with getting the compiler to see my variable as the filename I want to use and where the proper place is to stick ofstream fout; is?

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    spoke too soon again, I figured it out....thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21