User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,174 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,845 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3022 | Replies: 16
Reply
Join Date: Apr 2007
Location: Mississippi
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Help Writing data into a file

  #1  
Apr 19th, 2007
Hi.

Well, I got my subscription program right (thanks for all of the help, everyone!)

 
#include <iostream>
#include <iomanip>      //Needed for the showpoint and setprecison command
#include <fstream>      //Needed to use files
#include <string>       //Needed to use string variable
#include <conio>
using namespace std;
int main()
{
        char choice;
        string name;
        int hours;
        double charges, total;
 // Displays the menu choices  on the screen.
        cout << "\t\tInternet Service Provider\n";
        cout << "\t\t  Subscription Packages\n\n";
 cout << "A: For $9.95 per month, can get 10 hours of\n";
        cout << "   access. Additional hours are $2.00 per hour. \n\n" ;
        cout << "B: For $14.95 per month, you can get 20 hours\n";
        cout << "   of access. Additonal hours are $1.00 per hour. \n\n";
        cout << "C: For $19.95 per month unlimited access is provided.\n\n";
        cout << "Please enter your name. ";
        getline (cin, name);
        cout << "Which subscription package would you like? ";
        cin.get(choice);
        cout << fixed <<showpoint <<setprecision(2);
 

        if (choice == 'A' || choice == 'a')
        {
                cout << "How many hours did you use for this month? ";
                cin  >> hours;
                charges = 9.95;
                if (hours >=10)
                {
                total = charges + ((hours - 10) * 2.00);
                cout << name <<", your total payment for this month is $" << total <<endl;
                }
                else cout <<name << " , your total payment for this month is $";
                cout << charges << endl;
        }
        else if (choice == 'B' || choice == 'b')
        {
                cout << "How many hours did you use for this month? ";
                cin  >> hours;
                charges = 14.95;
                if (hours >=20)
                {
                total = charges + ((hours - 20) * 1.00) ;
                cout << name << ", your total payment for this month is $" << total << endl;
                }
                else
                cout << name << ", your total payment for this month is $" << charges << endl;
         }
        else if (choice == 'C' || choice == 'c')
        {
            charges  = 19.95;
            cout << name << ", your total payment for this month is $" << charges << endl;
        }
        else if
         (choice !='C' && choice != 'c' && hours >744)
        {       cout << "You must choose packages A, B, or C. Also, your hours\n";
                cout << "must not exceed 744.\n";
        }
        getch();
        return 0;
}
 

The only problem that I am having now is writing the information to a file called subscriptionbill.txt. I would like the following in the program:
(1) The header
(2) The subscription that the person chose
(3) Their monthly bill
(4) The statement: "Thanks for doing business with us!"

Can anyone help me out or at least direct me to a good tutorial that could point me in the right direction when it comes to writing files?

Thanks!
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Posts: 102
Reputation: mariocatch is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Writing data into a file

  #2  
Apr 19th, 2007
Basically just wrote the following by looking at the MSDN. Not sure if it's what you need but it creates a file in the directory of the project called "Test.txt", and writes the char* to it which is initialized to "Test".

#include<iostream>
 
usingnamespace std;
#include<fstream>
#include<istream>
int main()
 

{
FILE *stream;
errno_t err;
if( (err = fopen_s( &stream, "test.txt", "w+" )) != 0 )
printf( "The file 'data2' was not opened\n" );
else
printf( "The file 'data2' was opened\n" );
char *names = "Test"; fwrite(names, sizeof(int), 1, stream); if(stream)
if(fclose(stream))
printf("unable to close file stream");
return(0);
}
Last edited by mariocatch : Apr 19th, 2007 at 6:36 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,174
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Writing data into a file

  #3  
Apr 19th, 2007
Since you have a c++ program use c++ fstream class instead of C's FILE and associated functions. This is not very complicated to do, it just takes a little planning on what you want the file contents to look like.
float bill = 12.32; // $12.32
ofstream out("subscriptionbill.txt");
out << "This is the header\n";
out << "Monthly bill: $" << bill << "\n";
// other stuff here
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2007
Location: Mississippi
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Writing data into a file

  #4  
Apr 19th, 2007
So, can you show me exactly where it goes in my program please
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,174
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Writing data into a file

  #5  
Apr 19th, 2007
I would put it in another function then call it just before the getch() at the end of main().
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2007
Posts: 102
Reputation: mariocatch is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Writing data into a file

  #6  
Apr 20th, 2007
Ah yes i forgot about c++'s file IO stuff. I'm so used to using C's which is a pain Thx AD
Reply With Quote  
Join Date: Apr 2007
Location: Mississippi
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Writing data into a file

  #7  
Apr 20th, 2007
I got it to print! Thanks!
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote  
Join Date: Apr 2007
Location: Mississippi
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Writing data into a file

  #8  
Apr 22nd, 2007
What do you mean, another function
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote  
Join Date: Apr 2007
Location: Mississippi
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Writing data into a file

  #9  
Apr 22nd, 2007
What kind of function
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,174
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Writing data into a file

  #10  
Apr 22nd, 2007
Originally Posted by naya22 View Post
What kind of function


it doesn't matter -- just write a function and put all that print statements in it. That way you can call it as many times as desired or from any place. Writing functions is not difficult at all and makes your program a great deal simpler.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC