By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

nested structure

P: 8
hi this is the code that i have but i seem to have 2 errors and i dont understand were they are so can some one please help me. thanks

this is the code
#include<iostream.h>
#include<conio.h>

struct faculty
{
char fact1;
char fact2;
char fact3;
};

struct dean
{
char dean1;
char dean2;
char dean3;
};

struct seniorlecture
{
char seniorlec1;
char seniorlec2;
char seniorlec3;
};

struct secretary
{
char sec1;
};

void main()
{

faculty uni;
dean uni2;
seniorlecture uni3;
secretary uni4;

uni.fact1='business';
uni.fact2='law';
uni.fact3='engineering';

uni2.dean1='john';
uni2.dean2='sara';
uni2.dean3='jane';

uni3.seniorlec1='sam';
uni3.seniorlec2='pat';
uni3.seniorlec3='tom';

uni4.sec1='kate';

cout<< "Faculty is:"<<uni.fact1<<endl;
cout<< "Dean of department is:"<<uni2.dean1<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec1<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact2<<endl;
cout<< "Dean of department is:"<<uni2.dean2<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec2<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact3<<endl;
cout<< "Dean of department is:"<<uni2.dean3<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec3<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;
}

getche();


and this is the error message that i have

[too many characters in constant]
Nov 5 '06 #1
Share this Question
Share on Google+
14 Replies


P: 4
struct faculty
{
char fact1;
char fact2;
char fact3;
};
faculty uni;
uni.fact1='business';
uni.fact2='law';
uni.fact3='engineering';
Hi There,
According to the declaration above you have a structure named faculty which has three members. Those members are of type character (i.e) it can hold only one character at a time. for (e.g) you can store uni.fact1='a';
So if you want to store a array of characters in fact1 you have to decalre fact1 as character array as
char fact1[50];
now
strcpy(uni.fact,"Business"); will work.
Try this. If you have any problem do let me know,
Thanks and Regards,
Aarthy.
Nov 5 '06 #2

P: 8
ser
could you show me what you mean as it is the first time i'm using c++ and i dont understand how i'm going to add the changes that you have told me to put in the program.
thanks
Nov 5 '06 #3

P: 4
could you show me what you mean as it is the first time i'm using c++ and i dont understand how i'm going to add the changes that you have told me to put in the program.
thanks
Here goes the program,

#include<iostream.h>
#include<conio.h>

struct faculty
{
char fact1[50];
char fact2[50];
char fact3[50];
};

struct dean
{
char dean1[50];
char dean2[50];
char dean3[50];
};

struct seniorlecture
{
char seniorlec1[50];
char seniorlec2[50];
char seniorlec3[50];
};

struct secretary
{
char sec1[50];
};

void main()
{

faculty uni;
dean uni2;
seniorlecture uni3;
secretary uni4;

strcpy(uni.fact1,"business");
strcpy(uni.fact2,"law");
strcpy(uni.fact3,"engineering");

strcpy(uni2.dean1,"john");
strcpy(uni2.dean2,"sara");
strcpy(uni2.dean3,"jane");

strcpy(uni3.seniorlec1,"sam");
strcpy(uni3.seniorlec2,"pat");
strcpy(uni3.seniorlec3,"'tom");

strcpy(uni4.sec1,"kate");

cout<< "Faculty is:"<<uni.fact1<<endl;
cout<< "Dean of department is:"<<uni2.dean1<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec1<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact2<<endl;
cout<< "Dean of department is:"<<uni2.dean2<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec2<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact3<<endl;
cout<< "Dean of department is:"<<uni2.dean3<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec3<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;
}

getch();

So compile the above program and see the result. I have changed the declaration part. Basically in C and c++ you can't assign strings using equal to sign. You have to use strcpy function. Hope this helps you,
Aarthy.
Nov 5 '06 #4

P: 8
ser
i had done what you said and used your program i had more errors then before one of the errors was saying that i can not use the stycp so i took it out and have still 2 errors on both errors it says the following

error C2015: too many characters in constant
Nov 5 '06 #5

P: 8
ser
can someone please help i can not solve this error
Nov 6 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
I am betting where aarthy82 had

"business"

in their code you still have

'business'

as per your original code. "business" is a string constant and can contain any number of printable characters, where as '...' signifies a character constant and can only contain a single character, i.e. 'b' so 'business' has too many characters in it for a character constant.
Nov 6 '06 #7

P: 8
ser
i understand whta you are saying but how can i put it in the code.????
Nov 6 '06 #8

P: 8
ser
i have added some changes but i have know got more errors then before i need help asap as i have to hand this in tommorow. this is teh code that i have so far and i have been trying but i just dont get this c++
[code]
#include<iostream>
#include <string>

struct faculty
{
string fact1;
string fact2;
string fact3;
};

struct dean
{
string dean1;
string dean2;
string dean3;
};

struct seniorlecture
{
string seniorlec1;
string seniorlec2;
string seniorlec3;
};

struct secretary
{
string sec1;
};

int main()
{

faculty uni;
dean uni2;
seniorlecture uni3;
secretary uni4;

uni.fact1="business";
uni.fact2 ="law";
uni.fact3 ="engineering";

uni2.dean1 ="john";
uni2.dean2 ="sara";
uni2.dean3 ="jane";

uni3.seniorlec1 = "sam";
uni3.seniorlec2 ="pat";
uni3.seniorlec3 ="tom";

uni4.sec1 ="kate";

cout<< "Faculty is:"<<uni.fact1<<endl;
cout<< "Dean of department is:"<<uni2.dean1<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec1<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact2<<endl;
cout<< "Dean of department is:"<<uni2.dean2<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec2<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact3<<endl;
cout<< "Dean of department is:"<<uni2.dean3<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec3<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;
}

return0
Nov 8 '06 #9

sicarie
Expert Mod 2.5K+
P: 4,677
ser-

From what I can see, there are only three minor syntax issues with your completed code.

1) the includes

You declare the two includes, and you don't specify a namespace. Merely put underneath those two
Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
2) brackets

Any loops you create should have matching { } brackets. Whenever I code, if I'm going to do a for loop, for instance, I start with for () { } just so I don't forget them. You have a closing bracket for your main() routine above the return 0.


3) statement closure

All complete statements (aside of loops), should have a ';' finishing them off. You left this off on the return 0

Expand|Select|Wrap|Line Numbers
  1.     return 0;
  2. }
  3.  
And your code should compile and print.
Nov 8 '06 #10

P: 8
ser
can someone help this code i just dont understand how i'm going to make the code work i have tried another way but i have some errors and i dont understand what i'm doing wrong.
[code]
#include<iostream.h>
#include<conio.h>


struct faculty
{
char fact1;
char fact2;
char fact3;
};

struct dean
{
char dean1;
char dean2;
char dean3;
};

struct seniorlecture
{
char seniorlec1;
char seniorlec2;
char seniorlec3;
};

struct secretary
{
char sec1;
};

int main()
{

faculty uni;
dean uni2;
seniorlecture uni3;
secretary uni4;

uni.fact1="business";
uni.fact2 ="law";
uni.fact3 ="engineering";

uni2.dean1 ="john";
uni2.dean2 ="sara";
uni2.dean3 ="jane";

uni3.seniorlec1 = "sam";
uni3.seniorlec2 ="pat";
uni3.seniorlec3 ="tom";

uni4.sec1 ="kate";

cout<< "Faculty is:"<<uni.fact1<<endl;
cout<< "Dean of department is:"<<uni2.dean1<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec1<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact2<<endl;
cout<< "Dean of department is:"<<uni2.dean2<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec2<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact3<<endl;
cout<< "Dean of department is:"<<uni2.dean3<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec3<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

}
getche();
Nov 8 '06 #11

sicarie
Expert Mod 2.5K+
P: 4,677
Ok, this is where "reading" comes in handy. I told you the three things that were wrong with your code, just go back and look at them. You didn't even fix the one where I told you what to put and where. I don't know what getche() is, but it should be return 0;
Nov 8 '06 #12

P: 8
ser
dont worry i had to think abit and i have sorted it out so thanks for your help i have now finished the program and it works. thanks again
Nov 8 '06 #13

P: 1
hi this is the code that i have but i seem to have 2 errors and i dont understand were they are so can some one please help me. thanks

this is the code
#include<iostream.h>
#include<conio.h>

struct faculty
{
char fact1;
char fact2;
char fact3;
};

struct dean
{
char dean1;
char dean2;
char dean3;
};

struct seniorlecture
{
char seniorlec1;
char seniorlec2;
char seniorlec3;
};

struct secretary
{
char sec1;
};

void main()
{

faculty uni;
dean uni2;
seniorlecture uni3;
secretary uni4;

uni.fact1='business';
uni.fact2='law';
uni.fact3='engineering';

uni2.dean1='john';
uni2.dean2='sara';
uni2.dean3='jane';

uni3.seniorlec1='sam';
uni3.seniorlec2='pat';
uni3.seniorlec3='tom';

uni4.sec1='kate';

cout<< "Faculty is:"<<uni.fact1<<endl;
cout<< "Dean of department is:"<<uni2.dean1<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec1<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact2<<endl;
cout<< "Dean of department is:"<<uni2.dean2<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec2<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;

cout<< "Faculty is:"<<uni.fact3<<endl;
cout<< "Dean of department is:"<<uni2.dean3<<endl;
cout<< "Senior lecture of department is:"<<uni3.seniorlec3<<endl;
cout<< "Secretary of department is:"<<uni4.sec1<<endl;
}

getche();


and this is the error message that i have

[too many characters in constant]
i think the error is because u cannot have more than 2 chars in a char variable.
Aug 26 '07 #14

P: 51
gsi
Hi,
i think the error is because u cannot have more than 2 chars in a char variable.
A variable of data type ' char ' can never hold more than one character, Internally the character itself is represented as an integer constant (Its corresponding Ascii code as an integer constant).

Thanks,
gsi.
Aug 26 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.