Delete a character from a string

How would I go about deleting the very first character from a string?

My attempt below fails with several errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
char delspace[30] = "   t e  s   t";
string space = " ";

while
(delspace[0] != space)
{
delspace.erase(0);                       
                          }
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
How would I go about deleting the very first character from a string?


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
{
string someString("someString");
someString.erase(0,1);

cout << someString;
return 0;
}


Your code tries to use the string member function erase on an object that's not a string (delspace).
Last edited on
You have declare delspace to be a char* (an string array) but nevertheless you apply to it a member of std::string, i.e. erase()?

I think you have mixed up these two containers.

String in c are defined like arrays ending with \0.
In c++ on the other hand std::string is preferred.

You cannot normally delete an element of an array but instead you can do this:
either push all elements one position to the left and don't use the last position OR create a new array with the number of elements you want.

On std::string there are function members like erase() which you can use to your accommodation. Using these you can erase an element of your string. see next page for example over this option:
http://www.cplusplus.com/reference/string/string/erase/
Topic archived. No new replies allowed.