[SOLVED]Taking a letter off the end of a string?

Hey guys,

Just wondering the simplest way to take a letter off the end of a string.

Etc...
1
2
3
4
5
6
7
8
9
10
std::string myString;
std::cin >> myString;
std::cout << "Your string: " << myString << std::endl;

function(myString)
{
    myString - last character;
}

std::cout << "Your string minus the last letter: " << myString << std::endl;


So basically if the initial input was "Wordd" - the end output would be "Word".

Thanks,
Myth.
Last edited on
myString = myString.substr(0, myString.length()-1);
Cheers Zaita
Hmm, can you see whats wrong with this?
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
#include <iostream>
#include <string>
#include <conio.h>

void Password()
{
	std::cout << "Please Enter A Password: ";
	std::string password;
	int temp;

	do
	{
		temp = _getch();
		if (temp != 0 && temp != 13)
		{
			password += (char)temp;
			std::cout << "*";
		}
		if(temp == 8)
		{
			password = password.substr(0, password.length()-1);
			ClearScreen();
			std::cout << "Please Enter A Password: ";
			for(unsigned int i = 0; i < password.length(); i++)
			{
				std::cout << "*";
			}
		}
	}while (temp != 13);

	std::cout << "\nYour Password is: " << password << std::endl;
}


This compiles fine and works except for backspace. Which in conio.h is equal to 8. But when pressed doesn't do what I want it to. Basically to create a fake backspace to remove a character and then displays the "*" -1 as well in the console output.

ClearScreen() is my own function else where and works fine.
Whoops my bad fixed it - stupid me if (temp != 0 && temp != 13 && temp != 8)
Isn't this simpler?

myString.erase(myString.end()-1);

Both answers require that the length be at least 1 character. Both are fairly simple.
isn't it easier to use
istream& getline ( istream& is, string& str );
Making use of the conio.h is my goal.
Topic archived. No new replies allowed.