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
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
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.
#include <iostream>
#include <string>
usingnamespace 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;
}