on filling a string array it stores from 1 index not 0 why

Write your question here.

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
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
	int n;
	while(true){
		cin>>n;
		if(n !=0){
		vector<int> len;
		string x[1000];
		for(int i = 0 ;i < n;i++){
			getline(cin,x[i]);
		}
		cout<<x[0]<<endl;
		cout<<x[0]<<endl;
		for(int i = 0 ;i < n ; i++){
			int count=0;
			int j =1;
			while(x[i][j]=='X' && j < 25)j++;
			while(x[i][j++]=='B'&& j < 25)count++;
			len.push_back(count);
		}
		int min =27;
		for(int i = 0 ; i < n ; i++){
			if(len.at(i) < min)min =len.at(i);
		}
		int cnt=0;
		for(int i = 0 ; i < n ; i++){
			cnt+=(len.at(i)-min);
		}
		cout<<cnt<<endl;}
		else break;
	}
	
}


x[0] returns void
x[1] returns the first input string


on changing the i to 1 the code is okey and ready to go but whyy ?
You are mixing the extraction operator >> (on line 8) with getline (on line 13).

The extraction operator takes the number that you put in ... but doesn't clear the linefeed. So the first getline() call clears the rest of the line (which is probably blank) up to that first linefeed.

They are difficult to mix: either accept it and use a dummy getline statement to dump the rest of the line after the cin statement, or (probably better here) use getline for everything here and stringstream the first input into n.

Your program would be a lot easier for others to use if it had some prompts and the indentation was consistent.
Last edited on
uha .. thanks <3 , i will keep that in mind ^^.
What is in the input?

Line 8 reads an integer. (formatted input)
Line 13 reads up to next newline character and stores data to x[0] (on first iteration). (unformatted input)

What is in the input?
2
hello
world

Lets illustrate newlines in that:
2\n
hello\n
world\n

Lets read the integer. What remains:
\n
hello\n
world\n

The first line to read has only newline.

Lets type input differently:
2 hello\n
world\n

After reading integer:
 hello\n
world\n

Alas, x[0] will start with a whitespace.

Mixing formatted and unformatted input is tricky. You should somehow erase all whitespace after the integer before the first "string". See http://www.cplusplus.com/reference/istream/ws/


Why do you loop (on lines 20 and 21) up to 25?
How do you know that a string has 26 characters?
How do you know that a string has even 1 character? Your x[0] has probably 0 (depending on your input).
sorry i shoulda have linked the problem
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=355

it's specified that each line has a length of 25
i made it as an array of strings so the resulting matrix is N*25

the sample input is
4
XXXXBBBBBBBBBBBBBBBBXXXXX
XXXBBBBBBBBBBBBBBBXXXXXXX
XXXXXBBBBBBBBBBBBBBBBXXXX
XXBBBBBBBBBBBBBBBBBXXXXXX
2
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXBBBBBBBBBBBBBBXX
0
i should pay more attention to the I/O thanks for the link :)
it's specified that each line has a length of 25

If that is true, then size() returns 25 for each string.
If it is not true, the size() will still return a safe value.

Literal constants in the code (like the 25) are referred to as "magic". It is impossible to tell from a number, what does it represent. If the code has unrelated numbers that happen to have same value the reading becomes even more difficult. If the value should change, you would have to edit each occurrence of the magic constant.
Topic archived. No new replies allowed.