Another making a class problem

So I've been turning my programs into classes and I run into errors.



So this program is supposed to allow the user to open the file and either read or write to it.

I've omitted the read part from it as I want to attempt that on my own if I can get help on the write. Should be a similar problem.

I get these compile errors:

fileclass.cpp:13: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp:21: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp: In member function ‘int file::choice()’:
fileclass.cpp:36: error: cannot convert ‘std::string’ to ‘int’ for argument ‘1’ to ‘ssize_t read(int, void*, size_t)’
fileclass.cpp: In member function ‘void file::write(std::string)’:
fileclass.cpp:53: error: ‘myfile’ was not declared in this scope
fileclass.cpp:55: error: ‘myfile’ was not declared in this scope
fileclass.cpp:56: error: return-statement with a value, in function returning 'void'
fileclass.cpp: In function ‘int main()’:
fileclass.cpp:80: error: ‘choice’ was not declared in this scope
Last edited on
Line 11 choice(); you need a type void choice();
same on line 16

and your write method is defined a void but you have it returning a value on line 53.
Last edited on
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
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>

using namespace std;

class file
{
	public:
	void choice();
	int write(string fn);
	int read(string fn);
	//void read (string fn);
};

void file::choice()
{
	string filename;
	cout << "Enter the file name: ";
	cin>>filename;
	ofstream myfile( filename.c_str(), ios::app);
	//c = choice
	char c;
	cout << " Enter w to write or r if you want to read from the file: ";
	cin >> c;

	if(c == 'w' || c == 'W')
	write(filename);

	else if (c == 'r' || c == 'R')
	read(filename);

	else
		cout<< "Choice not found" << endl;
}

int file::write(string fn)
{
	ofstream myfile;
	//myfile.open(fn);
			int x;
		cout << "How many numbers would you like to add? ";
		cin >> x;

			for(int i=0; i < x; i++)
			{
				double text;
				cout << "Enter number to add: ";
				cin >> text;
				myfile << text << endl;
			}
		myfile.close();
		return 0;
}


int main()
{
    

	choice();
	return 0;

}




Check this one it works!!!!!!
Last edited on
@ Danyal Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/
closed account (j3Rz8vqX)
The source code provided and the compilation posted are not coherent; understandable, due to the exclusion of the read() method.
The errors are self explanatory:
fileclass.cpp: In member function ‘void file::write(std::string)’:
fileclass.cpp:53: error: ‘myfile’ was not declared in this scope
fileclass.cpp:55: error: ‘myfile’ was not declared in this scope
fileclass.cpp:56: error: return-statement with a value, in function returning 'void'
http://www.cplusplus.com/doc/tutorial/files/
Remove the "return 0;" in this method, since it isn't relevant.

Also:
You're operating the "choice" function from the scope of class "file" incorrectly:
(supposedly line 13, during declaration and line 21, during definition)
http://www.cplusplus.com/doc/tutorial/classes/
1
2
3
4
5
6
7
8
9
10
11
class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area() {return width*height;}
};

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}


You would declare the object file in main, and call it's functions with the dot(.) operator:
1
2
3
4
5
6
int main () {
  Rectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}

Have fun.
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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

class file
{
	public:
	void choice();
	int write(string fn);
	int read (string fn);
	
	private:
	
};

void file::choice()
{
	string filename;
	cout << "Enter the file name: ";
	cin >> filename;
	ofstream myfile( filename.c_str(), ios::app);
	//c = choice
	char c;
	cout << " Enter w to write or r if you want to read from the file: ";
	cin >> c;

	if(c == 'w' || c == 'W')
	write(filename);

	else if (c == 'r' || c == 'R')
	read(filename);

	else 
		cout<< "Choice not found" << endl;
}

int file::write(string fn)
{
		ofstream myfile;
		int x;
		cout << "How many numbers would you like to add? ";
		cin >> x;

			for(int i=0; i < x; i++)
			{
				double text;
				cout << "Enter number to add: ";
				cin >> text;
				myfile << text << endl;	
			}
		myfile.close();
		return 0;
}

int file::read(string fn)
{
		{
		  string line;
		  ifstream myfile;
		  if (myfile.is_open())
		  {
		    while ( myfile.good() )
		    {
		      getline (myfile,line);
		      cout << line << endl;
		    }
		    myfile.close();
  		}

		return 0;
		}
}

int main()
{
	choice();
	return 0;

}


I made all the suggested changes but I still get this error:

fileclass.cpp: In function ‘int main()’:
fileclass.cpp:81: error: ‘choice’ was not declared in this scope
hey you are including
 
#include<string> 


istead of
 
#include<cstring> 
1
2
3
4
5
6
7
int main()
{
    file a;
    a.choice();
    return 0;

}
Last edited on
Thanks everything worked but as I ran and compiled it I noticed that my program doesn't read or write to the file of my choice any more.

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
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>

using namespace std;

class file
{
	public:
	void choice();
	int write(string fn);
	int read (string fn);
	
	private:
	
};

void file::choice()
{
	string filename;
	cout << "Enter the file name: ";
	cin >> filename;
	ofstream myfile( filename.c_str(), ios::app);
	//c = choice
	char c;
	cout << " Enter w to write or r if you want to read from the file: ";
	cin >> c;

	if(c == 'w' || c == 'W')
	write(filename);

	else if (c == 'r' || c == 'R')
	read(filename);

	else 
		cout<< "Choice not found" << endl;
}

int file::write(string fn)
{
		ofstream myfile;
		int x;
		cout << "How many numbers would you like to add? ";
		cin >> x;

			for(int i=0; i < x; i++)
			{
				double text;
				cout << "Enter number to add: ";
				cin >> text;
				myfile << text << endl;	
			}
		myfile.close();
		
}

int file::read(string fn)
{
		{
		  string line;
		  ifstream myfile;
		  if (myfile.is_open())
		  {
		    while ( myfile.good() )
		    {
		      getline (myfile,line);
		      cout << line << endl;
		    }
		    myfile.close();
  		}

		
		}
}

int main()
{
	file a;
	a.choice();
	//return 0;

}


My current code.

Also say I want to open a file named "file.txt". This doesn't read, or write to that file and after running it the file remains unchanged.
Last edited on
closed account (j3Rz8vqX)
http://www.cplusplus.com/doc/tutorial/files/
myfile.open(fn.c_str());

Insert it after the declaration of myfile; should be twice in your program.
Last edited on
you never opened the file
also you wan't the string header file not cstring
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
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

class file
{
	public:
	void choice();
	int write(string fn);
	int read (string fn);

	private:

};

void file::choice()
{
	string filename;
	cout << "Enter the file name: ";
	cin >> filename;
	ofstream myfile( filename.c_str(), ios::app);
	//c = choice
	char c;
	cout << " Enter w to write or r if you want to read from the file: ";
	cin >> c;

	if(c == 'w' || c == 'W')
	write(filename);

	else if (c == 'r' || c == 'R')
	read(filename);

	else
		cout<< "Choice not found" << endl;
}

int file::write(string fn)
{
    ofstream myfile;
    myfile.open(fn.c_str(), std::ofstream::out | std::ofstream::app);
    if(!myfile)
    {
        cout << "File failed to open" << flush;
        cin.ignore();
        exit(1);
    }
    int x;
    cout << "How many numbers would you like to add? ";
    cin >> x;

    for(int i=0; i < x; i++)
    {
        double text;
        cout << "Enter number to add: ";
        cin >> text;
        myfile << text << endl;
    }
    myfile.close();

}

int file::read(string fn)
{

    string line;
    ifstream myfile;
    myfile.open(fn.c_str(), std::ofstream::out | std::ofstream::app);
    if(!myfile)
    {
        cout << "File failed to open" << flush;
        cin.ignore();
        exit(1);
    }
    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line);
            cout << line << endl;
        }

    }
     myfile.close();

}


int main()
{
	file a;
	a.choice();
	//return 0;

}
Topic archived. No new replies allowed.