PDA

View Full Version : Funny error in xcode......again




Quboid
Aug 24, 2007, 06:18 AM
I used windows ansi C compilers for a while since excode didn't have some of the functions that my teachers at school use. But now that we are doing C++ i decided to move to xcode.

Now the second program i wrote (one involving structures). Has an error that i donot recognise. Can anybody help me with this?

Error:
expected initializer before "alumno"
'alumno' was not declared in this scope


SOURCE CODE:

#include<iostream.h>

struct datos{
int ide, edad;
char nombre[30];
float promedio;
}
datos alumno={0,0,"",0.0};

//funciones

void entrar();
void mostrar();

int main(){
entrar();
mostrar();
return 0;
}

void entrar(){
cout<<"ide";
cin>> alumno.ide;
cout<< "nombre";
cin>>alumno.nombre;
}
void mostrar(){
cout<<alumno.ide<<"\n";
cout<<alumno.nombre;
}



thanks!



wittegijt
Aug 24, 2007, 08:37 AM
You need a semicolon at the end of your struct. Also, it is better to use <iostream> instead of <iostream.h>.

Wittegijt.

kainjow
Aug 24, 2007, 09:03 AM
Also don't you need a "using namespace std;" or "std::" prefix on your couts?

kpua
Aug 24, 2007, 11:42 AM
It's been a while since I've used C++, but this is how it has to be done in C:

Regarding this part of the code:

struct datos{
int ide, edad;
char nombre[30];
float promedio;
};
datos alumno={0,0,"",0.0};

In C, you'd either have to typedef the struct like this:

typedef struct _datos{
int ide, edad;
char nombre[30];
float promedio;
} datos;

or prepend 'struct' in the delcaration of the instance of the struct like this:

struct datos alumno={0,0,"",0.0};

Give that a try...