# Thread: strange function

1. ## strange function

Code:
```float getCard(float myCard, float myPoints){
myPoints = myCard;
char boolcard = "";
while ( boolcard != "aa" ){
printf("Your current points is %1.1f\n",myPoints);
printf("Do you want to get more Card? type y or n\n",myPoints);
scanf("%s",&boolcard);
if ( boolcard == "y" ) { myCard = assignPoint((int)checkCard()); myPoints =+ myCard; }
else if ( boolcard == "n" ) { break; }

}
return myPoints;
}```
when the while starts it prints, then ask for inserting a value, and he continue asking the same even if i put n or y

2. Code:
`boolcard == "y"`
You can't do comparisons like this (There are several instances of that sort of thing in your function). In this case, "y" is a string literal. You would need to use strcmp(), or more likely, get a char from the user ('y' or 'n') and do:

Code:
`boolcard == 'y'`
Code:
```float getCard(float myCard, float  myPoints){
myPoints = myCard;```
Why even pass myPoints in, if you are going to just destroy it like that with myCard?

3. Code:
```float getCard(float myCard, float myPoints){
myPoints = myCard;
char boolcard = "";
while ( strcmp(boolcard,"aa") != 0 ){
printf("Your current points is %1.1f\n",myPoints);
printf("Do you want to get more Card? type y or n\n",myPoints);
scanf("%s",&boolcard);
if ( strcmp(boolcard,"y") == 0 ) { myCard = assignPoint((int)checkCard()); myPoints =+ myCard; }
else if ( strcmp(boolcard,"n") == 0 ) { boolcard = "aa"; }
}
return myPoints;
}```
first of all ty )))

i try that way but crash the same...

if i use char i get problem the same...

bout myPoints what u mean exactely?

4. 95 C:\Users\Antonio\Desktop\desktop\programmazione\DE V\main es1 1.c [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast

5. Code:
`char boolcard = "";`
You have allocated no space for your user input here. boolcard needs to be an array, if you want to input a string from scanf. Personally, I would ditch that hacky string stuff, and use getchar to get your input from the user, and do direct comparisons apart from using strcmp.

As for mypoints, what I meant is this:

When you have a function such as yours

Code:
`float getCard(float myCard, float myPoints){`
What you are doing (among other things) is declaring the variables myCard, and myPoints. The only difference, functionally, is that these parameters are variables which are initialized externally, whereas any variables declared within the body of the function itself (like what you tried to do with boolcard) are initialized internally. In terms of using them, they are pretty much the same. You can use the value in any of the variables. You can also overwrite that value within the variable with another value. That is what you did with this:

Code:
`myPoints = myCard;`
So basically, you made the two variables myCard, and myPoints available for use within the function, and then you overwrote one with the other, thereby losing the value of the one for the rest of the life of the function. That is why I was indicating that it made no sense to do that.

6. i put something there put it crash the same

7. Originally Posted by xphoenix
i put something there put it crash the same
Code:
```float getCard(float myCard, float myPoints){
myPoints = myCard;
char boolcard = "aq";
while ( strcmp(boolcard, "aa") != 0 ){
printf("Your current points is %1.1f\n",myPoints);
printf("Do you want to get more Card? type y or n\n",myPoints);
scanf("%s",&boolcard);
/*if ( strcmp(boolcard,"ya") == 0 ) { myCard = assignPoint((int)checkCard()); myPoints =+ myCard; }
else if ( strcmp(boolcard,"na") == 0 ) { boolcard = "aa"; }  */
}
return myPoints;
}```
it crash

8. char boolcard[] = "aq";

9. Code:
```float getCard(float myCard, float myPoints){
myPoints = myCard;
char boolcard[] = "";
while ( strcmp(boolcard,"aa") != 0 ){
printf("Your current points is %1.1f\n",myPoints);
printf("Do you want to get more Card? type y or n\n",myPoints);
scanf("%s",boolcard);
if ( strcmp(boolcard,"y") == 0 ) { myCard = assignPoint((int)checkCard()); myPoints += myCard; }
else if ( strcmp(boolcard,"n") == 0 ) { boolcard[] = "aa"; }
}
return myPoints;
}```
ty... but still doesnt run, error compiling in elseif

10. You really need to get a C book*, and learn the language. This is basic stuff you will need to know if you want to program in C. That said,

Code:
`boolcard[] = "aa";`
You cannot assign strings to arrays like that. You need to use a function like strcpy.

* If you cannot afford one, you could try something like this. If you can afford one, be careful of which ones you spend your money on. Some are definitely better than others.

11. Originally Posted by kermit
You really need to get a C book, and learn the language. This is basic stuff you will need to know if you want to program in C. That said,

Code:
`boolcard[] = "aa";`
You cannot assign strings to arrays like that. You need to use a function like strcpy.
Or we could cheat.
Code:
```#include<stdio.h>
int main( void )
{
struct foo
{
char buf[ BUFSIZ ];
} b = { "hello world" };
char array[ BUFSIZ ] = "hello";

*((struct foo *) array) = b;

printf( "%s\n", array );

return 0;
}```
Of course this will totally not work if your destination is smaller than your source. Oh, and you have to have some way of actually getting what you want in your source structure anyway.

Still fun though.

Quzah.

12. Nice Quzah.

13. Originally Posted by kermit
You really need to get a C book*, and learn the language. This is basic stuff you will need to know if you want to program in C. That said,

Code:
`boolcard[] = "aa";`
You cannot assign strings to arrays like that. You need to use a function like strcpy.

* If you cannot afford one, you could try something like this. If you can afford one, be careful of which ones you spend your money on. Some are definitely better than others.
ty all

i have c programming by deitel&deitel, but on strings and vector it really sucks:P i'm gonna reading the website u gave me

14. Originally Posted by xphoenix
ty all

i have c programming by deitel&deitel, but on strings and vector it really sucks:P i'm gonna reading the website u gave me
Pointers on C, by Kenneth A. Reek, has a very good treatment of arrays, strings, and pointers. The only downside is the cost. However a good used copy can be had for a significantly lower price. If you want to find a decent priced used one, have a look here. Anyway, I highly recommend this book.

Popular pages Recent additions