setter/getter not working

Apr 24, 2017 at 1:47pm
When - in the source code - I attempt to get (for example) p[0] (check.getP(0)) it won't return with anything. I have already set p[0] to "password (check.setP(0, "password")) so it should be returning that, right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  class Check
{
	string p[5];
public:
	Check();
	~Check();
	void setP(int x, string y)
	{
		p[x] = y;
	};
	string getP(int x)
	{
		return p[x];
	};
}
Last edited on Apr 24, 2017 at 1:49pm
Apr 24, 2017 at 2:16pm
your code, with some minor edits, works fine. maybe the problem is elsewhere:
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
#include <iostream>
#include <string>

class Check
{
	std::string p[5];
public:
	//Check(){}
	//~Check(){}
	void setP(const size_t x, const std::string& y)
	//validate x for range
	{
		p[x] = y;
	};
	std::string getP(const size_t x)const
	{
		return p[x];
	};
};

int main()
{
    Check check{};
    check.setP(0, "password");
    std::cout << check.getP(0) << "\n";
}
Apr 24, 2017 at 2:23pm
UPDATE: Fixed some issues pointed out by AbstractAnon and neatened it up a bit.

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
#include <iostream>
#include <string>
#include "Check.h"
#include "Honeycheck.h"
using namespace std;

void generate(string x);

int main()
{
	bool secure = true;

	while (secure = true)
	{
		int sign;
		string password;

		cout << "[1]Sign-in/[2]Sign-up:";
		cin >> sign;

		if (sign == 1)
		{
			bool c = false;

			Check checkPass;
			Honeycheck checkHoney;

			cout << checkPass.getP(0) << endl;
			cout << "Password:";
			cin >> password;
			for (int i = 0; i < 5; i++)
			{
				cout << checkPass.getP(i) << endl;
				if (password == checkPass.getP(i))
				{
					c = true;
				}
			}

			if (c == true)
			{
				if (checkHoney.Pid(checkPass.getPid(password)) == true)
				{
					cout << "Password correct." << endl;
				}
				else { secure = false; }
			}
			else
			{
				cout << "Password incorrect." << endl;
			}

		}
		else if (sign == 2)
		{
			cout << "Enter password (must be between 6 and 12 characters long): ";
			cin >> password;

			generate(password);
		}
		else { cout << "ERROR 1" << endl; }
	}
}

void generate(string x)
{
	Check gen;
	Honeycheck genhoney;

	string lib[10];
	lib[0] = "123456";
	lib[1] = "ocean1";
	lib[2] = "Jesus1";
	lib[3] = "love48";
	lib[4] = "blessed";
	lib[5] = "Peppa06";
	lib[6] = "john316";
	lib[7] = "password";
	lib[8] = "football";
	lib[9] = "football1";

	genhoney.setHoney(0);

	gen.setP(0, "hello");
	gen.setP(1, lib[0]);
	gen.setP(2, lib[1]);
	gen.setP(3, lib[2]);
	gen.setP(4, lib[3]);
}


Check.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include <string>

using namespace std;

class Check
{
	string p[5];
public:
	Check();
	~Check();
	void setP(int x, string y);
	string getP(int x);
	int getPid(string x);
};


Check.cpp
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
#include "Check.h"
#include "Honeycheck.h"


Check::Check()
{
}


Check::~Check()
{
}

void Check::setP(int x, string y)
{
	p[x] = y;
}

string Check::getP(int x)
{
	return p[x];
}

int Check::getPid(string x)
{
	for (int i = 0; i < 5; i++)
	{
		if (p[i] == x)
		{
			return i;
		}
	}
}
Last edited on Apr 24, 2017 at 6:00pm
Apr 24, 2017 at 3:51pm
BUMP
Apr 24, 2017 at 4:38pm
Can you be a bit more specific? What are you inputting into your program? What output is it giving you? How is the behaviour you're seeing different from what you expect to see?
Apr 24, 2017 at 4:44pm
You haven't shown checkHoney, so I have no idea what that does.

In spite of that, you have a number of obvious problems.
source.cpp
----------
Line 54: passwords is local within the scope of sign == 2. It goes out of scope at line 60.

check.cpp
---------
Line 16,24: honey is a local variable. What is the point of calling the setter at line 24, when it goes out of scope at line 25.

Line 29: lib is a local variable. What is the point of setting the elements of lib, when it goes out of scope at line 42.

Line 41: You're returning an empty string. Why?

Line 56: Your for loop is going to cause an out of bounds reference. p has 5 elements. [0]-[4]. Your for loop is going to try and reference p[5] which is out of bounds.

Apr 24, 2017 at 5:16pm
Look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string Check::lib(int x)
{
	string lib[10];
	lib[0] = "123456";
	lib[1] = "ocean1";
	lib[2] = "Jesus1";
	lib[3] = "love48";
	lib[4] = "blessed";
	lib[5] = "Peppa06";
	lib[6] = "john316";
	lib[7] = "password";
	lib[8] = "football";
	lib[9] = "football1";

	return string();
}

Do you realize that this function always returns an empty string?

Apr 24, 2017 at 5:50pm
Here's Honeycheck.h and Honeycheck.cpp but I really doubt they're the issue...
1
2
3
4
5
6
7
8
9
10
11
#pragma once
class Honeycheck
{
	int honey;
public:
	Honeycheck();
	~Honeycheck();
	void setHoney(int x);
	int getHoney();
	bool Pid(int x);
};


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
#include "Honeycheck.h"



Honeycheck::Honeycheck()
{
}


Honeycheck::~Honeycheck()
{
}

void Honeycheck::setHoney(int x)
{
	honey = x;
}

int Honeycheck::getHoney()
{
	return honey;
}

bool Honeycheck::Pid(int x)
{
	if (x == honey)
	{
		return true;
	}
	else { return false; }
}

Apr 24, 2017 at 5:52pm
@Mikeyboy

The code I enter in and what it displays:

[1]Sign-in/[2]Sign-up:2
Enter password (must be between 6 and 12 characters long): password
[1]Sign-in/[2]Sign-up:1

Password:password





Password incorrect.
[1]Sign-in/[2]Sign-up:
Apr 24, 2017 at 6:01pm
Made some updates to my 2nd post
Apr 24, 2017 at 6:40pm
main.cpp
line 13: You're using the assignment operator (=), not the equality operator (==).

Line 25: checkpass consists of 5 empty strings.
Line 28: This is going to output an empty string.
Line 33-34: You're outputting and comparing against empty strings.

Line 26,42: checkhoney.honey is uninitialized (garbage).

Lines 70-80: These lines do nothing.

Lines 67-68: gen and genhoney are local variables. They go out of scope at line 89. What is the point of these variables?

check.cpp
---------
Line 33: If a match on x is not found, what is returned? How does the caller determine a match was not found?

Do you understand the concept of local variables going out of scope?
Apr 24, 2017 at 7:05pm
No, I did not understand local variables... do now thanks it's all fixed. I'm an idiot XD

Thank you so much
Topic archived. No new replies allowed.