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:17:40
|
Hi,
You are making a mistake when you try to declare all your structs :
for (i=1;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 end of each
iteration of the loop they will be destroyed.
Therefore these references to the structs are invalid.
1.name = "hydrogen";
1.atomic_no = 1;
1.atomic_weight = 1.007794;
[...]
Also, you should note that variable names cannot begin with numbers:
the compiler saw number, period, something, and interpreted that as a
floating point number, except the 'something' wasn't the rest of a
float.
in the rest of your program, you test against each struct in a if/elseif ,
this is a good place to use a loop.
A better way of writing the program would be:
A. declare the struct and give it a name (eg if you want to make a new one
sometime, you don't have to rerite the struct declaration) so:
typedef struct _Element {
//members
} Element;
You can now use this stuct by using 'struct _Element' or 'Element'
B. declare an array of these structs. I trust you'll manage this
C. fill the structs, referencing the elements in the array:
elements[1].name = "hydrogen";
and so on
D. you're ready to roll: get input, loop through the array, testing the
names
hope this will point you in the right direction.
Cheers,
Jonathan
PS. Sorry for the double post, bad aiming for a click....
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 the
> 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=1;i < 5;++i) {
> struct elements{
> string name;
> int atomic_no;
> float atomic_weight;
> }i;
> }
> 1.name = "hydrogen";
> 1.atomic_no = 1;
> 1.atomic_weight = 1.007794 ;
> 2.name = "helium";
> 2.atomic_no = 2;
> 2.atomic_weight = 4;
> 3.name = "lithium";
> 3.atomic_no = 3;
> 3.atomic_weight = 6.941;
> 4.name = "berylium";
> 4.atomic_no = 4;
> 4.atomic_weight = 9.012182;
> 5.name ="boron";
> 5.atomic_no = 5;
> 5.atomic_weight = 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 == 1.name) {
>
> cout << title << 1.name;
> cout << no << 1.name << is << 1.atomic_no;
> cout << wgt << 1.atomic_weight;
> }
>
> else if (element == 2.name) {
>
> cout << title << 2.name;
> cout << no << 2.name << is << 2.atomic_no;
> cout << wgt << 2.atomic_weight;
> }
>
> else if (element == 3.name) {
>
> cout << title << 3.name;
> cout << no << 3.name << is << 3.atomic_no;
> cout << wgt << 3.atomic_weight;}
>
> else if (element == 3.name) {
>
> cout << title << 3.name;
> cout << no << 3.name << is << 3.atomic_no;
> cout << wgt << 3.atomic_weight;}
>
> else if (element == 4.name) {
>
> cout << title << 4.name;
> cout << no << 4.name << is << 4.atomic_no;
> cout << wgt << 4.atomic_weight;}
>
> else if (element == 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")) ;
>
>
>
> }
>
|