•
•
•
•
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
![]() |
Hi.
Well, I got my subscription program right (thanks for all of the help, everyone!)
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!
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.
If you teach a man to fish, you feed him for a lifetime.
•
•
Join Date: Apr 2007
Posts: 102
Reputation:
Rep Power: 2
Solved Threads: 17
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" );elseprintf( "The file 'data2' was opened\n" );char *names = "Test"; fwrite(names, sizeof(int), 1, stream); if(stream)if(fclose(stream))return(0);printf("unable to close file stream");
Last edited by mariocatch : Apr 19th, 2007 at 6:36 pm.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,174
Reputation:
Rep Power: 38
Solved Threads: 930
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,174
Reputation:
Rep Power: 38
Solved Threads: 930
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,174
Reputation:
Rep Power: 38
Solved Threads: 930
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Get data out of excel file stored as an image (MS SQL)
- Writing data to a file. (C++)
- Listview Data to XML or file (VB.NET)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- I keep writing zero's, not my data using fprintf (C)
- C++ raster files (writing structs to a binary file) (C++)
- empty or delete data index file in temp internet files (Windows NT / 2000 / XP / 2003)
Other Threads in the C Forum
- Previous Thread: How to overload + operator for STL Vectors
- Next Thread: Need Help With Some Programs



Linear Mode