0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Please use code blocks and indent your code. Code blocks are really easy, just put [ code ] before the code and [ /code ] after it (without the spaces). You have been asked to do this before.

> in this part it keep looping how to stop it??

I don't know. There is no loop there, so it can't be looping. An if statement evaluates the expression it is given and executes the next statement or code block if the expression is true and skips over it if it is false.
It may be that by adding cust[sizeR][sizeC].name into the printf you're getting undefined behaviour. Take that out, it has no beneficial effect. The statement should just be printf("Please enter your name\n");

Don't forget that C counts from zero, not 1, so cust[sizeR][sizeC] doesn't exist. Let me know if you don't understand this and I'll explain it in more detail.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
dky please use code blocks when you have code snippets in the posts
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
Please use code blocks and indent your code. Code blocks are really easy, just put [ code ] before the code and [ /code ] after it (without the spaces). You have been asked to do this before.

> in this part it keep looping how to stop it??

I don't know. There is no loop there, so it can't be looping. An if statement evaluates the expression it is given and executes the next statement or code block if the expression is true and skips over it if it is false.
It may be that by adding cust[sizeR][sizeC].name into the printf you're getting undefined behaviour. Take that out, it has no beneficial effect. The statement should just be printf("Please enter your name\n");

Don't forget that C counts from zero, not 1, so cust[sizeR][sizeC] doesn't exist. Let me know if you don't understand this and I'll explain it in more detail.
i dont understand why saying that cust[sizeR][sizeC] doesn't exit??
i hv a other question :how to initializes the structure members?
i have do like this but cant:
Code:
customer cust[sizeR][sizeC]={"AK65","25-12-2008","23:45","G9","3B"}
what code i need to use??
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Thanks for putting the code block in, it makes it a lot clearer. Code blocks and indentation are to code what punctuation, words and caps are to sentences.
theprecedingsentenceismucheasiertoreadthanthisonen o?

OK. C counts from zero; that's how computers work internally. So when you create an array of size 5, you get 5 elements, but the first is element 0, not element 1.
If the first element was 1 as in other languages, then valid array indices would be 1,2,3,4,5.
But because C counts from zero the valid array indices are 0,1,2,3,4, and element 5 in a 5 item array does not exist, and you should not try to write to it.

As for the initialisation that you're trying to do, the compiler will allow this kind of shortcut for static initialisation, but the array sizes on each side of the "=" must match. cust is a 2D array of customer struct's, with the number of elements equal to sizeR*sizeC. But {"AK65"...} is only one record, not sizeR*sizeC records. (Your int ID[4][5] declaration earlier shows you know how to do static initialisation of a 2D array. It's the same for static initialisation of cust, except that each 0 should be replaced with {"AK65"...}.)

Secondly the compiler has no way to match up the data. There is no customer ID. time is declared as type char, which means you can only store a single character in it, so it can't store "23:45". Same for gate and seatNo.

Thirdly if you ARE trying to assign "AK45" etc to the single entry in the customer array cust[sizeR][sizeC], you need to drop "customer" so that it isn't declaring a new array, and as explained above this entry doesn't exist; the only valid entries are cust[0...sizeR01][0...sizeC-1].

Fourthly runtime assignment isn't allowed in this way; it's just a compile-time shortcut, so you must assign each item separately:
Code:
cust[a][b].id=27;
strcpy(cust[a][b].flightNo,"AK65");
strcpy(cust[a][b].date,"25-12-2008");
... etc
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
Thanks for putting the code block in, it makes it a lot clearer. Code blocks and indentation are to code what punctuation, words and caps are to sentences.
theprecedingsentenceismucheasiertoreadthanthisonen o?

OK. C counts from zero; that's how computers work internally. So when you create an array of size 5, you get 5 elements, but the first is element 0, not element 1.
If the first element was 1 as in other languages, then valid array indices would be 1,2,3,4,5.
But because C counts from zero the valid array indices are 0,1,2,3,4, and element 5 in a 5 item array does not exist, and you should not try to write to it.

As for the initialisation that you're trying to do, the compiler will allow this kind of shortcut for static initialisation, but the array sizes on each side of the "=" must match. cust is a 2D array of customer struct's, with the number of elements equal to sizeR*sizeC. But {"AK65"...} is only one record, not sizeR*sizeC records. (Your int ID[4][5] declaration earlier shows you know how to do static initialisation of a 2D array. It's the same for static initialisation of cust, except that each 0 should be replaced with {"AK65"...}.)

Secondly the compiler has no way to match up the data. There is no customer ID. time is declared as type char, which means you can only store a single character in it, so it can't store "23:45". Same for gate and seatNo.

Thirdly if you ARE trying to assign "AK45" etc to the single entry in the customer array cust[sizeR][sizeC], you need to drop "customer" so that it isn't declaring a new array, and as explained above this entry doesn't exist; the only valid entries are cust[0...sizeR01][0...sizeC-1].

Fourthly runtime assignment isn't allowed in this way; it's just a compile-time shortcut, so you must assign each item separately:
Code:
cust[a][b].id=27;
strcpy(cust[a][b].flightNo,"AK65");
strcpy(cust[a][b].date,"25-12-2008");
... etc

like this?
Code:
cust[sizeR][sizeC].id=100;
	strcpy(cust[sizeR][sizeC].flightNo,"AK65");
	strcpy(cust[sizeR][sizeC].date,"25-12-2008");
	strcpy(cust[sizeR][sizeC].time,"23:45");
	strcpy(cust[sizeR][sizeC].gate,"G9");
i still dont rely understant :

Thirdly if you ARE trying to assign "AK45" etc to the single entry in the customer array cust[sizeR][sizeC], you need to drop "customer" so that it isn't declaring a new array, and as explained above this entry doesn't exist; the only valid entries are cust[0...sizeR01][0...sizeC-1].
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
Quote:
Originally Posted by dky8810 View Post
like this?
Code:
cust[sizeR][sizeC].id=100;
	strcpy(cust[sizeR][sizeC].flightNo,"AK65");
	strcpy(cust[sizeR][sizeC].date,"25-12-2008");
	strcpy(cust[sizeR][sizeC].time,"23:45");
	strcpy(cust[sizeR][sizeC].gate,"G9");
i still dont rely understant :

Thirdly if you ARE trying to assign "AK45" etc to the single entry in the customer array cust[sizeR][sizeC], you need to drop "customer" so that it isn't declaring a new array, and as explained above this entry doesn't exist; the only valid entries are cust[0...sizeR01][0...sizeC-1].
i no ideal for next part!!!
help help!
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
T.T
help pls
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
OK. Count the fingers of one hand. One. Two. Three, four five. Six? No, of course not.

Now count them again, starting at zero instead of one. Zero. One, two, three four. Five? Hopefully no, unless you've grown a new one in the meantime.

That's what I mean when I say in a five element array, elements 0-4 exist, but 5 does not.

So if you are assigning to cust[sizeR][sizeC], when cust is only declared as cust[sizeR][sizeC], then you are doing the equivalent of trying to put a ring on the sixth finger on your hand.

If you can't grasp this then you need to take your tutor aside and tell him you have no idea why an array declared as having five elements only has five elements and why they are numbered 0,1,2,3,4. Getting this kind of understanding is way beyond what can be accomplished in a forum.
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
OK. Count the fingers of one hand. One. Two. Three, four five. Six? No, of course not.

Now count them again, starting at zero instead of one. Zero. One, two, three four. Five? Hopefully no, unless you've grown a new one in the meantime.

That's what I mean when I say in a five element array, elements 0-4 exist, but 5 does not.

So if you are assigning to cust[sizeR][sizeC], when cust is only declared as cust[sizeR][sizeC], then you are doing the equivalent of trying to put a ring on the sixth finger on your hand.

If you can't grasp this then you need to take your tutor aside and tell him you have no idea why an array declared as having five elements only has five elements and why they are numbered 0,1,2,3,4. Getting this kind of understanding is way beyond what can be accomplished in a forum.

#define sizeR 3
#define sizeC 4
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
If cust is defined as customer cust[3][4] then the following array entries exist and can be used:
cust[0][0] cust[0][1] cust[0][2] cust[0][3]
cust[1][0] cust[1][1] cust[1][2] cust[1][3]
cust[2][0] cust[2][1] cust[2][2] cust[2][3]

Which you will see is 3 rows of 4 columns, and neither cust[3][x] nor cust[x][4] are valid. So writing to cust[sizeR=3][sizeC=4] should not be attempted; that location is beyong the end of the array and you would be overwriting memory that belongs to another part of your program.