bool variable?

The code below is supposed to find a small string inside of a larger string. And it has to output the position in which the smaller string was found and the amount of matches found in the larger string. My professor told me that I have to use a bool variable to get the code to work properly. I'm new to c++ I'm so lost. Can someone please help me make this work, or explain to me how a bool variable works?

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  #include <iostream>
using namespace std;

int main()
{
int NUM_MTCH, POSS1, POSS2, LENS1, LENS2 ;
NUM_MTCH=0;    //NUM_MTCH= THE NUMBER OF MATCHES
POSS1=0;       //POSITION OF PATTERM IN STRING2
POSS2=0;       //POSITION OF PATTERN IN STRING1
LENS1=0;       //LENGTH OF FIRST STRING
LENS2=0;       //LENGTH OF SECOND STRING
bool SUCCESS(true);
string S1, S2;
do
{
            cout<< "Enter First String: ";
            cin>> S1;
            for (POSS1=0; S1[POSS1] != '\0'; POSS1++)
            {
                LENS1++;
            }
            cout<< "Enter Second String: ";
            cin>>S2;
            for (POSS2=0; S2[POSS2] != '\0'; POSS2++)
            {
                LENS2++;
            }

}
while(LENS2>LENS1);

while (S1[POSS1] != '\0')
{

    for(POSS2=0; S2[POSS2+POSS1]==S1[POSS1]; POSS2++)
    {
        if (S1[POSS1+POSS2] == S2[POSS2])
        {
            SUCCESS=true;
        }
        else
        {
            SUCCESS=false;
        }
    }
    if (SUCCESS=true)
    {
        NUM_MTCH++;
        POSS1++;
    }
}
cout<< "POSITIONS: "<<POSS1<<endl;

if (NUM_MTCH > 0)
{
    cout<< "THE AMOUNT OF MATCHES ARE: "<<NUM_MTCH;
}

if (NUM_MTCH == 0)
{
	cout<< "NOTHING";
}
return 0;
}
You should use the standard string!

To get length of string use string.length().

This can replace line 18-21 and 24-27 with:
LENS1 = S1.length();
and
LENS2 = S2.length();

http://www.cplusplus.com/reference/string/string/
Last edited on
Another thing on line 46, change:
if (SUCCESS=true)
to
if (SUCCESS==true)

or simple use if (SUCCESS) (will get the same result).
But now I don't get any result at all.
By the looks of it POSS1 starts off with the same value as LENS1... same for POSS2.

I was just pointing out a few things.
Oh you're right! Thanks, I really appreciate you helping me.
I would also like to point out that instead of that ugly 'for' loop checking each character, you can use the standard strings (I love the standard string) .substr(position, length) function to get a selected string out of S1 and then compare that string with S2.

1
2
3
if(S1.substr(position, S2.length()) == S2)
 SUCCEED = true;
else SUCCEED = false;


Sorry, I'm just pushing alternatives :p
I would like to use that, but I'm not allowed to use any built in functions. :'( That's why I'm having such a hard time...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

bool find( std::string bigger_str, std::size_t start_pos, std::string smaller_str )
{
    if( bigger_str.size() < ( smaller_str.size() + start_pos ) ) return false ;

    for( std::size_t i = 0 ; i < smaller_str.size() ; ++i )
        if( smaller_str[i] != bigger_str[i+start_pos] ) return false ;

    return true ;
}

int main()
{
    const std::string bigger = "abracrabradrabra" ;
    std::cout << bigger << "\n01234567890123456789\n\n" ;

    const std::string smaller = "abr" ;

    for( std::size_t pos = 0 ; pos < bigger.size() ; ++pos )
        if( find( bigger, pos, smaller ) )
           std::cout << "found '" << smaller << "' at position " << pos << '\n' ;
}

http://coliru.stacked-crooked.com/a/8b46c6d761a6f530
Topic archived. No new replies allowed.