Thread: Declare varialbe only once?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    23

    Declare varialbe only once?

    Hello, I am in a begging programing class and I am having trouble with my program. It is a craps game. I have to give the player $100 to start with and then they lose money as they go. The issue i have is... I set chips = 100 at the beginning of the program, but then later on they get chips added or taken away. So is it possible to just declare it once? Any information will help. Thanks, Curtis.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Yes, it's possible, and kinda necessary to only declare it once. Once you declare a variable, you can use it as many times as you want (to read the value, set the value, use it in an expression, etc...) as long as it's in scope. If you declare it inside a function, it's scope ends when the function ends. If you make it global, you can use it in any function. Alternately, you could declare it one function, then pass it as a parameter to other functions that need it, etc... Remember that if you declare the variable in one function to remember how many chips the player has, if a different function declares a variable in a different scope (even with the same name) it's a completely different variable - and you won't be able to track how many chips the player has.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    As mentioned, it really only makes sense to declare a variable once. Note that "declare" is certainly not the same as "assigning" or "changing" the value of a variable. If you want to prevent the value of a variable from changing, i.e. for it to be "constant", then declare it as such
    Code:
    const int answerToLife = 42;
    So that the value of this cannot be changed (and is a compiler error if you try to change it). Going back to declaring a variable only once, if you're a math guy/girl, then the following scenario should be obviously incorrect
    Code:
    Suppose we are graphing the relationship between these variables. Let X be the number of hours I spend practising programming.
    Let Y be the grade I get in the course.
    Let X be the number of hours I spend sleeping.
    Again, it doesn't make sense to declare "X" again.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    I understand all that. What I need is a way that, once the loop finishes it sets the chips back to 100 so is there a way to only have it set to 100 for the first time around? and thank you for the replies

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    So you're game is inside a loop, so that the person can "play again" or "exit", etc? If so, then can't you just assign the value at the beginning of the loop?
    Code:
    char playAgain;
    // ...
    do
    {
    // set number of chips here at start of game
    
    // rest of your game code
    } while ( playAgain == 'y');
    Assuming you ask the user to "play again", etc.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    yes its in a loop here is what ive got.... i just need to figure out a way to where chips=100 isnt valid after the first time through the loop.

    insert
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include"rollone.h"
    
    int main(void){
    double bet, chips, final;
    int roll, roll1, status, point, i;
    
    chips=100;
    chips=final;
    
    printf("* Welcome to my casino. The game this evening *\n");
    printf("* is Craps. (Pass line bets only, but what do *\n");
    printf("* you want from a class project?) *\n");
    printf("* The minumum bet this evening is $2.00\n");
    
    while(final>2) {
    
    printf("You have $%.2f.\n", final);
    printf("How much would you like to bet?");
    scanf("%lf", &bet);
    printf("\n");
    
    if(bet>0 && bet<2) {
    printf("I'm sorry, you can only bet between $2.00");
    printf("and $%.2f (or 0 to quit).\n", final);
    }
    
    if(bet==0) {
    printf("Your final bankroll is $%.2f.\n", final);
    printf("Looks like you had a good night.\n");
    printf("See the cashier on your way out.\n");
    return 0;
    }
    
    srand(time(NULL));
    roll= rollone(12);
    printf("Coming out...%d\n", roll);
    printf("Your point is %d\n", roll);
    
    switch(roll) {
    
    case 7:
    case 11:
    printf("A pass. We have a winner.\n");
    printf("You win $%.2f\n", bet);
    status=1;
    break;
    
    case 2:
    case 3:
    case 12:
    printf("Craps.");
    printf("You lose $%.2f.\n", bet);
    status=2;
    break;
    
    default:
    status=0;
    point=roll;
    break;
    }
    
    while(status==0) {
    srand(time(NULL));
    roll1=rollone(12);
    printf("Coming out...%d\n", roll1);
    printf("Your point is %d\n", roll1);
    if(roll1==point) {
    status=1;
    }
    
    else {
    if(roll==7 || roll==11)
    status=2;
    }
    }
    
    if(status==1) {
    printf("You win $%.2f\n", bet);
    final=chips+bet;
    }
    if(status==2) {
    final=chips-bet;
    }
    }
    
    if(final==0) {
    printf("I’m sorry. You can’t afford another bet.\n");
    printf("Your final bankroll is $%.2f./n", final);
    printf("Better luck next time.");
    
    }
    
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Its quite difficult to read the code if its not indented. In the future, make sure it is indented when you post any code.

    I still think you can just put your code inside an outer loop, like I described above. So that you have an outer loop (the one I described above) which loops the entire game, so that they can play again, the variables and chips can get reset, etc.. Then inside you have your game. It just so happens that your game has a loop in it, too. After they loose, you can ask them if they want to play again, etc..

    Is this even what you're talking about? That is, you want to reset the money and other things because you want them to be able to play again?

    Others will probably point out the actual errors in your code, such as the fact that you are using variables without giving them a value. For example, you declare (again, this does not mean assigning a value) "final", and then you use it in the "while" loop condition ("final > 2"). You must make sure to assign values to all variables before using them. If you're compiling with a verbose warnings option, then you'll see the compiler complaining about doing this. Let's suppose your applying for a job and you ask how much the hourly wage is. They tell you its X dollars. What is X? Maybe its 40, and so a great wage, on the other hand maybe its 5--who knows. This is the same with what you're currently doing in the program when you use variables without assigning them values.

    Also,
    Code:
    chips=100;
    chips=final;
    You assign "chips" the value of 100. Then right after that you overwrite the value of chips, and reassign it the value of whatever "final" is (as stated above, no one knows what "final" contains!). This probably isn't what you want to do.

    I dont know how to play Craps, so I can't help with the actual logic.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Yeah there are specific guidelines i have to follow for class that are the player continues to play until they have <$2 or out by betting $0

  9. #9
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by sean View Post
    Yes, it's possible, and kinda necessary to only declare it once. Once you declare a variable, you can use it as many times as you want (to read the value, set the value, use it in an expression, etc...) as long as it's in scope. If you declare it inside a function, it's scope ends when the function ends. If you make it global, you can use it in any function. Alternately, you could declare it one function, then pass it as a parameter to other functions that need it, etc... Remember that if you declare the variable in one function to remember how many chips the player has, if a different function declares a variable in a different scope (even with the same name) it's a completely different variable - and you won't be able to track how many chips the player has.
    A few corrections
    If you declare it inside a function, it's scope ends when the function ends.
    This is only true if the variable inside the function isn't static.

    If you make it global, you can use it in any function.
    This is only true if the variable has external linkage.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sean
    Yes, it's possible, and kinda necessary to only declare it once.
    Creedy, at the moment you should accept this as-is as the difference between a declaration and a definition is probably not important just yet (and not really relevant to your question), but note that more strictly speaking, some variables can be declared many times, but all must be defined at most once.

    Quote Originally Posted by Creedy
    Yeah there are specific guidelines i have to follow for class that are the player continues to play until they have <$2 or out by betting $0
    It looks like your program might be doing that, but as mentioned it is hard to follow the logic because of the lack of proper indentation. Perhaps you should edit your post and post the indented code in [code][/code] bbcode tags.

    Quote Originally Posted by Overworked_PhD
    This is only true if the variable inside the function isn't static.
    Actually, it is true even if the local variable is declared static. Scope and lifetime are not always the same thing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Okay, maybe you can clarify some possible misconceptions that I have here..

    Let's say that I have variable 'a' with static duration inside a function block. According to ISO/ANSI c88(?), the variable 'a' is just basically a named object. So with that, wouldn't the value of the object remain the same after the function leaves the block? Ie, the variable 'a' itself would cease to exist after the function gets done executing.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "Static" does not affect the scope of a variable.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programmatically declare vectors
    By franse in forum C++ Programming
    Replies: 15
    Last Post: 11-10-2008, 03:17 PM
  2. How to declare a pointer that points to another pointer?
    By yuzhangoscar in forum C Programming
    Replies: 1
    Last Post: 09-15-2008, 06:28 AM
  3. How to declare a list in another list???
    By zaracattle in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2006, 08:24 AM
  4. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  5. declare consts~
    By black in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2002, 10:55 PM