Thread: Quick Struct Question

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Quick Struct Question

    I'm trying to make an array with the information for five employees. However it's says that my structs aren't correct. I followed the book as much as I could I must be making a dumb mistake somewhere can someone point it out. I'm getting all sorts of errors.




    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct employeeType
    {
    	nameType employee;
    	nameType supervisor;
    	dateType dob;
    	dateType starting;
    	float rate;
    	int id_Num;
    	int hours;
    };
    
    struct nameType
    {
    	string first;
    	char mid;
    	string last;
    };
    
    struct dateType
    {
    	int month;
    	int day;
    	int year;
    
    };

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You can't use a type before it is declared...

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Those are the new variable types. I am confused. Isn't it being declared in the name and date type... That's how the book had it lol

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    It sure is declaring them, even defining them, but you use them before you declare them... If you still don't get it, I will post it completely explicitly

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    wow it was that the declarations had to go first lol. I am restarted at programming. Well now I can start working on my array thanks.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Post up your code now so that anyone who gets stuck on this can just search for something like it on the boards. At least the fixed code for the structs.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    const int MAX = 5;
    
    struct nameType
    {
    	string first;
    	char mid;
    	string last;
    };
    
    struct dateType
    {
    	int month;
    	int day;
    	int year;
    
    };
    
    struct employeeType
    {
    	nameType employee;
    	nameType supervisor;
    	dateType dob;
    	dateType starting;
    	float rate;
    	int id_Num;
    	int hours;
    };
    
    employeeType Database[MAX];
    
    int main()
    {
    	int i;
    
    	for (i = 1; i < MAX; i++)
    	{
    		cout << "Please enter information for employee number " << i << ": " << endl;
    		cin >> Database[i].id_Num >> Database[i].employee >> Database[i].dob >> Database[i].rate >> Database[i].hours 
    			>> Database[i].starting >> Database[i].supervisor;
    	}
    
    	return 0;
    
    }
    Now I'm trying to take all of the information for five employees. Gives me this error.

    "1>c:\users\doug\documents\visual studio 2008\projects\lab3\lab3\lab3.cpp(43) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'nameType' (or there is no acceptable conversion)"

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by GCNDoug View Post

    Now I'm trying to take all of the information for five employees. Gives me this error.

    "1>c:\users\doug\documents\visual studio 2008\projects\lab3\lab3\lab3.cpp(43) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'nameType' (or there is no acceptable conversion)"
    So you need to write a routine that "reads in" a nameType structure. You can overload operator >>, if you want, or you can change your input to read in Database[i].employee.first, and then Database[i].employee.middle, and then Database[i].employee.last (for example). (If it were I writing it, I'd probably test it out by writing it straight in, and then turn that into an overloaded operator >>; but that may seem like way too much work right now for you.)

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler cannot possibly know how to read or write information to/from your custom data types. You need to be explicit and help the compiler in this regard. So you either overload the function (advanced), or read/write into the specific members of your struct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. struct question
    By black_spot1984 in forum C Programming
    Replies: 5
    Last Post: 12-09-2006, 02:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. struct question...
    By Cheeze-It in forum C Programming
    Replies: 3
    Last Post: 11-07-2001, 06:28 AM