views:

127

answers:

4

I have the following code:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}

But there are errors:

1>------ Build started: Project: array_structures1, Configuration: Debug Win32 ------
1>Build started 8/1/2010 9:26:47 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\array_structures1.unsuccessfulbuild".
1>ClCompile:
1>  array_structures.cpp
1>c:\users\david\documents\visual studio 2010\projects\array_structures1\array_structures1\array_structures.cpp(9): error C2236: unexpected 'struct' 'stud'. Did you forget a ';'?
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.84
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Please help. How can I fix these errors?

+2  A: 

I don't know C++, but maybe:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }; // notice the semicolon
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}
Adrian
A: 

Your code compiles perfectly under g++.

But you could try this:

struct stud {

    int roll;
    char name[10];
    double marks;
};
karlphillip
Code compiles with <<COMPILER X>> does not, and has never, meant that code is standards compliant.
Billy ONeal
+10  A: 

Please read the error message:

error C2236: unexpected 'struct' 'stud'. Did you forget a ';'?

You are missing the semicolon at the end of the struct stud declaration.

wilx
http://blogs.msdn.com/b/oldnewthing/archive/2010/03/24/9983984.aspx
Billy ONeal
A: 

"Did you forget a ';'?" Says it all;

After you declare a struct you must put ";". You can also do this:

struct struct_name {
 struct_variables; } new_str;

This would create the structure and also create a new variable of that struct type.

So, you could easily have done this:

struct  stud{

    int roll;
    char name[10];
    double marks;
    } stud1={1,"ABC",99.9}, stud2={2,"xyz",80.0};

And also, after you create a structure, to declare a variable of that structure type you just have to write


"stud stud1" instead of "struct stud stud1"

Cristy