Thread: 2 beginner questions

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    2 beginner questions

    No 1:

    I noticed in a previous post that there was a debate about the use of cout and std::cout. I feel like I partially understand this and sense that it has something to do with <iostream.h> vs <iostream> and 'including namespace std' and the ANSI C++ standards.

    Does this make sense and is there any chance of a brief explanation of this.... becuase in the textbooks I'm using, they all seem to refer to cout rather than std::cout.



    No 2:

    I'm trying to write a program to do simple statistics (mean, standard deviation). At the moment I'm using arrays to store the data set. I've not had a problem as long as I am able to say how many numbers there are in the data set, e.g

    Code:
    int n;
    cout << "How many numbers in the data set? ";
    cin >> n;
    
    double dataSet[n]; 
     //But what to do if I don't know what 'n' is?
    
    for (int counter = 0; counter !=n; counter++)
    {
        cout << "\nNumber " << (counter +1) << ": ";
        cin >> dataSet[counter];
    }
    
    etc
    But what should I do if I don't know how many should be in the data set (i.e. what happens when I don't know what 'n' is before I input the data? I'm sure this isn't a new question. Is using <fstream> the solution?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    1) <iostream.h> is deprecated. <iostream> is standard. All of the new C++ Standard Library headers define their symbols in the std namespace, so to access them, you need to either a) import all symbols in the namespace (in the headers you've included) using 'using namespace std;' toward the top of your file (or at least in the scope where you use the symbols), b) use a 'using declaration' for each symbol (e.g. 'using std::cout; using std::endl; using std::cin;') before using the symbols, or c) prefix each one with 'std::' (e.g. 'std::cout << "Hello" << std::endl;'). Search the board, and you will see this has come up often (and there are some better explanations around).

    2) You either need to supply the array with a constant value, use dynamic memory allocation (new[] and delete[]), or preferably, use std::vector.

    Here is a short reference on vector: http://cppreference.com/cppvector.html

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1) <iostream.h> is deprecated.
    No, iostream.h is nonexistant. It was never a part of the standard, so how could it be deprecated?
    My best code is written with the delete key.

  4. #4
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    It was part of ATT C++ 2.0 which was taken as the basis of ANSI C++ 3.
    At the time ATT C++ was the standard. ANSI decided to rename the headers when they introduced namespaces to avoid problems.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Okay.. Fair enough... I've never really used pre-C++98.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >At the time ATT C++ was the standard.
    De facto standard. And this doesn't change the fact that the .h headers were never a part of Standard C++.
    My best code is written with the delete key.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Net result is, don't use the .h.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    yeah dont use .h
    Just because is old, it was used in C
    and C++ is the super set,
    anyway, you can use it, if you want.
    Sorry, I would like to give you a better explanation , you deserve a better explanation,
    but if you ask me use the std. is the better
    and the better also is using std:: in every statment you use than the using namespace std;
    look better and keep you away from possible troubles, just in case u are making a big proyect.
    Hope that helps.

    ****************** abyss********************
    Last edited by abyssphobia; 11-22-2004 at 01:41 PM.
    Have I crossed the line?

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Thankyou very much all for clearing that up.

    And thanks Zach L. for your thoughts on the array question. I've only just touched on vectors. Lots to learn!

  10. #10
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by Prelude
    >At the time ATT C++ was the standard.
    De facto standard. And this doesn't change the fact that the .h headers were never a part of Standard C++.
    Depends on how you define standard. While there was no official standards body involved everyone who wanted a piece of the market followed the ATT standards.

    In our industry that's all the standard you need

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Question 1:
    I will break this into two parts.

    Part 1: Don't use <iostream.h>. Therefore you must either use "using namespace std;" or use std::cout.

    Part 2:
    Why Not To Use "using namespace std;"

    I find that particular thread to be an excellent resource, and think it should be stickied or added to FAQ (unless it will spark too much controversy).


    Question 2:
    You can keep a count of how many elements you input, you can use a std::vector so that it will keep track for you, you can use a terminating value to determine where the 'end' of the array is (i.e. use -1 or something to signal that there are no more elements past this point) - not recommended - or you can keep going until you crash from array index out of bounds
    Last edited by Hunter2; 11-23-2004 at 11:59 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Depends on how you define standard.
    >While there was no official standards body involved everyone who wanted a piece of the market followed the ATT standards.
    >In our industry that's all the standard you need
    And your point is? You basically just agreed with me on all points, but with more words.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    ... just an addendum to the array question. I was talking about arrays of 'uncertain size' earlier today and my friend came up with a few alternative solutions. I had a go earlier and ended up with an infinitely looping program! (oh this learning curve is dreadfully steep!) Anyway here is the gist of the solution, as best as I can remember it. I'd best not post the code I came up with incase someone dies of terminal laughter:


    - declare array of size N
    - set up a for loop which stops when the array is full
    - if the array is full (and there is no sentinel value to signal the end of the data...
    - declare another array of size 2xN
    - transfer the contents of the initial array to the bigger array
    - continue with filling up the bigger array
    - repeat...
    - until the sentinel value is reached, at which point you should have a single array of the complete data set


    Like I said, I gave it a try, but its early days for me and I''m still tyring to get a grasp of pointers, heaps and stacks.

    My friend also added that I'll probably not use a lot of this stuff later on and that memos were worth a look.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Really, I think using std::vector or std::deque would be a better solution - they'll do the memory resizing and copying for you. Or if you don't need to access individual elements, you can use a std::list so that no copying needs to be done at all.

    I see nothing wrong with the theory you have though, assuming an appropriate starting N is chosen - since the size of the array increases exponentially, you probably won't have to do too much copying unless you're inputting astronomical amounts of data. On the downside though, if you get a worst-case scenario you might have a lot of wasted memory.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    That particular algorithm is fairly common for allocating memory for arrays. At any rate, your best bet is probably going to be a vector. Here is a small piece of example code that I wrote up to demonstrate its use:
    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
       std::vector<int> vec;
    
       std::cout << "Number of elements: ";
       std::flush(std::cout);
       
       // There should be some input checks, but that is not the point of this
       // example, so I'll leave it out. Assume the user is smart enough to enter
       // a positive integer.
       int n;
       std::cin >> n;
       
       for(int i = 0; i != n; ++i)
       {
          // Add some value to the end of the vector.
          // In this case, the value 17i mod 9 is pushed onto the back.
          vec.push_back((17 * i) % 9);
       }
    
       // And now, for accessing.
       std::cout << "Accessing via iterator:" << std::endl;
       std::vector<int>::const_iterator it;
       for(it = vec.begin(); it != vec.end(); ++it)
       {
          std::cout.width(2);
          std::cout << *it; // "Dereference" the iterator. (Get the value stored
                            // in the indicated poisition.)
       }
       std::cout << std::endl;
       
       std::cout << "Accessing via index:" << std::endl;
       std::vector<int>::size_type i;
       for(i = 0; i != vec.size(); ++i)
       {
          std::cout.width(2);
          std::cout << vec[i];
       }
       std::cout << std::endl;
    
       std::cout << "Accessing via at():" << std::endl;
       std::vector<int>::size_type j;
       for(j = 0; j != vec.size(); ++j)
       {
          std::cout.width(2);
          std::cout << vec.at(j); // Like indexing, but throws an exception if you
                                  // overrun the bounds of the vector.
       }
       std::cout << std::endl;
       
       return 0;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some beginner questions.
    By Meikj in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2009, 11:37 AM
  2. Total beginner questions
    By Sparky in forum C Programming
    Replies: 5
    Last Post: 06-22-2008, 04:50 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. beginner questions.
    By Jaken Veina in forum C Programming
    Replies: 5
    Last Post: 03-16-2005, 09:38 PM
  5. several beginner questions...
    By Taikon in forum C Programming
    Replies: 2
    Last Post: 02-09-2005, 09:53 AM