Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
initially our teacher gave this as an example.
told us to check to see if it works,
internet was slow so we ended the class, but i kept working on it
C++
#include <iostream>
#include <stdio.h>

using namespace std;

class structure{

   typedef struct{
        char firstname[20];
        char lastname[20];
        int  age;
    } student_info;

    int main (){
        student_info students[0];

        students[0] = { "Wang", "Dong", 18};
        Students[1] = { "wong", "Dang",19};

        Cout<<"The record of the first student contains"<<student[0].irstname<<student[0].lastname<<student[0].age;

    Return 0;
}};

at first try, it failed an gave this error message:
C++
"
data structures and algorithms\test\main.cpp|9|error: expected unqualified-id before '[' token|
data structures and algorithms\test\main.cpp|10|error: expected unqualified-id before '[' token|"

fixed that as it appears above
then tried again and this appeared
"||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
data structures and algorithms\test\main.cpp||In member function 'int structure::main()':|
data structures and algorithms\test\main.cpp|18|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|
data structures and algorithms\test\main.cpp|18|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|
data structures and algorithms\test\main.cpp|18|error: no match for 'operator=' (operand types are 'structure::student_info' and '<brace-enclosed initializer="" list="">')|
data structures and algorithms\test\main.cpp|13|note: candidate: structure::student_info& structure::student_info::operator=(const structure::student_info&)|
data structures and algorithms\test\main.cpp|13|note: no known conversion for argument 1 from '<brace-enclosed initializer="" list="">' to 'const structure::student_info&'|
data structures and algorithms\test\main.cpp|19|error: 'Students' was not declared in this scope|
data structures and algorithms\test\main.cpp|19|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|
data structures and algorithms\test\main.cpp|21|error: 'Cout' was not declared in this scope|
data structures and algorithms\test\main.cpp|21|error: 'student' was not declared in this scope|
data structures and algorithms\test\main.cpp|23|error: 'Return' was not declared in this scope|
data structures and algorithms\test\main.cpp|24|warning: no return statement in function returning non-void [-Wreturn-type]|
||=== Build failed: 5 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
"
i thinks its coming from "iostream" i don't know how or why?

What I have tried:

i'm new so i don't know what this box is for,but it's required so...
Posted
Updated 15-Sep-20 2:59am
Comments
KarstenK 11-Sep-20 6:00am    
you have some typos in your code. This doesnt like the compiler.

You code looks like a mix of C and C++ programming languages.
Typically, the C programmer writes
C
#include <stdio.h>
  
typedef struct
{
  char firstname[20];
  char lastname[20];
  int  age;
} student_info;

int main ()
{
  student_info student[] = {{ "Wang", "Dong", 18},{ "wong", "Dang",19}};

  printf("The record of the first student contains %s, %s, %d\n", student[0].firstname, student[0].lastname, student[0].age);

 return 0;

}

while the C++ programmer writes
C++
#include <iostream>  
using namespace std;

class student_info
{
  friend ostream & operator <<(ostream & os, const student_info & si);

  string firstname, lastname;
  int age;
public:
  student_info(string firstname, string lastname, int age): firstname(firstname), lastname(lastname), age(age){}
};

ostream & operator <<(ostream & os, const student_info & si)
{
  os << si.firstname << ", " << si.lastname << ", " << si.age;
  return os;
}

int main ()
{
  student_info student [] =  { student_info( "Wang", "Dong", 18), student_info("wong", "Dang",19) };
  cout<<"The record of the first student contains " << student[0] << endl;
}


Please note, both C and C++ are case sensitive, while return is a keyword, Return is NOT a keyword.
 
Share this answer
 
First of all, iostream is the least of your problems. By far!

I'm not quite sure what your teacher wanted to achieve with this task. If your programming skills aren't advanced enough to immediately spot and fix at least a dozen errors before bothering the compiler with this garbage, then your chances of interpreting the compiler messages correctly and fixing the issues appropriately are zero. Most importantly, the code is so wrong there is little chance the compiler produces helpful error messages (but it still does a remarkable job nonetheless)

Here are some tips on fixing code like this:

1. consider the general outline of a program: typically you have some include statements, and some declarations, maybe typedefs and definitions of compound types; then you might have additional functions, and last you have a function main. In your code, all of that is mashed up in a nonsensical class definition: most importantly main() is contained in that class definition, which doesn't make any sense, unless you're programming Java!

2. That class (named "structure") isn't used anywhere. Well it can't possibly be, because that would require the existance of a global function main() which uses that class, or a function called within main() that does so. But even when you move that function out of the class, it doesn't use "structure" - therefore the entire class definition is nonsense. Getting rid of that class is easy: just remove the first line and the closing bracket.

3. As Carlo pointed out above, you should be clear what language you are using, and what kind of code you want to get. Either you go for C, use stdio.h to print out messages, and use old style char arrays and structs, or you use iostream to print out messages using std::cout, include <string> as well, and use std::string instead of char arrays. It doesn't make sense to mix languages and styles, even if it compiles.

4. Once you've removed the class and decided on the target language, you can start using the compiler. Make sure that you tell it what language to use, so it will issue warnings or errors when your code doesn't fit the chosen language.

5. With the compiler messages, the best approach is to just look at the first warning or error, and try to understand what the compiler is telling you. It is a good idea not to ignore warnings, because they often indicate that your code may not be doing what you intended, even though it is syntactically correct. You should ignore warnings only if you fully understand what is causing it, and have confirmed that this doesn't affect the functionality of your code (and even then it would be better to fix it, if you know how). Once you changed the code to address the very first compiler messages, it might be a good idea to simply run the compiler again: sometimes, fixing a single line in your code can eliminate multiple compiler messages! Keep doing this, until all warning and error messages are gone, or at the very least, the code compiles

6. If you can't find out on your own how to fix a specific message, then you can check online reources, such as this web site: here you can show the code that produces the message, and the message text. That is normally enough for us to find out what is wrong and explain what you need to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900