Hey folks, this is my first program and i dont know what to do to make it output my imput to a .txt file, can anybody tell me or show me what im doing wrong. thanks.

/*MuTech Computers Address Book by Michael Grayson.*/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() { 
		 cout << "Welcome To The MuTech Address Book.\nHere You Can Store All Your Friends Infomation.";
		 cout << "\n\nPlease Imput The Relevant Infomation";
		 ofstream address;
		 address.open ("Address'.txt");
			string name;
				string home;
					string mobile;
						string address0; 
							string postcode; 
								string email; 
									string notes;
		 cout << "Contacts Name: ";
			getline (cin, name);
		 cout << "\nContacts Home Phone Number: ";
			getline (cin, home);
		 cout << "\nContacts Mobile Phone Number: ";
			getline (cin, mobile);
		 cout << "\nContacts House Address: ";
			getline (cin, address0);
		 cout << "\nContacts Postcode: ";
			getline (cin, postcode);
		 cout << "\nContacts Email: ";
			getline (cin, email);
		 cout << "\nNotes: ";
			getline (cin, notes);
        ofstream myfile ("Addressbook.txt");
        if (myfile.is_open()){
		                       myfile.write << name << "\n" << home << "\n" << mobile << "\n" << adress0;
		                       myfile.write << "\n" << postcode << "\n" << email << "\n" << notes;
		                       myfile.close();
                             { 
        else 
        cout << "Unable to open file";
        return 0;
		   }

Recommended Answers

All 3 Replies

myfile.write << name << "\n" << home << "\n" << mobile << "\n" << adress0;
		                       myfile.write << "\n" << postcode << "\n" << email << "\n" << notes;
		                       myfile.close();
                             { 
        else 
        cout << "Unable to open file";
        return 0;
		   }

Remove the .write and then try the program.

And By the way, The indentation is not formal and is pretty confusing too . So i guess you should work a little on that,

Hey thanks for the imput, i have since reviewed my code and hope it is easyier to understand, however im still having trouble getting my program to compile.

/*MuTech Computers Address Book by Michael Grayson.*/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() { 
		 cout << "Welcome To The MuTech Address Book.\nHere You Can Store All Your Friends Infomation.";
		 cout << "\n\nPlease Imput The Relevant Infomation";
		 ofstream address;
		 address.open ("Address'.txt");
			string name;
			string home;
			string mobile;
			string address0; 
			string postcode; 
			string email; 
			string notes;
		 cout << "Contacts Name: ";
			getline (cin, name);
		 cout << "\nContacts Home Phone Number: ";
			getline (cin, home);
		 cout << "\nContacts Mobile Phone Number: ";
			getline (cin, mobile);
		 cout << "\nContacts House Address: ";
			getline (cin, address0);
		 cout << "\nContacts Postcode: ";
			getline (cin, postcode);
		 cout << "\nContacts Email: ";
			getline (cin, email);
		 cout << "\nNotes: ";
			getline (cin, notes);
        ofstream myfile ("Addressbook.txt");
        if (myfile.is_open()){
               myfile << name << "\n" << home << "\n" << mobile << "\n" << adress0;
		       myfile << "\n" << postcode << "\n" << email << "\n" << notes;
		       myfile.close();
                             { 
        else 
               cout << "Unable to open file";
        return 0;
		   }

Any help would be much appriciated :)

myfile.close();
                             {

That should be a closing brace '}'

myfile << name << "\n" << home << "\n" << mobile << "\n" << adress0;

The spelling of address0 is wrong..... in the above line. Correct them and the code works well :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.