Cant see whats wrong...

Hello all Im writing a program that is supposed to be a survey for two people to see if they are compatible roomates...however the problem is that I cant seem to get the program to give me the proper response when I input parameters that should give me a negative response i still get a response of positive compatibility.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
using namespace std;

bool survey();


int main()
{
  cout<<" Hello, the purpose of this program is to see if you and the person you are taking this survey with are compatible roomates"<<endl<<endl;
  if (survey() == true)
	  cout<<" congratulations"<<endl;
  else
	  cout<< " You are not a good match" <<endl;

 system("pause");
 return 0;
}

bool survey()
{
	int age1, age2;
	char sex1,sex2;
	char answerx1, answerx2, answery1, answery2;
	bool smoking1, smoking2;
	bool pets1, pets2;
	int results = 0;
	int gender1, gender2;
	bool final_answer;

	cout << "Please enter your age:"<<endl<<endl;
	cin >> age1;

	cout<< "please enter your gender, type m for male or f for female:"<<endl<<endl;
	cin >> sex1;
	//cout<< sex1<<endl;

	if (sex1 == 'm' || sex1 == 'M')
	{	
		gender1 = 0;
	}	
	else
	{
		gender1 = 1;
	}
	cout << "do you prefer non-smoking environments, please type y or n:"<<endl<<endl;
	cin >> answerx1;
	if (answerx1 == 'y' || answerx1 == 'Y')
	{	
		smoking1 = true;
	}
	else
	{
		smoking1 = false;
	}

	cout << "do you like pets,please type y or n:"<<endl<<endl;
	cin >> answerx2;
	if (answerx2 == 'y' || answerx2 =='Y')
	{	
		pets1 = true;
	}
	else
	{
		pets1 = false;
	}
	//This part of the survey is for the other person
	cout << "Please enter your age:"<<endl<<endl;
	cin >> age2;

	cout<< "please enter your gender, type m for male or f for female:"<<endl<<endl;
	cin >> sex2;
	//cout<< sex2<<endl;

	if (sex2 == 'm' || sex2 == 'M')
	{	
		gender2 = 0;
	}
	else 
	{
		gender2 = 1;
	}
	cout << "do you prefer non-smoking environments, please type y or n:"<<endl<<endl;
	cin >> answery1;
	if (answery1 == 'y' || answery1 == 'Y')
		smoking2 = true;
	else
		smoking2 = false; 

	cout << "do you like pets,please type y or n:"<<endl<<endl;
	cin >> answery2;
	if (answery2 == 'y' || answery2 == 'Y')
		pets2 = true;
	else
		pets2 = false;


	//Now we will compare answers
	
	//cout<< results<<endl;
	
	//cout<< age1<< age2<<endl;
	//cout << age1 - age2 <<endl;
	if (age1 - age2 <= 10 || age1 - age2 >= -10)
		results++;
	
	//cout<<gender1<<gender2<<endl;
	if (gender1 == gender2)
		results++;
	//cout<<pets1<<pets2<<endl;
	if (pets1 == pets2)
		results++;
	//cout<<smoking1<<smoking2<<endl;
	if (smoking1 == smoking2)
		results++;
	
	
	//now we count results

	//cout<< results<<endl;
	//cout<< gender1<< " "<<gender2<<endl;


	if (results >= 2)
		final_answer = true;
	else
		final_answer = false;


	
	return final_answer;
}


i forgot to add when I input
36,f,n,y
for the first half and input 20,m,y,y for the second half, it get congratulations, but that should not happen if the counter (results) is working properly ...which i dont think it is...as I stated I'm a bit lost
36-20 = -16, which is less than 10. So that check passes. Try this instead:
1
2
if (abs(age1 - age2) <= 10)
	results++;


Or you could make it be (age1 - age2 <= 10 && age1 - age2 >= -10), which would accomplish the same thing, but the abs() version is easier.
Last edited on
First thing I noticed when debugging this is that pretty much any answer on the age comparison line is going to result in a true. I assume you meant for the ages to be within 10 years of each other. But look what happens. If age 1 is 50 and age 2 is 20 the expression will still be true. 50-20=30. While 30 <= 10 is false, 30 >= -10 is in fact true. So that comparison will always be true for any values that I can see. What you could do is #include <cmath>, then declare another variable and use the abs function to get the absolute value of the age difference and compare that to 10, like this:

1
2
3
4
int agediff;
agediff = abs(age1-age2);
if (agediff <= 10)
    results++;


That should work for ya.
thank you guys that helped a lot...so far that seems to have worked.
Topic archived. No new replies allowed.