1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// File stream object
fstream dataFile;
// Function Prototypes
int firstUserInput(fstream &dataFile);
int SecondInputandDisplay(fstream &dataFile);
// Variables
string name, address, city, state, zipCode, accountBalance, lastPayment, phoneNu, emailAddress;
int main()
{
// Calling functions
firstUserInput(dataFile);
SecondInputandDisplay(dataFile);
return 0;
}
// Definition for firstUserInput **********************************************
// ****************************************************************************
int firstUserInput(fstream &dataFile)
{
// Opens the file
dataFile.open("C:\\Users\\Brittney\\Documents\\College Courses\\C++ 2\\Chapter 12\\Test\\userInput.txt", ios::in | ios::out);
// Shows error message if the file didn't open correctly.
if (dataFile.fail())
{
cout << "Error! The file did not open correctly." << endl;
return 1;
}
// Prompts user to input data
cout << "Hello, I will be opening a file and prompting you to enter your information." << endl;
cout << "Please enter your name" << "\n";
getline(cin, name);
cout << "Please enter your street address" << "\n";
getline(cin, address);
cout << "Please enter your city" << "\n";
getline(cin, city);
cout << "Please enter your state" << "\n";
getline(cin, state);
cout << "Please enter your zip code" << "\n";
getline(cin, zipCode);
cout << "Please enter your account balance" << "\n";
getline(cin, accountBalance);
cout << "Please enter the last date of your payment" << "\n";
getline(cin, lastPayment);
// Saves the data.
dataFile << name;
dataFile << address;
dataFile << city;
dataFile << state;
dataFile << zipCode;
dataFile << accountBalance;
dataFile << lastPayment;
cout << endl << "Saving your information on the file..." << endl;
// CLoses the file
dataFile.close();
}
// Definition for secondInputandDisplay ***************************************
// ****************************************************************************
int SecondInputandDisplay(fstream &dataFile)
{
// Reopens the file for more input
dataFile.open("C:\\Users\\Brittney\\Documents\\College Courses\\C++ 2\\Chapter 12\\Test\\userInput.txt", ios::in | ios::app);
// Shows error message if the file didn't open correctly.
if (dataFile.fail())
{
cout << "Error! The file did not open correctly." << endl;
return 1;
}
// Prompts user to add more input
cout << endl << "Hello, I have reopened the file and will be asking information one more time." << endl;
cout << "Please enter your phone number" << "\n";
getline(cin, phoneNu);
cout << "Please enter your email address" << "\n";
getline(cin, emailAddress);
// Saves the data.
dataFile << phoneNu;
dataFile << emailAddress;
cout << endl << "Saving your information again..." << endl;
// CLoses the file
dataFile.close();
// Displays results
cout << "-------------------------------------------------" << endl;
cout << endl << "Here is all your information on the file." << endl;
cout << endl << "Your name: " << name << "\n";
cout << "Your address: " << address << "\n";
cout << "Your city: " << city << "\n";
cout << "Your state: " << state << "\n";
cout << "Your zip code: " << zipCode << "\n";
cout << "Your account balance: " << accountBalance << "\n";
cout << "Your last payment: " << lastPayment << "\n";
cout << "Your phone number: " << phoneNu << "\n";
cout << "Your email address: " << emailAddress << "\n";
}
|