cout question?

This is a discussion on cout question? within the C++ Programming forums, part of the General Programming Boards category; hello, I cants get my output to work so i can see the last 2 statements. what am i doing ...

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

    Question cout question?

    hello,
    I cants get my output to work so i can see the last 2 statements. what am i doing wrong??


    Code:
    #include <iostream>	//cin, cout, <<, >.
    #include <vector>	//for vector
    using namespace std;
    
    int main()
    {
    	vector<int> number,
    		v(10, 20),
    		w(10);
    	
    	int num;
    	
    	for (;;)	//forever loop
    	{
    		cin >> num;	//input numbers
    
    		if (num < 0) //if true break else contine to next statement
    			break;
    		number.push_back(num);	//append number at end
    		number.pop_back();	//pop_back eraes last number 
    		number.push_back(number.front());	//append number to 1ts element
    		
    		cout << number.pop_back <<'\n'; //output
    	}
    		
    	return 0;
    }
    Last edited by correlcj; 11-03-2002 at 11:55 AM.
    "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
    Why do you push the number on, pop it off, and then puc the front back on to the back?

    I am not able to understand what you are trying to accomplish here. Perhaps you could post what your current output is, and what your desired output would be.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Why do you push the number on, pop it off, and then puc the front back on to the back?
    I dont either, this book i have wants me to do that so i get used to these, i guess.

    Anyways...

    i input numbers: 99 33 44 88 22 11 55 66 77 -1
    it should read these and out put the numbers with the last element erased because of the pop_back then return a refenece to num's first element.

    like so 99 33 ... 55 66 then return 77 to the first element.

    I trued placing those last two staements into my cout but did not work. I understand how this is suppose to work but getting the program to accomplish it is a different task. I hope to figure this out??
    "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
    Well it might be a problem that you do the pop_back() and push_back(number.front()) every iteration.

    If you want to input the whole vector and then work with the complete vector afterwards, you should put the working with the vector code after the loop.

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

    OOOOOOOOOOOK!

    It works!
    Thanks Imperito!
    "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

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Really? wow. Unless you changed something else, it ought not to!

    One thing I notice now...

    You try to do this:

    Code:
    cout << number.pop_back <<'\n'; //output
    pop.back() is a void function. It pops the number off the back, but does NOT return the value thereof!!!!!

    A common mistake, nonetheless.

    A vector reference (Hosted by Vassar, of all places...) can be found here

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

    I changed that!

    pop.back() is a void function. It pops the number off the back, but does NOT return the value thereof!!!!!
    My compiler caught that and i changed it to output the number so i could see what it was doing. It really works now!
    Thanks for helping me with that also Imperito.
    "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. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 11:10 PM
  3. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 06:10 AM
  4. cout question
    By The Gweech in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2002, 05:11 PM
  5. cout question...
    By bishop_74 in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2001, 11:22 AM

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