Initializer before int

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
#include<iostream.h>
int i, j, n, t[10];
char s[10] , A[10][10];
struct patrat
{ char p1;
	int p2;
}A
int main()
{

cout<<"n=";cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			cout<<"S["<<i<<"]=";cin>>s[i];
			cout<<"T["<<i<<"]=";cin>>t[i];
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			A[i][j]=i+j-2;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			A[i][j].p1=s[A[i][j]];
			A[i][j].p2=t	[A[i][j]];
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			cout<<"("<<A[i][j].p1<<" , "<<A[i][j].p2<<") ";
		cout<<endl;
}

Can someone explain why i need to use initializer before int ? And what is an initializer :-?
Last edited on
Please use
[code][/code]
tags around your code.

That is just an (admittedly rather unhelpful) error message saying that you're missing a semicolon. The error message is likely to be "expected initializer before int", which is saying the compiler was hoping to find an initializer, but it got an int instead.

The root of this is in the line before, where you have (effectively):
struct patrat { /* ... */ } A // no semicolon

Not only is that A a duplicate, the missing semicolon is what lead to your particular compilation error.

(By the way, an initializer is just a statement that "initializes" a variable, or sets it to a value. It's a little bit confusing because there doesn't have to be an initializer here – a semicolon is equally valid – but you get the error likely because the compiler sees that something else is there but can't recognise it as an initializer.)

Also, I would highly recommend doing away with the global variables, and also updating to a (much) more modern compiler – <iostream.h> is an ancient, non-standard variant from before C++98.
Last edited on
variable on the end of struct is C-ish IMHO. I prefer
patrat A;

But this is just trying to be more c++ style ... it lets you put the types in .h files cleanly.

Also, you will find that you reused the variable A once you fix the above ; issue. You need to use another name.
Topic archived. No new replies allowed.