Results 1 to 3 of 3
  1. #1
    utherson601's Avatar
    Join Date
    Oct 2010
    Gender
    male
    Location
    Behind Closed Doors
    Posts
    55
    Reputation
    10
    Thanks
    7
    My Mood
    Yeehaw

    C++ modifying variables from outside main()

    i was wondering how to modify the following to make the loop end without using "return".
    in otherwords im asking how to use "meow()" to directly edit "main()"s int1
    Code:
    void meow(int int1)
    {
    	int1 += 10;
    	cout << int1 << endl;
    }
    
    
    int main()
    {
    	int int1 = 5;
    	while(int1 < 100)
    	{
    	meow(int1);
    	}
    
       system("pause");
        return 0;
    }
    "He who joyfully marches to music rank and file, has already earned my contempt. He has been given a large brain by mistake, since for him the spinal cord would surely suffice"-Eistein

  2. #2
    radnomguywfq3's Avatar
    Join Date
    Jan 2007
    Gender
    male
    Location
    J:\E\T\A\M\A\Y.exe
    Posts
    8,858
    Reputation
    381
    Thanks
    1,823
    My Mood
    Sad
    Quote Originally Posted by utherson601 View Post
    i was wondering how to modify the following to make the loop end without using "return".
    in otherwords im asking how to use "meow()" to directly edit "main()"s int1
    Code:
    void meow(int int1)
    {
    	int1 += 10;
    	cout << int1 << endl;
    }
    
    
    int main()
    {
    	int int1 = 5;
    	while(int1 < 100)
    	{
    	meow(int1);
    	}
    
       system("pause");
        return 0;
    }
    Use a reference.

    Code:
    void meow(int& int1)
    {
    	int1 += 10;
    	cout << int1 << endl;
    }
    
    
    int main()
    {
    	int int1 = 5;
    	while(int1 < 100)
    	{
    	meow(int1);
    	}
    
       system("pause");
        return 0;
    }
    You can also use a pointer (less desired method in C++, but only method in C):


    Code:
    void meow(int* int1)
    {
    	*int1 += 10;
    	cout << *int1 << endl;
    }
    
    
    int main()
    {
    	int int1 = 5;
    	while(int1 < 100)
    	{
    	meow(&int1);
    	}
    
       system("pause");
        return 0;
    }
    However, this deafeat the purpose of a function, which is to be more or less self-contained. Rarely should you use references in this sort of context.
    Last edited by radnomguywfq3; 09-09-2012 at 11:25 PM.



    There are two types of tragedies in life. One is not getting what you want, the other is getting it.

    If you wake up at a different time in a different place, could you wake up as a different person?


  3. The Following User Says Thank You to radnomguywfq3 For This Useful Post:

    utherson601 (09-09-2012)

  4. #3
    utherson601's Avatar
    Join Date
    Oct 2010
    Gender
    male
    Location
    Behind Closed Doors
    Posts
    55
    Reputation
    10
    Thanks
    7
    My Mood
    Yeehaw
    thanks, i almost gave up on trying to do it..... i really need to remember this ....
    "He who joyfully marches to music rank and file, has already earned my contempt. He has been given a large brain by mistake, since for him the spinal cord would surely suffice"-Eistein

Similar Threads

  1. [Tutorial] How to play Vindictus From outside NA (Vpn)
    By spoon850 in forum Vindictus Tutorials
    Replies: 54
    Last Post: 08-23-2011, 04:22 AM
  2. [Tutorial] Connecting to Vindictus from outside the US
    By Paladin in forum Vindictus Tutorials
    Replies: 9
    Last Post: 06-07-2011, 08:58 AM
  3. Avoid banning from your main account!
    By xhavefunx in forum Suggestions, Requests & General Help
    Replies: 3
    Last Post: 05-07-2010, 05:01 PM
  4. Replies: 3
    Last Post: 04-16-2010, 10:29 PM
  5. Learn how to code C++ || Day 4 - Input and Modifying Variables
    By P0SEID0N in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 0
    Last Post: 04-15-2010, 08:49 PM