At wit's end-Beginner problem

Apr 14, 2013 at 3:37pm
I have solved some of this problem but not all of it and have spent so much time already my laptop will be reduced to trash.

C++ program that prompts the user to enter a person's name and a telephone number.
Store both of these items into a file named "phonebook.txt". The program should loop until the user enters "quit" for the name. The name should be stored as First Last. The phone number should be stored as (734) 555-1212. Display the contents of the file when the user enters quit.

Can't figure this out at all.
Apr 14, 2013 at 3:52pm
You say you have solved some of it. Show the code. (in proper tags)
Apr 14, 2013 at 3:53pm
yes. show it before ask.

and quit is mean user input quit or

by cin.eof() quit? different type
and i think got people ask same question..
Apr 14, 2013 at 4:01pm
This is what I have so far. And I only have this because I had help from Vlad from Moscow. I am a beginning student and haven't even been exposed to some of this programming but this code gets me close to what I want. I need only now to display the number into the text file in the proper format.
Example: (734) 555-7565
I would also like to upon quiting the program display the file.


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
//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;
}
Apr 14, 2013 at 4:04pm
so what's your problem now?
Apr 14, 2013 at 4:16pm
Want to format the phone number to the text file as (734) 444-3456
Apr 15, 2013 at 1:47am
what i do for this is quite complicated.
and something like this

maybe other senior might get easier solution
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
#include <iostream>
#include <string>

using namespace std;

int main(){

	string phone = "7344443456";
	char *newphone;
	newphone = new char;

	for( int i = 0 ; i < phone.size()+3 ; i++ ){
		if( i == 0 ){
			newphone[i] = '(';
		}
		else if( i < 4 && i > 0 ){
			newphone[i] = phone[i-1];
		}
		else if( i == 4 ){
			newphone[i] = ')';
		}
		else if( i > 4 && i < 8 ){
			newphone[i] = phone[i-2];
		}
		else if( i == 8 ){
			newphone[i] = '-';
		}
		else{
			newphone[i] = phone[i-3];
		}
	}

	cout << newphone << endl;

	system( "pause" );
	return 0;
}


keep use else if and else to compare the index
Topic archived. No new replies allowed.