Need help with determining specific characters in strings

I am working on a project where we are taking latin words such as "amare" and replacing the end (are, ire, ere) with infinitives like ("o", "amus" etc)

For example if I input "amare" im supposed to cout the conjugate like: amo, amamus etc.

The problem I am running into is deciding whether a string has "are", "ire" or "ere" at the end. I have posted my code below. I have tried with if/else statements and while loops but I keep getting "exception abort code 6". I dont know if this is a correct way to do this. I should mention in prints "are" words but when I enter "ire" it fails.

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

using namespace std;

string infin;

string are = "are", ire = "ire" , ere = "ere";




int x;

int main()
{
	while(cin >> infin)
	{
		while(infin.find(are))
		{
			x = infin.find(are);
		
			infin.erase(x, 3);

			cout << infin + "o" << "     " << infin + "amus\n";

			cout << infin + "as" << "    " << infin + "atis\n";

			cout << infin + "at" << "    " << infin + "ant\n";
		}

		while(infin.find(ire))
		{
			x = infin.find(ire);

			infin.erase(x, 3);

			cout << infin + "eo" << "     " << infin + "emus";

		}

	}
		
	

return 0;

}
Last edited on
closed account (2b5z8vqX)
The conditions of all the while loops invoking the std::basic_string<>::find() member function will always be true if the value returned by the aforementioned member function is not zero. When the given argument for std::basic_string<>::find() is not found within the string, std::basic_string::npos (-1) is returned. Therefore, your loops will continue iterating until the string being searched for is located at the start of infin.

http://en.cppreference.com/w/cpp/string/basic_string/npos
http://en.cppreference.com/w/cpp/string/basic_string/find

Compare the result of std::basic_string<>::find() to std::string::npos using the inequality operator (!= or not equal to) in the conditions of the inner while loops to fix the problem.


The problem I am running into is deciding whether a string has "are", "ire" or "ere" at the end.

I suggest you use the std::basic_string<>::substr() member function.

http://en.cppreference.com/w/cpp/string/basic_string/substr
I would use substr() but I unfortunately do not know how many characters will be in the input (our professor will be using random latin verbs). Could I use find(are, ire, ere)? and then compare the end of that string to see which one it is? Basically I'm unsure of how to determine whether or not the end of the string is are, ere, or ire. If you could help me out that would be great or I could get some tutoring tomorrow. The project isnt due until thursday, I just thought id take a crack at it now.
Never mind, figured out the npos thing. Working now. Thanks for the help! I'll have to mention it to my prof. tomorrow.
closed account (2b5z8vqX)
I would use substr() but I unfortunately do not know how many characters will be in the input (our professor will be using random latin verbs). Could I use find(are, ire, ere)? and then compare the end of that string to see which one it is? Basically I'm unsure of how to determine whether or not the end of the string is are, ere, or ire. If you could help me out that would be great or I could get some tutoring tomorrow.

An object of type std::string is aware of its size (how many characters it contains), so there is no need for you to know anything about the input given by your professor.

Using the std::string::substr member function and the std::string::size member function you can obtain the characters at the end of the string object and then compare them to are, ire, and ere.

1
2
3
4
5
const std::string::size_type size(infin.size());
if(infin.substr(size - 3, 3) == "are") {
        // ...
}
// ... 


Never mind, figured out the npos thing. Working now. Thanks for the help! I'll have to mention it to my prof. tomorrow.

No problem.
Topic archived. No new replies allowed.