Static vs. Automatic Variables

This is a discussion on Static vs. Automatic Variables within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to get the second call to SumNumbers a value other than 0. I think that the second ...

  1. #1
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20

    Static vs. Automatic Variables

    Hi,

    I'm trying to get the second call to SumNumbers a value other than 0. I think that the second call to SumNumber is 0 just like the first call because I have "static int cnt = 0;". I don't know how to change my program to get the second call to say 1. Here is my code:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int sum;
    void SumNumbers();
    ifstream NumData;
    void main()
    {//block 1
    	SumNumbers();
    	cout << "Call 1: the sum of the numbers is " << sum << endl;
    	SumNumbers();
    	cout << "Call 2: the sum of the numbers is " << sum << endl;
    }
    void SumNumbers()
    {//block 2
    	NumData.open("numbers.dat");
    	sum = 0;
    	int num;
    	static int cnt = 0;
    	NumData >> num;
    	while (cnt <=4)
    	{//block 3
    		sum = sum + num;
    		cnt++;
    		
    	}
    }
    Thanks!

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I have absolutely no idea what you want to do. The second time the function runs cnt will be equal to 5, and the loop will never execute. (Maybe you should read up on the static keyword)

    We're going to need some more detail on what exactly you are trying to do.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    void how_many_times_has_this_function_been_called(void) {
        static int calls = 0;
    
        calls ++;
    
        return calls;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    int sum;  
    void SumNumbers();
    ifstream NumData;
    
    void main()
    1)Don't ever declare a variable outside of a function. Either declare it in main() or another function.
    2)Here is the program template for all beginning programmers:
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    
    
    
    	return 0;
    }
    All of your programs should contain that code. Fill in any other code you need.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    just FYI, the 'return 0;' at the end of main() is implicit.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by miken
    just FYI, the 'return 0;' at the end of main() is implicit.
    It's still good form to write it lest you try to do the same in any other function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 03:16 AM
  3. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 07:35 AM
  4. Order of initialization of static variables
    By Sang-drax in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2004, 05:31 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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