Thread: Initializing an array? what is goin on

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by mburt
    Code:
    #include <iostream>
    using namespace std;
    int main() {
    int number[] = {1,1,1,1,0,0,0,0};
    int count = 0;
    for (int i=0;i<=8;i++) {
    if (number[i]==1) {count++;};
    };
    cout << count;
    system("PAUSE");
    }
    I'm not quite sure of what that code is supposed to be doing or why it has anything to do with strings or character arrays. I could tell you that you're indentation is bad and you're making unsafe calls to system(). In any event, there is nothing there that couldn't be done with a string.
    Sent from my iPadŽ

  2. #17
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Also, being neww to C++, I see std:: a lot and really havent found an explaination. Would anyone mind explainging this?

  3. #18
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by lilrayray
    Hmm strings seam nice. I almost feel like i am betraying C.
    Well you're writing C++ code... so if you feel like you're betraying C you might as well feel like you're betraying Java because you aren't using that, either.

    Quote Originally Posted by lilrayray
    Also, being neww to C++, I see std:: a lot and really havent found an explaination. Would anyone mind explainging this?
    It's a namespace. All standard libraries are in the std namespace, there for unless you declare that you're using it at the top of your code then you have to qualify all standard identifiers with the namespace. If you don't understand this yet, don't worry, just keep learning.
    Sent from my iPadŽ

  4. #19
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Oh ok I see, Im guessing this can be used as a meathod to use more than onee namespace?

  5. #20
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    oops. My fault

  6. #21
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    You don't have to do it like SlyMaelStrom said. You can define it first:

    #include<string>
    using namespace std;

    as I said before

    than just use this as a type of variable:

    string mystring = "test";

  7. #22
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by mburt
    #include<string>
    using namespace std;

    as I said before

    than just use this as a type of variable:

    string mystring = "test";
    We try and avoid this as it's a bad habit to get in to, even though in his case it's likely fine. Just avoid using the "using" directive inside of headers.
    Sent from my iPadŽ

  8. #23
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    So should I not use "using namespaces"?

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    For beginners and small programs it really doesn't matter much. If you want to get into a good habit then use std:: everywhere, but if you decide to use the using namespace directive it is totally fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM