I thought I fixed line 8...
int lengthLeft = strlen(s2);
...But it still underlines line 8 (s2) line 13 (s1) and line 17 (s1)
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <cstring>
using namespace std;
// Checking if a string is a substring
int indexOf(const string& s1, const string& s2)
{
int lengthLeft = strlen(s2);
int startingIndex = 0;
toWhile: //I found this in the textbook: toWhile is a label. You can use break with a label attached.
while (strlen(s1) <= lengthLeft)
{
bool same = true;
for (int i = 0; i < strlen(s1); i++)
{
if (s1[i] != s2[startingIndex + i])
{
startingIndex++;
lengthLeft--;
same = false;
}
}
if (same) return startingIndex;
}
return -1;
}
int main()
{
// Prompt the user to enter a string
cout << "Enter the first string: ";
std::string s1;
cin.getline(s1, 80);
// Prompt the user to enter a string
cout << "Enter the second string: ";
const std::string s2;
std::getline(std::cin,s2);
cout << "indexOf(\"" << s1 << "\", \"" <<
s2 << "\") is " << indexOf(s1, s2) << endl;
return 0;
}
|
I am still unsure of line 37-38
1 2
|
const std::string s2;
std::getline(std::cin,s2);
|
after reading about the std::getline....etc
My errors now say:
error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const std::string' to 'const char *'
: note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
: cannot convert argument 1 from 'const std::string' to 'const char *'
note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
: error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const std::string' to 'const char *'
: note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
:error C2664: 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::getline(_Elem *,std::streamsize,_Elem)': cannot convert argument 1 from 'std::string' to 'char *'
: error C2039: 'getline': is not a member of 'std'
: error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
And here I thought I was getting close to solving this...