can anyone help me Removing letters from Alphabet, depending on which letter is entered.

Jan 24, 2012 at 10:12pm
Hi people,

I have the alphabet which has 26 letters in a string and I wish to eliminate the letters one by one, by entering the letter I choose to press from this list.

I came to tackle it with something like this:

string alphabet;
char letter; //letter inputted
alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
alphabet -= letter;

cout << alphabet;

I would appreciate any help given to me as I feel I came to a point of no return. I am still a beginner in C++ and have still a long way to go. Programming language I have knowledge of is C programing. I tried to tackle this problem also buy using function from string and list but had no luck. Thanks in advance.
orangeapple
Last edited on Jan 25, 2012 at 9:49am
Jan 24, 2012 at 10:18pm
The string class has no operator -, so trying to use it on a string object like this:

alphabet -= letter;

will not work. Here is a page giving all the operators and functions the string class has.

http://www.cplusplus.com/reference/string/string/

The erase function looks like what you need. It is still up to you to direct which element in the string to erase.
Jan 25, 2012 at 9:03am
Hi I tried the subtract function as when searching in the link you gave me I found a very similar example. But mine doesn't seem to work for some reason or another. What I did was this code and it keeps failing on and on again without any success:

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z";

string subtraction, lettersleft;
size_t pos;

pos = alphabet.find("live"); // position of "live" in str
lettersleft = alphabet.substr (pos); // get from "live" to the end

cout << subtraction << ' ' << lettersleft;

return 0;
}

Thanks in advance,

Regards,
Orangeapple
Jan 25, 2012 at 9:06am
just to point out that live will be

char live;
cin >> live;

because I forgot to tell in the code that i will be entering the letter.
Is there a more compact way to tackle such thing?>
Jan 25, 2012 at 10:41am
Hi I tried the subtract function as when searching in the link you gave me I found a very similar example.


I'm guessing you're talking about substr. substr does not mean "subtract string".

IN your code above, I see that you create an empty string called subtraction, that does nothing and you never do anything to. It doesn't seem to have any use.

You then seacrh for the word "live" in the string ""a b c d e f g h i j k l m n o p q r s t u v w x y z". Clearly, the word "live" is not in that string; unfortunately, your code then attempts to delete a letter at this non-existent position, and an exception is thrown.

Try this instead:

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

using namespace std;

int main ()
{
string alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z";

char letterToRemove;
cout << "Which letter to remove? Enter a single letter." << endl;
cin >> letterToRemove;

size_t pos;

pos = alphabet.find(letterToRemove);
if (string::npos == pos)
{
  cout << "Letter not found";
}
else
{
  alphabet.erase(pos, 2); // get from "live" to the end
  cout << "String is now " << alphabet;
}

return 0;
}
Jan 26, 2012 at 9:48pm
Hi people, thanks for your prompt replies. You were so helpful. Thanks again really appreciate
Topic archived. No new replies allowed.