Issue with loop counter

Oct 8, 2018 at 2:14am
Hi,
I'm doing a personal cpp challenge to help me learn. My program takes an integer (int x) and loops that in a cin input for that many strings. (string 1)
For instance, int x =4.. the loop cins string 1 four times.

Then, I'm creating a an if (find) to search each string for the letters CD appearing in that order.

Then, I want it to spit out how many of the string y's had those letters.

However, my code just spits out the loopcounter number for each time it finds those letters. How can I fix this so that I just get the number of times it is found?

edit** Actually, I need the number of times those letters DO NOT show up. **
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

int main()
{
             
        int x ;
        cin >> x ;
        string Value1 ;
        
        for (int i=1; i <=x; i++) {
        cin >> Value1 ; 
        
         
         if (Value1.find("CD") != string::npos)
        {
         cout << i ; 
        }}
        return 0;
 }
Last edited on Oct 8, 2018 at 2:19am
Oct 8, 2018 at 2:55am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
    int x ;
    std::cin >> x ;

    if( x > 0 )
    {
        int count_cd_found = 0 ;

        for( int i = 0 ; i < x ; ++i )
        {
            std::string str ;
            std::cin >> str ;
            if( str.find("CD") != std::string::npos ) ++count_cd_found ;
        }

        std::cout << "found \"CD\" in " << count_cd_found << " strings\n"
                  << "did not find \"CD\" in " << x - count_cd_found << " strings\n";
    }
}
Oct 8, 2018 at 3:17am
DUUUUUUUUUUUUUUUDE.

There's a count find!?

Thank you.
Topic archived. No new replies allowed.