strange function

This is a discussion on strange function within the C Programming forums, part of the General Programming Boards category; Code: float getCard(float myCard, float myPoints){ myPoints = myCard; char boolcard = ""; while ( boolcard != "aa" ){ printf("Your ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    46

    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. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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?
    Last edited by kermit; 07-01-2010 at 06:21 PM.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    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?
    Last edited by xphoenix; 07-01-2010 at 06:42 PM.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    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. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.
    Last edited by kermit; 07-01-2010 at 06:53 PM.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    i put something there put it crash the same

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    Quote Originally Posted by xphoenix View Post
    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. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    char boolcard[] = "aq";

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    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. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.
    Last edited by kermit; 07-01-2010 at 07:25 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kermit View Post
    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.
    Hope is the first step on the road to disappointment.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Nice Quzah.

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    Quote Originally Posted by kermit View Post
    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. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by xphoenix View Post
    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 subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21