C++ problem

N=a number introduced by us, and N natural numbers.Display the triplets of numbers read consecutive which have the last number 3 and display their number. One number can be part of multiple triplets.
Example:
for n=10, a=23 5 13 123 6113 7 123 83 553 393 => 3 triplets: 13,123,6113 & 123,83,553 & 83,553,393.
That's what I tried to do but it doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>

using namespace std;

int main()
{
    int a, b, c, n, nr=0;
    cin>>n;
    for(int i=1;i<=n; i++)
    {
        cin>>a;
        cin>>b;
        cin>>c;
        if(a%10==3 && b%10==3 && c%10==3)
        {
            nr++;
        }
    }
    cout<<nr;
    return 0;
}
I'm not 100% certain on what you are trying to do here, but if I understand correctly you want to input n numbers, then look through those numbers and find each unique set 3 numbers that end in 3. Is that right?

If so, to start off you might want to create an array (or other container) of n numbers first. Use prompts to ask the user for input and fill your array (or other container). Then you can go about your logic looking for triplets.

The requirements you have listed for the triplets are pretty vague, do you need every possible combination of triplets (in your example you only listed a partial list of the possible triplets)? Do you need to account for numbers that are used twice (for example what if you have 3 of the same number in your list of n numbers?)?
Yeah, you got it.
But I don't know how to do it... I need to display each set of 3 consecutive numbers that end in 3.
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
#include <iostream>
#include <deque>

int main()
{
    unsigned int n ;
    std::cout << "how many numbers? " ;
    std::cin >> n ;

    // to print out three consecutive numbers that end in three,
    // we need to only look at a 'window' containing the most recent three numbers
    // std::deque - to maintain a queue that we can iterate over
    // http://en.cppreference.com/w/cpp/container/deque
    std::deque< unsigned int > sliding_window ;

    std::cout << "enter " << n << " non-negative integers\n" ;
    int number ;
    for( unsigned int i = 0 ; i < n && std::cin >> number ; ++i )
    {
        if( number%10 == 3 ) // if the number ends in three
        {
            sliding_window.push_back(number) ; // add it to the back of the window

            if( sliding_window.size() == 3 ) // we now have three consecutive numbers ending in three
            {
                static int cnt = 0 ;

                // print out the three numbers
                std::cout << ++cnt << ".  " ;
                for( int v : sliding_window ) std::cout << v << ' ' ;
                std::cout << '\n' ;

                // and remove the oldest number in the window; we are then left with
                // the most recent consecutive two numbers that end in three
                sliding_window.pop_front() ;
            }
        }

        else sliding_window.clear() ; // not ending in three; clear the window
    }
}

http://coliru.stacked-crooked.com/a/6851241fa13c00c4
Hi forta2k, JLBorges's example looks good!
I have a different one; I hope it helps you to clarify the process, which seems very simple!

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
#include <iostream>
#include <vector>

int main(int argc, const char * argv[]) {
    
    int n = 0; ////integer to store: "N=a number introduced by us"
    std::vector<int> numbers; ////integer container to store: "N natural numbers"
    std::vector<int> triplets; ////integer container to store: "triplets of numbers"
    
    std::cout<<"Enter a natural number: "; ////<- Get n number from user
    std::cin>>n;
    
    if(n < 3) ////<- Checking if we have a joker user!
    {
        std::cout<<"\nYour computer has a virus! D:\n.....erasing your disk drive!\n\n";
        return 0; ////<- Finishing program!
    }
    
     std::cout<<"Now you'll enter " << n <<" numbers!\n"; ////<- Get n numbers from user
    for(int i = 0; i < n; ++i)
    {
        std::cout<<"Enter number " << i+1 <<" : ";
        int number;
        std::cin>>number;
        numbers.push_back(number);
    }
    
    for(size_t i = 0; i < numbers.size(); ++i) ////<- We'll check each number and- 
////the next two consecutive numbers (only if there is two more numbers to check)
    {
       if(i+2 < numbers.size()) ////<- Checking if there is two more numbers than-
//// the current number
       {  ////Checking a,b,c numbers are triplets?
           if(numbers[i]%10 == 3 && numbers[i+1]%10 == 3 && numbers[i+2]%10 == 3) 
           {
               ////<- then: store these 3 numbers in "triplets" container!
               triplets.push_back(numbers[i]);
               triplets.push_back(numbers[i+1]);
               triplets.push_back(numbers[i+2]);
           }
        
       }
    }
    
    ////Calculating the number of triplets!
    std::cout<<"\nNumber of triplets: " << triplets.size()/3 <<'\n'; 
    
    int counter = 1;
    for(const auto number : triplets) ////<- We'll iterate through our triplets container
    {
        if(counter%3 != 0)////<- if this is not the third number print number + ',' comma
            std::cout<<number <<',';
        else
////if this is the third number print number and a new line
            std::cout<<number <<'\n'; 
        
        ++counter; ////<- pour counter he has the most boring job :(
    }
    
    return 0;
}




Enter a natural number: 10
Now you'll enter 10 numbers!
Enter number 1 : 23
Enter number 2 : 5
Enter number 3 : 13
Enter number 4 : 123
Enter number 5 : 6113
Enter number 6 : 7
Enter number 7 : 123
Enter number 8 : 83
Enter number 9 : 553
Enter number 10 : 393

Number of triplets: 3
13,123,6113
123,83,553
83,553,393



Enter a natural number: -1

Your computer has a virus! D:
.....erasing your disk drive!
I will use eyenrique's solution. Thanks to you both guys for helping me!
Topic archived. No new replies allowed.