Ameen Izhac
Ameen Izhac

Reputation: 29

why can I not include 2 variables in the first statement of a for loop

I keep getting the error: array must be initialised with a brace-enclosed initialiser

This is the code:

for (int i=1, seporated_words[0]=sentence[0]; sentence[i]!=' '; i++)
{
seporated_words[0]=seporated_words[0]+sentence[i];
}
cout << seporated_words[0];
}

Does anyone know why I'm getting this error?

Upvotes: 0

Views: 66

Answers (2)

MRazian
MRazian

Reputation: 88

you have two closed braces!

The first part of for loop runs only once. You can place seporated_words[0]=sentence[0] before for.

Upvotes: 0

SergeyA
SergeyA

Reputation: 62563

Such are the syntax rules of C++. int i = 1, seporated_words[0]... tries to define two variables - an integer i and an array seporated_words, with size of the array 0. Following = ... is seen by a compiler as initialization of the said array, and an incorrect one (still wouldn't be correct to have a zero-sized array, but compiler is not there to report it yet).

You should either set seporated_words to desired value before the loop statement - it seems to be external to the loop anyways, or make i an external variable, and initialize it in the loop initialazer together with seporated_words.

Btw, you have a typo - it is spelled separated.

Upvotes: 3

Related Questions