I/O File

Hi,
This project is for school, so error spotting only please. No solutions.
I'm trying to create a simple contact book in a text file with a console application. I've tried using headers iostream, fstream and ostream in every file just be sure that wasn't the problem, it still doesn't work.
This is what comes up in the error list after attempting to compile:
"1>------ Build started: Project: c++2_Final, Configuration: Debug Win32 ------
1> c++2_Final.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Contact.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="


Here is my code:
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
//Contact.h
#pragma once
class Contact
{
public:
	string name, phone, address, email;
	ofstream myFile;
	
	void writeFile();
};

//Contact.cpp
void  Contact::writeFile()
{
	myFile.open(name + ".txt");
	myFile << "Name:" + name << endl;
	myFile << "Phone number:" + phone << endl;
	myFile << "Address:" + address << endl;
	myFile << "Email address:" + email << endl;
	myFile.close();
}

//main.cpp
int _tmain(int argc, _TCHAR* argv[])
{
		
		vector<Contact> contact(10);		
		bool eof = false;
		string checkAddContact;
		int contactNumber = 0;

	 while(eof == false)
	 {
		cout << "Please enter name:" << endl;
		cin >> contact[contactNumber].name;
		cout << "Please enter phone:" << endl;
		cin >> contact[contactNumber].phone;
		cout << "Please enter address:" << endl;
		cin >> contact[contactNumber].address;
		cout << "Please enter email:" << endl;
		cin >> contact[contactNumber].email;
		contact[contactNumber].writeFile();
		cout << "Would you like to enter another contact? y/n" << endl;
		cin >> checkAddContact;
		contactNumber++;
		if(contactNumber > 9)
		{
			eof = true;
		}
		if(checkAddContact == "y")
		{
			eof = false;
		}
		else
		{		
			eof = true;
		}
	 }
	 
	return 0;
}




Ok couple of things, some are personal suggestions others are coding standards.

Personal suggestion: Ditch Microsoft Visual C editor... [ found it a might annoying] but you can get a free C++ IDE at bloodshed.org called Dev-Cpp. It is what I personally use and I think it will return a more useful error code. [I couldn't make head-or-tails out of the one you posted sorry. :( ]

Coding standard: Usually all variables are declared as private in a class, so as to avoid unwanted manipulation of said variables.

Now as to possible problems in your code, the first which jumped at me was the order of your if-if-else checks.

Your first if needs to be your second, for this reason. As the program executes, if your exceed the 9th address of your vector you are out of space and cannot continue even if the user says go on. Currently your user saying go on will override your memory exceeded check.

Otherwise the problem seems to be in your ofstream variable. Try this:

// code
...
string = name + ".txt";
myFile.open(string);
...
//code


I hope this at least gets you something to try, if not fixes it. Good Luck!
you can get a free C++ IDE at bloodshed.org called Dev-Cpp


some people might flame you because of that comment, since Dev-Cpp hasn't been updated in ages. :P
Yep I'm going to flame you, that was a dumb comment
1) declaring the variable as private and function as public
2) Declaring and defining constructor , default constructor is called but it is good habit in school to declare and define the constructor .
3) using of vector<> is not required in this can you can use array of the class .

1
2
3
4
5
6
7
8
9
10
11
12
13
string m_strTemp;
                 cout << "Please enter name:" << endl;
		 cin >> m_strTemp;
                 contact[contactNumber].name = m_strTemp;
		 cout << "Please enter phone:" << endl;
		 cin >> m_strTemp;
                 contact[contactNumber].phone = m_strTemp;
		 cout << "Please enter address:" << endl;
		 cin >> m_strTemp ;
                 contact[contactNumber].address = m_strTemp;
		 cout << "Please enter email:" << endl;
                 cin >> m_strTemp;
                 contact[contactNumber].email = m_strTemp;



1
2
3
4
5
6
7
 cout << "Would you like to enter another contact? y/n" << endl;
		cin >> checkAddContact;
		contactNumber++;
		if(contactNumber > 9 && checkAddContact == 'n')
			eof = true;
                else
		  	eof = false;



1
2
   name = name + ".txt";
                myFile.open(name .c_str());
Last edited on
@bluecoder

Yes, I would use an array if I wanted anything less than an A on this assignment. :P
I need to include a vector in this program. Thank you very much for your response.
After some modification, I'm still getting some error. It looks like I'm illegally using the "=" operator somewhere with the I/O Stream. Or something wrong with my stream declaration?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
1>  c++2_Final.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Here is my modified code:
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
//Contact.h
#pragma once
class Contact
{
public:
	void writeFile(void);
	void setName(string assignName);
	void setPhone(string assignPhone);
	void setAddress(string assignAddress);
	void setEmail(string assignEmail);

private:
	string name, phone, address, email;
	ofstream myFile;
};

//Contact.cpp
void Contact::writeFile()
{
	name = name + ".txt";
    myFile.open(name .c_str());
	myFile << "Name:" + name << endl;
	myFile << "Phone number:" + phone << endl;
	myFile << "Address:" + address << endl;
	myFile << "Email address:" + email << endl;
	myFile.close();
}

void Contact::setName(string assignName)
{
	name = assignName;
}

void Contact::setPhone(string assignPhone)
{
	phone = assignPhone;
}

void Contact::setAddress(string assignAddress)
{
	address = assignAddress;
}

void Contact::setEmail(string assignEmail)
{
	email = assignEmail;
}

//main.cpp
int _tmain(int argc, _TCHAR* argv[])
{
		
		vector<Contact> contact(10);		
		bool eof = false;
		string checkAddContact, tempString;
		int contactNumber = 0;

	 while(eof == false)
	 {
		cout << "Please enter name:" << endl;
		cin >> tempString;
		contact[contactNumber].setName(tempString);
		cout << "Please enter phone:" << endl;
		cin >> tempString;
		contact[contactNumber].setPhone(tempString);
		cout << "Please enter address:" << endl;
		cin >> tempString;
		contact[contactNumber].setAddress(tempString);
		cout << "Please enter email:" << endl;
		cin >> tempString;
		contact[contactNumber].setEmail(tempString);
		contact[contactNumber].writeFile();
		cout << "Would you like to enter another contact? y/n" << endl;
		cin >> checkAddContact;
		contactNumber++;
		if(contactNumber > 9 || checkAddContact == "n")
		{
			eof = true;
		} 
		else
		{
		  	eof = false;
		}
	 }
	 
	 
	return 0;
}
Nevermind, I got it. It seems inefficient, but it works if I declared the stream object in the writeFile function.

Thanks.
Topic archived. No new replies allowed.