Re: [Dev-C++] Const member in a Struct
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jonathan W. <jon...@gm...> - 2007-07-28 20:05:13
|
Hi,
You are making a mistake when you try to declare all your structs :
for (i=3D1;i < 5;++i) {
struct elements{
string name;
int atomic_no;
float atomic_weight;
} i;
}
This will not declare 5 structs, called '1' through '5', but all called 'i'=
.
They will also be local to the inside of the loop: at the ed of each
iteration of the loop thez will be destroyed.
Therfore these references to the structs are invalid
1.name =3D "hydrogen";
1.atomic_no =3D 1;
1.atomic_weight =3D 1.007794;
2.name =3D "helium";
2.atomic_no =3D 2;
2.atomic_weight =3D 4;
3.name =3D "lithium";
3.atomic_no =3D 3;
3.atomic_weight =3D 6.941;
4.name =3D "berylium";
4.atomic_no =3D 4;
4.atomic_weight =3D 9.012182;
5.name =3D"boron";
5.atomic_no =3D 5;
5.atomic_weight =3D 10.811;
string element;
string title("\n\t You have selected :\t");
string no("\n\t The atomic number for ");
string is(" is:\t");
string wgt("\n\t The atomic weight is:\t");
printf ("\t The Begining of the Periodic Table of the Elements \n");
printf ("\t In the hopes of learning C programming and elemental values.
\n\n\n");
do {
cout << "\n\t Please enter an Element you would like to examine : " ;
cin >> element;
if (element =3D=3D 1.name) {
cout << title << 1.name;
cout << no << 1.name << is << 1.atomic_no;
cout << wgt << 1.atomic_weight;
}
else if (element =3D=3D 2.name) {
cout << title << 2.name;
cout << no << 2.name << is << 2.atomic_no;
cout << wgt << 2.atomic_weight;
}
else if (element =3D=3D 3.name) {
cout << title << 3.name;
cout << no << 3.name << is << 3.atomic_no;
cout << wgt << 3.atomic_weight;}
else if (element =3D=3D 3.name) {
cout << title << 3.name;
cout << no << 3.name << is << 3.atomic_no;
cout << wgt << 3.atomic_weight ;}
else if (element =3D=3D 4.name) {
cout << title << 4.name;
cout << no << 4.name << is << 4.atomic_no;
cout << wgt << 4.atomic_weight;}
else if (element =3D=3D 5.name) {
cout << title << 5.name;
cout << no << 5.name << is << 5.atomic_no;
cout << wgt << 5.atomic_weight;}
cout << "\n\n\t Again Yes/No :";
cin >> x;}
while (!strcmp(x,"yes")) ;
}
On 7/28/07, J Hall <jsh...@ho...> wrote:
>
> Here is a program that I have been working on.
> when I compile, i get an invalid suffix on floating const error on all th=
e
> elements of the members of struct elements.
> Is this the type of situation that a pointer would help?
> It seems that since the periodic table has numbers 1-112, I ought to be
> able to write a loop
> that gives the struct elements the members 1-112 ,
> any ideas?
>
>
>
>
>
>
> using namespace std;
> int i,e;
>
> char x[3];
> main()
>
> {for (i=3D1;i < 5;++i) {
> struct elements{
> string name;
> int atomic_no;
> float atomic_weight;
> }i;
> }
> 1.name =3D "hydrogen";
> 1.atomic_no =3D 1;
> 1.atomic_weight =3D 1.007794 ;
> 2.name =3D "helium";
> 2.atomic_no =3D 2;
> 2.atomic_weight =3D 4;
> 3.name =3D "lithium";
> 3.atomic_no =3D 3;
> 3.atomic_weight =3D 6.941;
> 4.name =3D "berylium";
> 4.atomic_no =3D 4;
> 4.atomic_weight =3D 9.012182;
> 5.name =3D"boron";
> 5.atomic_no =3D 5;
> 5.atomic_weight =3D 10.811;
>
>
> string element;
> string title("\n\t You have selected :\t");
> string no("\n\t The atomic number for ");
> string is(" is:\t");
> string wgt("\n\t The atomic weight is:\t");
> printf ("\t The Begining of the Periodic Table of the Elements \n");
> printf ("\t In the hopes of learning C programming and elemental values.
> \n\n\n");
> do {
> cout << "\n\t Please enter an Element you would like to examine : " ;
> cin >> element;
>
> if (element =3D=3D 1.name) {
>
> cout << title << 1.name;
> cout << no << 1.name << is << 1.atomic_no;
> cout << wgt << 1.atomic_weight;
> }
>
> else if (element =3D=3D 2.name) {
>
> cout << title << 2.name;
> cout << no << 2.name << is << 2.atomic_no;
> cout << wgt << 2.atomic_weight;
> }
>
> else if (element =3D=3D 3.name) {
>
> cout << title << 3.name;
> cout << no << 3.name << is << 3.atomic_no;
> cout << wgt << 3.atomic_weight;}
>
> else if (element =3D=3D 3.name) {
>
> cout << title << 3.name;
> cout << no << 3.name << is << 3.atomic_no;
> cout << wgt << 3.atomic_weight;}
>
> else if (element =3D=3D 4.name) {
>
> cout << title << 4.name;
> cout << no << 4.name << is << 4.atomic_no;
> cout << wgt << 4.atomic_weight;}
>
> else if (element =3D=3D 5.name) {
>
> cout << title << 5.name;
> cout << no << 5.name << is << 5.atomic_no;
> cout << wgt << 5.atomic_weight;}
>
> cout << "\n\n\t Again Yes/No :";
> cin >> x;}
> while (!strcmp(x,"yes")) ;
>
>
>
> }
>
>
>
> ------------------------------
> See what you're getting into=85before you go there. Check it out!<http://=
newlivehotmail.com>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
--=20
<Morpheus> linux, c'est une question de VI ou de MORE
|