Thread: Writing to a file

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    22

    Writing to a file

    I am doing a challenge that asks the user a name and telephone number and then stores it in a file. This is what I have so far:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cctype>
    using namespace std;
    
    int main()
    {
    	fstream dataFile("phones.dat", ios::out);
    	if (!dataFile)
    	{
    		cout << "Error opening the file.";
    		return 0;
    	}
    	const int NAME_LENGTH = 81;
    	const int PHONE_LENGTH = 26;
    	char name[NAME_LENGTH], phone[PHONE_LENGTH];
    
    	cout << "This program allows you to add names and phone\n";
    	cout << "numbers to phones.dat.\n";
    	do
    	{
    		char add;
    		cout << "Do you wish to add an entry? ";
    		cin >> add;
    		if (toupper(add) == 'Y')
    		{
    			cout << "Pleas enter a name and phone number: " << endl;
    			cin.get(name, phone);
    			dataFile.put(name, phone);
    		}
    	} while (toupper(add) == 'Y');
    	dataFile.close();
    	return 0;
    }
    I just need help finishing it up.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What problem are you having?

    >> cin.get(name, phone);
    This line looks suspicious. Do you know how to use cin.get()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM