problem with while loop :(

Hey guys;
This is my code for a project I was working on, the code is supposed to accept two strings from the user and determine if they are anagrams (by removing spaces and non characters/numbers from the strings). The code works correctly ... the first time, but when it loops to have the user enter two new words, it crashes. I cant for the life of me figure out why, any hints/tips would be much appreciated.

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

using namespace std;

class anagram
{
private:
	string input1, input2, mod1, mod2;
	string preprocess(string str);
	
public:
    
	anagram(string one, string two)
	{
		mod1=preprocess(one);
		mod2=preprocess(two);
	}
	
	bool isAnagram_iterative();
	
};

bool anagram::isAnagram_iterative()
{
	if(mod1.size() != mod2.size())
	{return false;}
	for (int i =0; i<mod1.size();i++)
	{
		if(mod1[i] ==mod2[i])
		{
			return true;
		}
		if(mod1[i] != mod2[i])
		{return false;}
	}
    return false;
    
}
string anagram::preprocess(string str)
{
	int num_nonletters=0;
    for (int i =0; i <str.size(); i ++)
    {
        if(str[i] == toupper(str[i]))
        {
            str[i] = tolower(str[i]);
        }
        
        if(ispunct(str[i]))
        {
            num_nonletters++;
        }
        if(isspace(str[i]))
        {
            num_nonletters++;
        }
    }
    
    char  temp;
	bool swap;
    
	do
	{	swap = false;
		for (int iterator = 0; iterator < (str.size() - 1); iterator++)
		{
			if (str[iterator] > str[iterator + 1])
			{
				temp = str[iterator];
				str[iterator] = str[iterator + 1];
				str[iterator + 1] = temp;
				swap = true;
			}
		}
    } while (swap);
	str=str.substr(num_nonletters);
    return str;
}

int main ()
{
	char ask;
	string first_word, word2;
	int iterator=1;
	do{
    cout << "input: " <<iterator <<endl;
    cout <<"Enter first input string: \n";
    getline(cin, first_word);
    cout <<"Enter second input string: \n";
    getline(cin, word2);
    anagram object(first_word,word2);
    if(object.isAnagram_iterative())
    {
        cout << "Iterative test passed!\n";
    }
    else 
    {	
        cout <<"Iterative test FAILED!\n";
    }
        cout << "run again \n";
        cin >> ask;
        iterator++;
    }while(ask=='y');
}
The reason the error is triggered on the second pass of the loop, is the trailing newline '\n' remaining in the input buffer after cin >> ask; at line 102. That can be fixed by the use of cin.ignore().
102
103
            cin >> ask;
            cin.ignore(1000, '\n');


As for the actual error which causes the crash, it looks like your code cannot handle a zero-length string. That's something you should look at fixing either at the stage of getting user input, or in the anagram preprocess() function.
Last edited on
that makes sense, I probably should have just used getline for the ask answer and turned it into a string variable; thank you for your quick response.
Topic archived. No new replies allowed.