using the find function

Sep 22, 2014 at 8:08am
Hello,
I am trying to use the find function to find when there is a period in a sentence.
The code I have written always finds a period even when there is not one.

1
2
3
4
5
6
7
8
9
10
11
12
13
  {
        bool found=false;
        cout<<"Enter a sentence: "<<endl;
        cin>>sentence;
    {
        if (found=sentence.find('.',0));
        found=true;
    }
        if (found=true)
        {
            cout << "Period found"<<endl;
        }
    }


For some reason it skips over my find code and goes strait to the cout?
Sep 22, 2014 at 8:19am
Change line 6 to:
if ( sentence.find('.') != std::string::npos)

and line 9 to:
if (found == true)
Sep 22, 2014 at 8:26am
Of course..... I feel really dumb now

Thank you!
Sep 22, 2014 at 3:00pm
one last thing I tried to add the word "stop" to find, it finds it if it is not entered with a period '.' but when I try to find "stop." it only finds '.'

why is that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 {
        bool pfound=false;
        bool sfound=false;
        cout<<"Enter a sentence: "<<endl;
        cin>>sentence;
    {
        if (sentence.find('.') != std::string::npos)
        pfound=true;
        else if (sentence.find("stop")!= std::string::npos)
        sfound=true;
    }
        if (pfound)
        
            cout << "Period found"<<endl;
        
        if (sfound)
        
            cout<<"stop found"<<endl;
        

    }
Sep 22, 2014 at 5:00pm
My code now only outputs if stop or period are found but not if they are not found why will it not display the not found if it does not find a period or the word stop?

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

{
        bool pfound=false;
        bool sfound=false;
        cout<<"Enter a sentence: "<<endl;
        cin>>sentence;
    
        if (sentence.find('.') != std::string::npos)
        pfound=true;
        
    
        if (pfound)
        
            cout << "Period found"<<endl;
            
        else (sentence.find('.')== std::string::npos);
        pfound=false;
        if (pfound)
        
            cout<<"period not found"<<endl;
        
        
        else if (sentence.find("stop")!= std::string::npos)
        sfound=true;
        
        if (sfound)
        
            cout<<"stop found"<<endl;
            
        else (sentence.find("stop")== std::string::npos);
        sfound=false;
        if (sfound)
        
            cout<<"stop not found"<<endl;
        
        
            
    }
Sep 22, 2014 at 5:22pm
Because you messed up code blocks. That is how your program looks after proper identation and separating code into logic blocks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (sentence.find('.') != std::string::npos)
    pfound=true;
        
if (pfound)
    cout << "Period found"<<endl;
else 
    (sentence.find('.')== std::string::npos); //does nothing here

pfound=false; // always executed
if (pfound)
    cout<<"period not found"<<endl;
else if (sentence.find("stop")!= std::string::npos)
    sfound=true;

if (sfound)
    cout<<"stop found"<<endl;
else 
    (sentence.find("stop")== std::string::npos); //does nothing here

sfound=false;
if (sfound)
    cout<<"stop not found"<<endl;

use curly brackets if your if/else block will contain more than one line of code.
Sep 22, 2014 at 5:41pm
got it, this worked

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
{
        bool pfound=false;
        bool sfound=false;
        cout<<"Enter a sentence: "<<endl;
        cin>>sentence;
    
        {if (sentence.find('.') != std::string::npos)
        pfound=true;
        
    
        if (pfound)
        
            cout << "Period found"<<endl;
            
        else 
        
            cout<<"period not found"<<endl;
        }
        
        {if (sentence.find("stop")!= std::string::npos)
        sfound=true;
        
        if (sfound)
        
            cout<<"stop found"<<endl;
        
            
        else 
        
            cout<<"stop not found"<<endl;
        }
        
            
    }


is this ugly code?
Sep 22, 2014 at 6:06pm
I added the find function to say where "stop" was found or where '.' was found, but then I noticed it would not allow me to input sentences, so I change the cin to getline and now it skips over all my code?

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
{
        bool pfound=false;
        bool sfound=false;
        cout<<"Enter a sentence: "<<endl;
        getline(cin,sentence);
    std::size_t found=sentence.find('.');
        {if (sentence.find('.') != std::string::npos)
        pfound=true;
        
    
        if (pfound)
        
            cout << "The character '.' is located at index "<<found<<endl;
            
        else 
        
            cout<<"Your sentence does not contain the character '.'"<<endl;
        }
        std::size_t found1=sentence.find("stop");
        {if (sentence.find("stop")!= std::string::npos)
        sfound=true;
        
        if (sfound)
        
            cout<<"The word "<< "stop"<<" starts at index "<<found1<<endl;
        
            
        else 
        
            cout<<"Your sentence does not contain the word "<< "stop"<<endl;
        }
        
            
    }
Sep 22, 2014 at 6:55pm
Sep 22, 2014 at 8:33pm
It can find '.' and/or "stop" now in a sentence, but the location it gives is incorrect. why would the getline change how it computes the location of what its looking for?

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
{
        bool pfound=false;
        bool sfound=false;
        cout<<"Enter a sentence: "<<endl;
        ((cin >> sentence).get() && std::getline(cin, sentence));
    std::size_t found=sentence.find('.');
        {if (sentence.find('.') != std::string::npos)
        pfound=true;
        
    
        if (pfound)
        
            cout << "The character '.' is located at index "<<found<<endl;
            
        else 
        
            cout<<"Your sentence does not contain the character '.'"<<endl;
        }
        std::size_t found1=sentence.find("stop");
        {if (sentence.find("stop")!= std::string::npos)
        sfound=true;
        
        if (sfound)
        
            cout<<"The word "<< "stop"<<" starts at index "<<found1<<endl;
        
            
        else 
        
            cout<<"Your sentence does not contain the word "<< "stop"<<endl;
        }
        
            
    }
Sep 23, 2014 at 2:36pm
Csn you post both your input and result of your program?
Sep 24, 2014 at 9:38pm
got it thank you, I found the error of my ways
Topic archived. No new replies allowed.