Thread: C++: How can I update a variable

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by cpjust View Post
    I don't know if the code was updated since you posted this, but the if statements look indented to me (albeit a little too indented for my taste).
    Sure, but look at this:

    Code:
    	if (build=="1"||"build 1") {
    		(b=10); // inc. att. +5
    		(a=3); // inc. blo. +1
    		(d=9); // inc. secondary att. +4
    		(c=2); // inc. secondart blo. +0
    		(x=120); // inc. hp +20
    	while (x >= 1&&y >= 1) {
    	cout<<"do you want to hit or block "<<e1<<"? ";
    	cin>>answer;
    	if (answer=="hit") {
    Indenttion is messed up because while is on the same level as the first if. And the second if is at the same level as the while. So indentation is messed up.

    You're right, it is a bit strange to see this type of initialization for primative types like int, but as for classes, I use constructor initialization as much as possible rather than assignment since it just seems like a waste to default construct an object and then change the state right afterwards.
    But you can also do:
    Code:
    MyObject obj = myvalue;
    Unless the constructor is explicit. It will still call the constructor.
    For objects, it's usually a little better, but yeah... it looks kind of messed up sometimes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia
    Indenttion is messed up because while is on the same level as the first if.
    Oh, you're right. I didn't even notice that the while was inside the if block. I guess that just proves how hard it is to read when not indented properly.

    Quote Originally Posted by Elysia
    But you can also do:

    Code:
    MyObject obj = myvalue;
    Of course you can do something like:
    Code:
    string str = "Hello World";
    but unless the compiler can figure out how to optimize that, it still has to call the default constructor, then the assignment operator; so I prefer to do:
    Code:
    string str( "Hello World" );
    but for primitive types I just use:
    Code:
    int num = 5;

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by cpjust View Post
    Of course you can do something like:
    Code:
    string str = "Hello World";
    but unless the compiler can figure out how to optimize that, it still has to call the default constructor, then the assignment operator; so I prefer to do:
    Code:
    string str( "Hello World" );
    Well sure, but in that case it will complain, so you'll have to use the different syntax. When it's just one argument, it usually works fine!
    And besides, string str("Hello World") looks much better and not as confusing as int n(5), so it's alright!

    but for primitive types I just use:
    Code:
    int num = 5;
    Same here
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    but unless the compiler can figure out how to optimize that, it still has to call the default constructor, then the assignment operator;
    I think that only the constructor that takes a null terminated string will be called. You're confusing it with assignment, where indeed the constructor will be called, then the assignment operator, unless optimisation takes place.

    EDIT:
    Actually, I suspect optimisation is not possible with the copy assignment operator. One may have to treat it as a special case by providing an assignment operator that takes a null terminated string, but std::string has no such assignment operator, if I remember correctly.

    EDIT #2:
    Haha, std::string does have an assignment operator that takes a const char*. Oh well.
    Last edited by laserlight; 12-13-2007 at 12:28 PM.
    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

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by nekkron View Post
    For another part then, iMalc, what do you mean when you say "four identical funtions"? Are you refering to these parts?

    Code:
    int subtract(int x, int y)
    {
     return x-y;
    }
    Because even from my very limited knowledge, each function there is doing something different; they are all decreasing the x and a values depending on which word I use 'hit' or 'block' when I test it currently it works like I said.
    I certainly am referring to those functions. They do not do what you just described. Every one of them simply returns the result of subtraction between any two supplied integers. Trust me, all four of them did EXACTLY the same thing! Your limited knowledge, and intuition, were both failing you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The only thing different between those 4 functions are the variable names for the parameters, which makes absolutely no difference to the compiler since it doesn't care about names.

  7. #22
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    nekkron,

    Here's the thing about functions - When you "pass-in" a variable, you don't actually pass-in the variable itself. You pass-in a copy of it's value. So, if x=2 and y=3, then the values 2 and 3 get passed-in.


    And, you can pass variables A & B into this function: int subtract(int x, int y). Once you've created your subtract() function, you can use it to subtract any two integer variables!

    When you pass-in A & B, their values are assigned to new local variables x & y... The variables inside the function are are new local variables. If you have other x & y variables in your program, you can change x & y inside the function and the other x & y variables will not be affected by the function.



    Code:
    -------------------------------------------------------------------------
    int subtract(int x, int y); //  Function prototype
    int subtract(int, int);       //  Another way to write the function prototype
    -------------------------------------------------------------------------
    
    
    -------------------------------------------------------------------------
    int subtract(int x, int y)  // Function definition
    {
     return x-y;
    }
    -------------------------------------------------------------------------
    
    
    -------------------------------------------------------------------------
    D = subtract(x, y);  //   Function Call. Passes-in x & y and assigns subtraction result to D
    E = subtract(a, b);  //   Calls same subtract() function, passes-in a & b 
    F = subtract(a, c);  //    Calls same subtract() function, passes-in a & c 
    Z = subtract(a, 4);  //   Calls same subtract() function, passes-in a & 4 
    
    -------------------------------------------------------------------------

    As has been pointed-out, you don't need to create a function to subtract two numbers, since you can simply use the subtraction operator (the minus sign). There is no problem making an integer subtraction function as a learning exercise, but there is no reason to use it in a real world program.

    A function is sort-of like a subcontractor. You use a function to do a particular job. You can use a function if you want to do something several times at different points in your program, and you don't want to repeat the same code over-and-over. You can also use functions to do special or complicated tasks. Putting these tasks into a function avoids cluttering-up your main() function with lots of details. And, if you are working a large project, different programmers can write different sets of functions.

    NOTE - These are just the basics - This is not everything you need to know about functions! Most C++ books will have a chapter or two (or more) on functions.
    Last edited by DougDbug; 12-13-2007 at 08:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 08:00 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM