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!:cool:

iamthwee commented: getch()? -> cin.get() -2

Recommended Answers

All 16 Replies

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()
 

{[INDENT]FILE *stream;[/INDENT][INDENT]errno_t err;[/INDENT]

[INDENT]if( (err = fopen_s( &stream, "test.txt", "w+" )) != 0 )[INDENT]printf( "The file 'data2' was not opened\n" );[/INDENT]

else[INDENT]printf( "The file 'data2' was opened\n" );
 
[/INDENT]char *names = "Test";
fwrite(names, sizeof(int), 1, stream);
 

if(stream)[INDENT]if(fclose(stream))[INDENT]printf("unable to close file stream");[/INDENT]

[/INDENT]return(0);
 

[/INDENT]}

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

So, can you show me exactly where it goes in my program please

I would put it in another function then call it just before the getch() at the end of main().

Ah yes i forgot about c++'s file IO stuff. I'm so used to using C's which is a pain :) Thx AD

I got it to print! Thanks!

What do you mean, another function

What kind of function

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.

Well, I halfway solved the problem:

This is what I did:

#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.
        ofstream outputFile;
        cout << "\t\tInternet Service Provider\n";
        cout <<  "\t\t  Subscription Packages\n\n";
        outputFile.open("subscriptionbill.txt");
        outputFile << "\t\tInternet Service Provider\n";
        outputFile << "\t\t  Subscription Bill\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;
                outputFile << name <<", your total payment for this month is $" << total <<endl;
                }
                else
                cout << name << ", your total payment for this month is $" << charges << endl;
                outputFile <<name << " , your total payment for this month is $" << 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;
                outputFile << name << ", your total payment for this month is $" << total << endl;
                }
                else
                cout << name << ", your total payment for this month is $" << charges << endl;
                outputFile << 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;
            outputFile << 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";
        }
        outputFile << "\n\n*********************************************\n";
        outputFile <<"\t\nThanks for doing business with us! Have a nice day! ";
        outputFile.close();
        getch();
        return 0;
}

Kinda long, isn't it.:-/ But, I'm a beginner, and I have to take baby steps.

Anyway, the only problem is this:

Internet Service Provider
    Subscription Bill
Nadia McIntosh, your total payment for this month is $11.95
Nadia McIntosh , your total payment for this month is $9.95

*********************************************
 
Thanks for doing business with us! Have a nice day!

Do you see? It outputs both choices, but I can't seem to figure out how to put one particular choice in the file. For example, I did this particular program for subscription choice A. If the person only used 10 hours (which is the limit for that month), then I want written to the file that person's name and their total payment. If they used more than 10 hours, I wanted that to be inputted in the file. I didn't intend for both of them to be written in.

What should I do??

The problem is line 30 -- it should look like this: choice = cin.get(); you also need to initialize hours to zero on line 12 because it can be used before initialized on line 71.

Also, make sure you have braces around everything that needs braces. I think I see a spot where you are missing some.

I checked and I didn't see any braces missing

how about lines 45 and 46?

:) OHMIGOODNESS! It worked! Thanks!!!

Oh, wow, it worked! Thanks so much AncientDragon!! You're a lifesaver

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.