Wrong output

// t is the number of test cases.

When I input t, I get a output of 0 first.
Then I am asked to enter the strings 2 times as there are 2 test cases.

The program takes strings as input. These strings is a sequence of + and -. the sequence is called chain if each two neighboring symbols of sequence are either '-+' or '+-'.
calculate the minimum number of symbols he need to replace (ex. '-' to '+' or '+' to '-') to receive a Chain sequence.
and output contains that minimum number.

Why am I getting a output of 0? I want to get rid of it.

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
65
#include <iostream>
#include <cmath>



using namespace std;

int main()
{
    
    int t;
    cin >> t;
    t++;
    while(t--)
    {
    const long k = pow(10,5);
    char input[k];
    
    cin.getline(input,k);
  
    int count = 0;
    
    if (input[0] == '+')
    {
        for (int i = 2; i < k; i = i + 2)
        {
            if (input[i] == '-')
            {
                count++;
            }
        }
        for (int i = 1; i < k; i = i + 2)
        {
            if (input[i] == '+')
            {
                count++;
            }
        }
    }
        
        
    else if (input[0] == '-')
    {
        for (int i = 2; i < k; i = i + 2)
        {
            if (input[i] == '+')
            {
                count++;
            }
        }
        for (int i = 1; i < k; i = i + 2)
        {
            if (input[i] == '-')
            {
                count++;
            }
        }
        

    }
        cout << count << endl;
    
    }
}
Thanks. The problem was due to the presence of a newline character in the buffer. I cleared it with cin.ignore();
I want to input a string whose maximum length could be pow(10,5).
And input into the string must be terminated when a newline is recognised.

How can I calculate the total number of characters entered into the string?
I am using a character string.
How can I calculate the total number of characters entered into the string?
getline has a second parameter: size of buffer. Use it to limit size of input
http://en.cppreference.com/w/cpp/io/basic_istream/getline
Topic archived. No new replies allowed.