Anagram code not working when I put in on a switch statement

Hello there, I am stuck in this code about finding anagrams. I practiced this in a different code and it work. The first code is working but when I use it in a switch statement it is not working. I can't seem to find where is my mistake. I just copied and paste my finished code in the switch statement.

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
 #include<iostream>
#include<iomanip>

#include<string>
#include<algorithm>

using namespace std;

char menu();
string sort(string str);//reaaranging the orders of string to test if its anagram
bool areAnagrams(string str1,string str2);//process the strgin if anagram anf returns the value 1 or 0
string RemSpacePunct(string str);//function that removes space and punctation in aa string

int main(){
	bool tite;
	string str1, str2, str1_space, str2_space, str1_sort,str2_sort;
	cout << "string 1: \n"; 
	getline (cin, str1);
	
	str1_space = RemSpacePunct(str1);
	str1_sort = sort(str1_space);
	cout << "string 2: \n"; 
	getline (cin, str2);
	
	str2_space = RemSpacePunct(str2);
	str2_sort = sort(str2_space);

	
	tite = areAnagrams(str1_sort, str2_sort);
	
	if(tite)
	{
		cout <<"They are anagrams";
	}
	else
	{
		cout <<"They are not anagrams";
	}
}
string sort(string str)
{
	
	sort(str.begin(), str.end());
    return str;
}
//////////////////////////////
bool areAnagrams(string str1,string str2)
{
	//add code here
  return str1 == str2;
}
//////////////////////////////
string RemSpacePunct(string str)
{
	str.erase(remove(str.begin(), str.end(), ' '), str.end());
	return str;
}
(I put the code in the top here in a switch statement)
int main(){
	char let,ans, sal;
	bool anagram;
	string string1, string2;
	string str1_space, str2_space, str1_sort,str2_sort;
    
	cin >> ans;
	   switch(ans)
  	    {
  			case 'a':
  				char reverse[120];
  				system("cls");
  				// for palindrome
  				break;
  			case 'b':
  		
  				system("cls");

				cout << "enter string1: "; 
				getline(cin, string1);
				
				str1_space = RemSpacePunct(string1);
				str1_sort = sort(str1_space);
							
				cout << "enter string2: "; 
				getline(cin, string2);
			
				str2_space = RemSpacePunct(string2);
				str2_sort = sort(str2_space);
					

				if(areAnagrams(str1_sort, str2_sort))
				{
					cout <<"string 1: " << str1_sort << endl;
					cout <<"string 2: " << str2_sort << endl;
					cout <<"They are anagrams";
				}
				else
				{
					cout <<"string 1: " << str1_sort << endl;
					cout <<"string 2: " << str2_sort << endl;
					cout <<"They are not anagrams";
				}
  				break;
  				
//end main 


Last edited on
let me guess, its not reading the strings correctly :)
try cin.ignore(10, '\n');
on line 66 after the cin statement.

cin and getline treat the input stream differently, and you need to look up this interaction (there are many a website that explain it in a page or two in detail). This is just one of those weird things that you don't know until you encounter it the first time, but remember it, it happens a lot.
Last edited on
Yo! It works!!! Thanks dude... I am stuck in this problem for almost an hour! Thanks for your help!
Topic archived. No new replies allowed.