new total and carrying it over.

This is a discussion on new total and carrying it over. within the C++ Programming forums, part of the General Programming Boards category; what I'm looking for is this: to have the new total of a persons money carried over into the new ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    new total and carrying it over.

    what I'm looking for is this:
    to have the new total of a persons money carried over into the new game. here is some code i did with out the carrying over.

    Code:
    if ( gameStatus == WON ){
           w = x * 2;
           cout << "You have won: " << w << endl;
          newtotal = w + total;
           cout << "You now have " << newtotal << " dollars. ";
           newtotal = total;
           }
    sorry if its badly writen.. im still new.. but what i thought was that the newtotal = total; would over write the old total. and carry it to the next game.. but it doesnt. any ideas?

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    How about total = newtotal;?

    Would be my guess, but hard to tell from just this code.

  3. #3
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    You have your LValue backwards.
    You should have
    Code:
    total = newtotal;
    The value on the left becomes equal to the value on the right.
    Thus, you want total to be equal to newtotal, and not vice versa.

    LVALUES/RVALUES
    C++ has the notion of lvalues and rvalues associated with variables and constants. The rvalue is the data value of the variable, that is, what information it contains. The "r" in rvalue can be thought of as "read" value. A variable also has an associated lvalue. The "l" in lvalue can be though of as location, meaning that a variable has a location that data or information can be put into. This is contrasted with a constant. A constant has some data value, that is an rvalue. But, it cannot be written to. It does not have an lvalue.

    Another view of these terms is that objects with an rvalue, namely a variable or a constant can appear on the right hand side of a statement. They have some data value that can be manipulated. Only objects with an lvalue, such as variable, can appear on the left hand side of a statement. An object must be addressable to store a value.

    Here are two examples.
    int x;

    x = 5; // This is fine, 5 is an rvalue, x can be an lvalue.
    5 = x; // This is illegal. A literal constant such as 5 is not
    // addressable. It cannot be a lvalue.
    Taken from http://cplus.about.com/od/beginnerct.../aa021202a.htm
    Last edited by CrazyNorman; 10-21-2005 at 07:46 PM. Reason: Fixed bold tags
    Programming Your Mom. http://www.dandongs.com/

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Code:
     if ( gameStatus == WON ){
           w = x * 2;
           cout << "You have won: " << w << endl;
          newtotal = w + total;
           cout << "You now have " << newtotal << " dollars. ";
           newtotal = total;
           }
            
           cout << "New Game? (y/n)" << endl; 
           cin >> newGame; 
          } while (newGame == 'y' || newGame == 'Y');
                
       return 0;
       
    }
              
    int rollDice( void )
    {
        int die1, die2, workSum;
        
        die1 = 1 + rand() %6;
        die2 = 1 + rand() %6;
        workSum = die1 + die2;
        cout << "player rolled " << die1 << " + " << die2
             << " = " << workSum << endl;
        
        system("PAUSE"); 
        return workSum;
    there more if ya need it... but all im reallya sking is how would i bring the new total over to the next game. since after it asks for a new game, it clears the total data back to 100.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Take the intialization of total out of the loop.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    thanks sly once again

    *edit* and crazy norman, i did fix that before i saw your post. but thank you any way.
    Last edited by Gardul; 10-21-2005 at 08:14 PM.

Popular pages Recent additions subscribe to a feed

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