variables never change~

This is a discussion on variables never change~ within the C++ Programming forums, part of the General Programming Boards category; Hi all, please search my code first: #include <iostream> #include <string> class character { public: void showX(); private: int x; ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question variables never change~

    Hi all, please search my code first:

    #include <iostream>
    #include <string>

    class character
    {
    public:
    void showX();
    private:
    int x;
    };
    void character::showX()
    {
    x=0;
    cout << x << endl;
    x++;
    cout << x << endl;
    }
    void main()
    {
    character black;
    black.showX();
    black.showX();
    }

    finally, output shows:
    0 1
    0 1

    it seems the x is initialized after being invoked a second time. it looks a bit like const not variable here, and it confused me much, what cause it be ???
    Never end on learning~

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and it confused me much, what cause it be ???
    Your question confuses me much, what is the problem? Every time showX is called, x is set to 0, printed, incremented, and then printed again. Aside from your ghastly use of undefined behavior, this is correct and predictable.
    Code:
    #include <iostream> 
    #include <string> 
    
    class character 
    { 
    public: 
      void showX(); 
    private: 
      int x; 
    };
    
    void character::showX() 
    { 
      x=0;
      /* Note that I had to change calls to cout,
      ** cin, and endl to include the namespace prefix
      ** since you don't have a using statement. I 
      ** also changed the first call to cout so that it
      ** ends with a space to create the exact output
      ** from your post.
      */
      std::cout << x << ' '; 
      x++; 
      std::cout << x << std::endl; 
    }
    
    // void main is wrong, main returns an int.
    int main() 
    { 
      character black; 
      black.showX(); 
      black.showX(); 
    }
    
    --------
    Output:
    
    0 1
    0 1
    Could you be more specific as to what output you were expecting?

    -Prelude
    My best code is written with the delete key.

  3. #3
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    if you are expecting to get 0 and 1 the first time you call the showX() function and 1 and 2 the second time, then heres your problem. the x variable being private is only used by the showX function. the first time you run through it you set x to 0, cout that, then increment it and cout that. that gives you 0 1. now, if you run it again, it sets x to 0 via the first statement in the function, and couts that, then increments, then couts again. so, if you wanted it to show up 0 1 the first time then 1 2 the second time, make the int x public not private...that will work if you use the same class for your code in the main function. otherwise you should create a new version of showX that returns x. next time be clear on your question
    PHP and XML
    Let's talk about SAX

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Talking

    soooooooooooooooooooooooooooory everyone, I found that it was some mistakes of my own, now it is solved, Cheers~
    Last edited by black; 06-20-2002 at 09:24 PM.
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM

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