So I'm trying to do a program that short the name of a website
(ex: http://www.cplusplus.com to cplusplus.com)
But i get String Subscript out of range;
What's wrong?
Just search up to the '.' then remove everything infront of the period.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string site = "www.cplusplus.com";
int period = site.find_first_of('.');
if (period != string::npos)
site = site.substr(period + 1); // We need to add one to get past the period
cout << site << endl;
}
It worked thanks, I'd like to know where i can find information about the functions you used, like ".find_first_of(), string::npos, substr()" .find_first_of() is kinda obvious but I don't understand the others and i only can be well with myself if i know what i did in the program.
And all the other methods for strings can be found here
http://www.cplusplus.com/reference/string/string/
The only tricky one to understand is string::npos so I will explain it a bit. The way we used it in the example above is to check if the string.find_first_of() function found what it is was looking for. So lets say it didn't find a period in the string, it would then return the value string::npos from the function instead of the position where it found what we were looking for. So basically we use it to make sure the function found what it was looking for before we move on.