I am writing a program for class and i am stumped on file part. in the program I need to Write a program that asks for user input such as name, hours of internet usage, and hours the internet was used. It should then create a bill that includes the user input and the total due. Then write it to a file.
I understand and can do everything but the file part. Here is what I have so far. This program is due November 5, 2013. by 5:00pm. All help will be appreciated.
[code]
#include <iostream>
#include <string> // needed for strings.
#include <fstream> // needed to use files.
using namespace std;
/*******************************************************************
* Second Program : Internet Service provider Part I- will calculate*
* the bill for a customer based on their package choice. Also uses *
* a file. *
*******************************************************************/
int main()
{
string first, last;
char package, again;
double hours, bill;
do
{
cout << "Welcome to Your Internet Service." << endl;
cout << endl;
// Asking for user input of first and last name.
cout << "Enter your first name: ";
inputFile >> first;
cout << "Enter Your last name: ";
inputFile >> last;
cout << "How many hours were used?";
inputFile >> hours;
// Menu display for the packages offered.
cout << "Please choose one of the following: " << endl;
cout << endl;
cout << "Package A : For $9.95 per month 10 hours of access";
cout << " are provided. Additional hours are $2.00 per hour." << endl;
cout << endl;
cout << "Package B: For $14.95 per month 20 hours of access";
cout << " are provided. Additional hours are $1.00 per hour." << endl;
cout << endl;
cout << "Package C: For $19.95 per month unlimited accesss is provided." << endl;
cout << endl;
//Asks user to input which package they want.
cout << "Please choose a package: (Please use all Capital letters)";
inputFile >> package;
//Switch statement that has case a, b c.
switch (package)
{
case 'a':
case 'A': bill = (hours - 10) * 2.00 + 9.95;
break;
case 'b':
case 'B': bill = (hours - 20) * 1.00 + 14.95;
break;
case 'c':
case 'C': bill = 19.95;
break;
default : cout << "You must Choose A, B, or C.\n";
}
// Clears the screen
system("pause");
system("cls");
outputFile << "Your Internet Provide" << endl;
outputFile << first << " " << last << endl;
outputFile << "You owe: $" << bill;
cout << endl;
//CLOSE THE FILE
outputFile.close();
inputFile.close();
// Clears the screen
system("pause");
return 0;
}
Do you really need inputFile? I thought from the description that the input should come from cin. In any case, creating an output file with the same filename would create a brand-new file and wipe out any previous file.
This program is due November 5, 20123. by 5:00pm.
You're lucky. That gives you over 18000 years to complete the project. With any luck C++ will be obsolete by then. :)
I don't know what I need for my file that is why I am asking and the project is do November 5, 2013( i changed it). This is the first time I have had to make a program write to a file, usually our programs output to the screen only. If you could show what is wrong and tell me whyt i is wrong and how to fix it, that would be very helpful. That way I can learn and will not make the same mistake again.
in the program I need to Write a program that asks for user input such as name,
I think that should look something like this:
1 2 3
// Asking for user input of first and last name.
cout << "Enter your first name: ";
cin >> first;
However, for some reason which I don't think makes sense, rather than cin your version has inputFile.
From the description given, the program needs to create an output file. So yes, ofstream outputFile; is needed. However, I think you should remove all references to ifstream inputFile; and just use cin instead.
thats why i changed it I did get a laugh out of your comment though....lol. I will do you as you suggest but I may have a few questions if it works...lol.
so every thing works except displaying on the screen. the info display on the screen. And How do I know for sure that the information went to the file. I have read that section in the book over and over again and I am still not quite sure how it all works. Do I keep this part
outputFile << "Your Internet Provide" << endl;
outputFile << first << " " << last << endl;
outputFile << "You owe: $" << bill;
as is, leave it out, add cout statements with the same info, or take it out and add the cout statements.
How do I know for sure that the information went to the file
There are two distinct parts to the answer here. Firstly, you can take an ordinary text editor and open the file after the program has ended, to see if it looks correct. But that's only half the answer.
Say you do this:
Well, the code looks ok, but how do you know what actually happened? You could add a simple check on the status of the file at the end.
1 2 3 4
if (outputFile.good())
cout << "Output was created successfully" << endl;
else
cout << "There was a problem with the output file" << endl;
Quote:
Do I keep this part
1 2 3
outputFile << "Your Internet Provide" << endl;
outputFile << first << " " << last << endl;
outputFile << "You owe: $" << bill;
as is, leave it out, add cout statements with the same info, or take it out and add the cout statements.
Well, you should definitely keep that part.
Referring back to your original question, it reads:
It should then create a bill that includes the user input and the total due. Then write it to a file.
Now if that is the exact wording of the question, it is slightly ambiguous. It doesn't actually say that the bill should be displayed on the screen. But it doesn't say that it should not be displayed either.
As a suggestion only, I would say, add cout statements which are in effect duplicating the file output, so you get the same information displayed on the screen too.