Apr 14, 2013 at 10:27am Apr 14, 2013 at 10:27am UTC
you can use
while (strcmp (firstName,"quit" ) != 0 ) //remove semicolon from here in your code
Apr 14, 2013 at 10:35am Apr 14, 2013 at 10:35am UTC
Without testing:
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
//Writing data to a file.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cctype>
using namespace std;
int main()
{
ofstream outputFile;
outputFile.open("phonebook.txt" );
while ( outputFile )
{
const char quit[] = "QUIT" ;
const size_t QUIT_SIZE = sizeof ( quit ) -1;
string firstName;
string lastName;
string phoneNumber;
cout << "Enter your first name: " << endl;
cin >> firstName;
if ( firstName.size() == QUIT_SIZE &&
equal( firstName.begin(), firstName.end(), quit, []( char a, char b ) { return ( toupper(a ) == b ); } ) )
{
break ;
}
cout << "Enter your last name: " << endl;
cin >> lastName;
cout << "Enter the telephone number: " << endl;
cin >> phoneNumber;
cout << "The numbers were saved to a file. \n" ;
//Write the names and phone number to a file.
outputFile << firstName << " " << lastName << " " << phoneNumber << endl;
}
//Close the file.
outputFile.close();
cout << "Done. \n" ;
system ("pause" );
return 0;
}
EDIT: By the way in your original code there is a serious bug: presence of a semicolon after while
while ( firstName != quit )
;
Last edited on Apr 14, 2013 at 10:38am Apr 14, 2013 at 10:38am UTC
Apr 14, 2013 at 1:07pm Apr 14, 2013 at 1:07pm UTC
I have 2 more quick questions. How can I format the number in the text file in the fashion as follows. (333) 565-4444
Also upon the user entering quit and the loop shutting down is it possible to display the contents of the file on the console.
Thank you for any help provided.