contents of vector <T>

This is a discussion on contents of vector <T> within the C++ Programming forums, part of the General Programming Boards category; Greetings, If you input these values 99 33 44 88 22 11 55 66 77 -1 what should you get? ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question contents of vector <T>

    Greetings,

    If you input these values 99 33 44 88 22 11 55 66 77 -1 what should you get?
    From what I read about push_back is that it should push back the very last number right? Thats not what I get though, can someone please explain why? Am i missing something in my code??

    Code:
    #include <iostream>	//cin, cout, <<, >.
    #include <vector>	//for vector
    using namespace std;
    
    #define nl '/n'
    
    vector<int> number,
    		 v(10, 20),
    		 w(10);
    
    int num;
    
    int main()
    {
    	for (;;)	//loops forever
    	{
    		cin >> num;	//input number 
    		if (num < 0) break;	//if number is less than 0 break
    		number.push_back(num);	//push_back a number
    		cout << num << nl;	//output the number with the push_back
    	}
    	return 0;
    }

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    A few things right away..

    Code:
    #define nl '\n'
    This is probably a bad idea, it really isn't that much to type '\n', and with such a short string for the substituition you risk breaking things.

    Code:
    for (;;) //loops forever
    {
    //Stuff
    if (num < 0) break;//if number is less than 0 break
    // Stuff
    }
    This is another bad idea... Why not use a while construct to accomplish this? Exiting from the middle of a loop is not only inelegant, but can confuse readers of your code, and is technically not consistant with the OO paradigm.

    Code:
    vector<int> number,
     v(10, 20),
     w(10);
    
    int num;
    These almost certainly do not need to be global variables. You should not allow yourself to get into the habit of making variables global in this type of situation. It will pay off greatly later on.

    Also, it should output the number you inputted, after pushing it onto the vector, no?

    What is it that you are trying to do, exactly?

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    What is it that you are trying to do, exactly?
    After changing the suggestions you made, my problem finally matches that of the book. Thanks again Imperito!

    I was trying to get this to output those numbers but because the book declared then globablly that as you said caused problems. Thanks for the help!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Your book used globals in a context like that?

    Which book?

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Talking The Book!

    C++, an intro to computing by joel adams, sandford leestma and larry nyhoff.

    Thanks again!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Swap the file contents
    By jayfriend in forum C Programming
    Replies: 5
    Last Post: 01-16-2007, 10:36 PM
  3. controlling contents of a linked list
    By jaro in forum C Programming
    Replies: 7
    Last Post: 05-04-2006, 01:31 AM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 03:34 PM
  5. combining file contents with command line
    By pxleyes in forum C Programming
    Replies: 4
    Last Post: 04-12-2004, 11:27 PM

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