Why do these two consecutive loops cause a segfault?

Why do these two consecutive for loops cause a segfault but one works fine on its own and both functions work fine in one loop. Please help I want to be able to cycle through the objects a second time to run another function. Thanks.

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

using namespace std;

	class text
	{
		public:
		string address = addr;
		string getaddr(int);
		string putaddr();
		private:
		string addr;
	};
	
	string text :: getaddr(int i)
	{
		cout <<"Enter the 1st line of the " << i+1 << " address\n";
	
	getline(cin,address);
	}
	
	string text::putaddr()
	{
		cout << "You entered " << address << "\n";
	}
	
	
	
int main()
{
	int size = 3;
	
	text obj [size];
	
	for(int i = 0; i<size; i++)
	{
		obj[i].getaddr(i);
	}
	
		
	for(int i = 0; i<size; i++)
	{
		obj[i].putaddr();
	}
	
Last edited on
Your functions getaddr and putaddr say they return strings. They don't.
Both of these functions are broken:

1
2
3
4
5
6
string text :: getaddr(int i)
{
    cout <<"Enter the 1st line of the " << i+1 << " address\n";

    getline(cin,address);
}


1
2
3
4
string text::putaddr()
{
    cout << "You entered " << address << "\n";
}


Each function declares that it will return a value of type std::string. But it does not return anything.

Probably in this case the solution is to change the return type to void instead of string. (remember to change both the class and function definition.

Or - consider what string you would like to return, and add a statement to actually return it before the function exits.

Hello Alistair888,

I think your class mighrt work better like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class text
{
public:
	text();
	~text();

	string getaddr(int);
	void putaddr();  // <---Changed.

private:
	string addr;
};

void text::getaddr(int i)  // <---Changed.
{
	cout << "Enter the 1st line of the " << i + 1 << " address\n";

	getline(cin, addr);  // <---Changed.
}

void text::putaddr()  // <---Changed.
{
	cout << "You entered " << addr << "\n";
}


Still need to load up the whole program and see how it runs.

Hope that helps,

Andy
Hello Alistair888,

The other problem I found is line 33. It should Be either constexpr int size = 3; or at lest const .

Hope that helps,

Andy
Please does anybody know how to get -fpermissive working on c4droid so I can use const? I can't find anything on google. Thanks. Al
Hello Alistair888,

I am not familiar with -fpermissive or the c4droid, bur I beleieve that -fpermissive is a command line switch or directive for the compiler.

Did you try a search here? I did find this here:

http://www.cplusplus.com/search.do?q=fpermissive

http://www.cplusplus.com/forum/beginner/98155/

There is more, but I did not look at everything.

Next time information on the IDE, compiler and operating system would help.

Andy
Please does anybody know how to get -fpermissive working on c4droid so I can use const?


-fpermissive lets you get away with bad code that the compiler should stop you using. It's better to improve your code than convince the compiler to let you do things you shouldn't.
Just my 2 cents worth, but I think it's an important convention:

IMO getaddr should be named setaddr, and putaddr should be printaddr.

If the current putaddr returned a value it would be called getaddr.

A set function sets the value of a member variable.

A get function retrieves a member variable value. These should be const.
Topic archived. No new replies allowed.