Anagram function not returning

Hi, i'm a beginner in c++, and i can really use some help here. In the code, when i run it, the program does not process the strings i am entering. Am i missing something here? 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
48
49
50
51
52
53
  using namespace std;

bool b;

bool isAnagram(string s1, string s2);
void inputAna(string s1, string s2);
void outputAna(string s1,  string s2);


int main() 
{ 
	string str1, str2; 	
	inputAna(str1, str2);
	b = isAnagram(str1, str2); 
	outputAna(str1, str2);

	
} 

void inputAna(string s1, string s2)
{
	//input
	cout << "Enter a word: "; 
	getline(cin, s1); 
	cout << "Enter another word: "; 
	getline(cin, s2);
}


bool isAnagram(string s1, string s2)
{ 
	bool b = false; 
	if (s1.length() == s2.length() ) 
	{ 
		sort(s1.begin(), s1.end()); 
		sort(s2.begin(), s2.end()); 
		if (s1 == s2) 
			{ 
				b = true; 
			} 
	} 
	return b; 
}

void outputAna(string s1,  string s2)
{
	if (b == true)
		cout<<s1<<" and "<<s2<<" are anagrams.";
	if (b == false)
		cout<<s1<<" and "<<s2<<" are not anagrams.";
	return; 
	
}
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
bool isAnagram(string s1, string s2);
void inputAna(string &s1, string &s2);
void outputAna(string s1,  string s2, bool b);


int main() 
{ 
	string str1, str2; 	
	inputAna(str1, str2);
	bool b = isAnagram(str1, str2); 
	outputAna(str1, str2, b);
} 

void inputAna(string &s1, string &s2)
{
	//input
	cout << "Enter a word: "; 
	getline(cin, s1); 
	cout << "Enter another word: "; 
	getline(cin, s2);
}


bool isAnagram(string s1, string s2)
{ 
	bool b = false; 
	if (s1.length() == s2.length() ) 
	{ 
		sort(s1.begin(), s1.end()); 
		sort(s2.begin(), s2.end()); 
		if (s1 == s2) 
			{ 
				b = true; 
			} 
	} 
	return b; 
}

void outputAna(string s1,  string s2, bool b)
{
	if (b == true)
		cout<<s1<<" and "<<s2<<" are anagrams.";
	if (b == false)
		cout<<s1<<" and "<<s2<<" are not anagrams.";
	return; 
	
}


Have you learned references yet? If you want to indirectly modify the variables in a function when you pass them to the function you have to use references.
You don't need to declare a variable just to be used as a return value. Simply return what you need directly.
1
2
3
4
5
6
7
8
9
10
bool isAnagram(string s1, string s2)
{ 
    if (s1.length() == s2.length()) 
    { 
        sort(s1.begin(), s1.end()); 
        sort(s2.begin(), s2.end()); 
        if (s1 == s2) return true;
        else return false;
    } 
}


1
2
3
4
5
6
7
8
9
void outputAna(string s1,  string s2, bool b)
{
	if (b == true)
		cout<<s1<<" and "<<s2<<" are anagrams.";
	if (b == false)
		cout<<s1<<" and "<<s2<<" are not anagrams.";
	return; 
	
}

The parameter b is in no way related to the variable b in isAnagram(). You need to call isAnagram() passing in s1 and s2 as arguments, like so
1
2
3
4
5
6
7
void outputAna(string s1,  string s2)
{
    if (isAnagram(s1, s2));
        cout<<s1<<" and "<<s2<<" are anagrams.";
    else
        cout<<s1<<" and "<<s2<<" are not anagrams.";
}


Then in main() you can just do this
1
2
3
4
5
6
int main()
{ 
    string str1, str2;  
    inputAna(str1, str2);
    outputAna(str1, str2);
}
Last edited on
thanks so much!
Topic archived. No new replies allowed.