-2

I can't seem to get this code to run and I need to know the output. Can some one please help me?

vector<int> myinventory(4);
myinventory[0] = 2;
for (int i = 1; i <= 3; i++)
{
    myinventory[i] = 2*myinventory[i -1];
}
myinventory.push_back(34);
for (int i = 0; i < myinventory.size(); i++)
{
    cout << myinventory[i]<< " ";
}

Please, if you can provide the output that would be great.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
wayne
  • 1
  • 5
    Write it, compile it, run it. Period. – Kiril Kirov May 10 '11 at 20:16
  • 1
    If that's the entirety of your code, you're missing some `#include` statements, and a `main` function. – wkl May 10 '11 at 20:17
  • 3
    Do I look like a compiler to you? – CRM May 10 '11 at 20:17
  • do you seriously want the question to the answer? – Kunal Vyas May 10 '11 at 20:18
  • this is the main body of the program ... – wayne May 10 '11 at 20:18
  • ofcourse i have #include – wayne May 10 '11 at 20:19
  • @Wayne: So what you posted is not your whole code? You might want to look at this: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Bart May 10 '11 at 20:19
  • using namespace std; int main() ect.... but it wont compile – wayne May 10 '11 at 20:19
  • 1
    The next time you ask a question like this, at least post the errors you're getting. – Bart May 10 '11 at 20:21
  • 1
    @wayne - Bart is right - you had to post the whole code and provide us the errors. We need to see, that you've tried something..I guess you've missed `#include `, btw. – Kiril Kirov May 10 '11 at 20:25
  • 1
    @wayne - Welcome to Stackoverflow. You need to post the error message too along with small compilable code that reproduce this error. With that said, have you included `vector` header. `#include `. – Mahesh May 10 '11 at 20:26
  • 1
    If it doesn't compile, there is no output. If your compiler is broken, [try this](http://ideone.com/) – Benjamin Lindley May 10 '11 at 20:28
  • 5
    @Wayne: If you walked to a car repair shop and said "my car is broken" and they said "ok, let's take a look" and you said "here are the keys, I didn't bring the car", how do you think they'd look at you? You need to show us your entire program, how you're compiling it, what you expected, and what you got instead. All of it. – GManNickG May 10 '11 at 20:30
  • @GMan That was a good metaphor. – CRM May 10 '11 at 20:33
  • @wayne - Apply what GMan suggested to your question. I will vote to reopen it :) – Mahesh May 10 '11 at 20:34
  • @wayne And I'll be glad to help you :) – CRM May 10 '11 at 20:35

2 Answers2

1

Just walk through it on pen and paper. Or as already said; write it, compile and run.

ahodder
  • 11,353
  • 14
  • 71
  • 114
1

Try cout << myinventory[i]<< " " << std::endl;

to flush the console output. Hard to tell if this will work since "it won't run" is not hugely informative. Have you tried stepping through itin the debugger?

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140