Funny error in xcode......again

Discussion in 'Mac Programming' started by Quboid, Aug 24, 2007.

  1. macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #1
    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!
     
  2. macrumors member

    Joined:
    Feb 18, 2007
    Location:
    Eindhoven
    #2
    You need a semicolon at the end of your struct. Also, it is better to use <iostream> instead of <iostream.h>.

    Wittegijt.
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Also don't you need a "using namespace std;" or "std::" prefix on your couts?
     
  4. macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    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:

    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:

    Code:
    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:

    Code:
    struct datos alumno={0,0,"",0.0};
    Give that a try...
     

Share This Page