Thread: variables

  1. #1
    ___
    Join Date
    Jun 2003
    Posts
    806

    variables

    OK I'm at a point in my book that doesn't explain storage duration for variables. Correct me if I am wrong but this is what I think it means.

    First off there is automatic variables. They are within scope and are only for the duration of that scope. (i.e curly brackets). They can be changed in a different scope and hold that value in that scope only and return when it closes.

    Then there is global scope which I'm thinking is for the entire program. There are 2 different storage durations in that which are static and dynamic.

    That is what I am getting. So can anyone explain in further detail about global scopes and their duration for variables. And also is what I get from that correct?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Global variables are considered bad practice. I don't think that declaring a global variable static would have any effect. You can read or write to it from anywhere, and it will retain the last value assigned to it.

    Declaring a variable static doesn't change it's scope (as far as I know). But, it will retain it's value when you return to the function where the variable was declared. So, you could use a static variable to count how many times a function was execuited.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Static limits the scope of the variable to the file it is declared in. Consider global space as a wrapper for the entire program. All global variables, provided you use the extern keyword, and don't use static, are available in all files. Otherwise, if you use static, they're only able to be accessed in the function they're declared in.

    Tihs is like static functions. Static functions are limited to the file they are declared in. They are not accessable elsewhere.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    ___
    Join Date
    Jun 2003
    Posts
    806
    Alright thanks a bunch. Oh one more thing when you get input from the keyboard like a multiple charactors is this what you do

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       char name1[13] = " ";
    
      cout << " insert char ";
      cin.getline(name1, 13);
     
      return 0;
    }
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Yeah, that'll work. For input from stdin(cin), I prefer to use string objects because I find them a little easier to work with than char arrays.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    	//The string can be created just like an int, char, etc.
    	string firstname, fullname;
    
    	cout <<"What's your first name?" <<endl;
    	
    	//You can use the cin >>operator to input to the string
    	//However, the string will be delimited by whitespace
    	//e.g. If you entered "John Smith", the string "John" would be stored
    	
    	cin >>firstname;
    
    	//The string can be printed with the cout << operator
    	cout <<"What's your full name, " <<firstname <<"?" <<endl;
    
    	//To input a whole line into the string, use getline() (not cin.getline())
    	getline(cin, fullname);
    
    	//The first argument is the stream that you want to input from(cin), and the second
    	//is the string to input to
    
    	cout <<"Well, " <<fullname <<", it's been fun but I gotta go." <<endl;
    }
    For more info on string objects, refer to your book(if it's any good), or search for "c++ string objects" with google.
    Last edited by PorkyChop; 07-10-2003 at 07:55 PM.

  6. #6
    ___
    Join Date
    Jun 2003
    Posts
    806
    To input a whole line into the string, use getline()
    So I would do this string right here...

    Code:
    getline(cin, xxx);
    with xxx being substituted by the name of the variable?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Yep

  8. #8
    ___
    Join Date
    Jun 2003
    Posts
    806
    Alright thanks. I learned allot today
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Uh oh. I just ran that program I created because I thought it would brighten my day a little, and I realized that I forgot something important.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    	string firstname, fullname;
    
    	cout <<"What's your first name?" <<endl;
    	
    	cin >>firstname; 
    
    	cout <<"What's your full name, " <<firstname <<"?" <<endl;
    
    	//********THIS IS WHAT I FORGOT**********
    	/*When you use the cin >> operator to input, you press enter to...enter the input
    	  That newline character is left over in the buffer, and unless you tell cin to 
    	  ignore it, getline() will see it and will terminate the input before you get the
    	  chance to type it in. */
    
    	cin.ignore(256, '\n');
    	
    	//The first argument tells ignore how many characters deep in the buffer to look for the
    	//specified character. The second argument is the character to ignore(in this case, newline)
    
    	getline(cin, fullname);
    
    	cout <<"Well, " <<fullname <<", it's been fun but I gotta go." <<endl;
    }

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, 11:07 PM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM