Problem: Creating a variable

Hi!
I am vey new to C++ and I hope you can help me. Here is what I want to do: I want to create a new vector inside a block, under centain circumstances ("if ()"), with a length of "R" (R being a predefined variable), and I want it to be visible for the rest of the blocks.
Here is what I have managed to do:

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
int R;
cin>>R;
char p;
cin>>p;
if (p=='s' || p=='y')
	long double *v2=new long double[R];
	{
	if (*Idm=='e')
		cout<<endl<<"Introduzca v2"<<endl;
	else if (*Idm=='i')
		cout<<endl<<"Introduce v2"<<endl;
	}

	{
	for (i=0; i<R; i++)
		cin>>v2[i];
	}
	{
	cout<<"v2: [ ";

	for (i=0; i<R; i++)
		cout<<v2[i]<<" ";
	}
	{
	cout<<"]";
	}

else if (p=='n')
	if (*Idm=='e')
		cout<<endl<<"Listo"<<endl;
	else if (*Idm=='i')
		cout<<endl<<"Ready"<<endl;


(With Idm being the language variable: i for English, e for Spanish)
Here is the problem: I want a *v2 variable to exist only if the user types y or s, also, I want *v2 to be used later in the program so it has to be visible outside the "if". Another problem: my complier (Turbo C++) tells me that the line "else if (p=='n') is misplaced, here I cant find what is wrong.

Thanks,
Benjamin.
You can't. You have to declare it beforehand for it to be accessible later like that.

As for the second error, you need { } around that if statement, otherwise it will only span one block.

Also, Turbo C++ is REALLY old, I suggest you go update to a newer (and probably far more standards complaint compiler): http://www.cplusplus.com/forum/articles/7263/
Thank you very much firedraco.

The function of this program would be a "vector calculator", allowing arithmetic operation such as vector sum or product. What I want to do is to create and store a new vector every time the user requires it. The vectors should be visible from other blocks to allow arithmetic operations with them. Is there a standard way to do this?.

Thanks,
Benjamin.
Topic archived. No new replies allowed.