Click here to Skip to main content
15,881,826 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int>S;
    cout<<S.size();
    S.push_back(23);
    S.push_back(45);
    S.push_back(-99);
    S[4]=99;
    cout<<'|'<<S.at(0)<<'|'<<S.at(1)<<'|'<<S.at(2)<<'|'<<S.at(3)<<'|'<<S.at(4)<<'|';
    cout<<endl<<S.size();
    return 0;
}


What I have tried:

i compiled and ran it,but it didn't show anything.Please,help me.what is wrong with my program?
Posted
Updated 24-Aug-16 14:33pm
v3

Your program is undefined because you access non-existing elements.

In particular, the following line has undefined behavior because item at index 4 does not yet exist.
C++
S[4]=99;

Thus, you should read the documentation:
vector::operator[] - C++ Reference[^]
vector::operator[^]

In fact, just before that line get executed, you have 3 items at index 0, 1 and 2.

Given that, next line is also incorrect because you try to display items at index 3 and 4. Thus you should call push_back twice to add those items.
C++
S.push_back(0);   // Whatever you want at index 3...
S.push_back(99);  // Instead of S[4] = 99;

By the way, I would recommand you to add some white space (and indentation) for readability. Here are a few example from your code that are more readable with some extra spaces:
C++
vector<int> S;
//...
S[4] = 99;
cout << '|' << S.at(0) << '|' << S.at(1) << '|' << S.at(2) << '|' << S.at(3) << '|' << S.at(4) << '|';
cout << endl << S.size();
return 0;
 
Share this answer
 
v2
Comments
Member 12702056 24-Aug-16 20:38pm    
Thank you very much :) Very clear explanation :)
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

[Update]
Use indentation in your code, it helps reading when code become complicated.
 
Share this answer
 
v2
Comments
Member 12702056 24-Aug-16 20:37pm    
Thanks a lot :) I'll try :). But i want to know that will debugger help to me about finding logical problems?
Patrice T 24-Aug-16 21:02pm    
The debugger allow you to run your program line by line, and as it allow you to inspect variables after each line, you will see where things go wrong.
Member 12702056 25-Aug-16 3:58am    
Thanks :) Finally I got what debugger is :) I also read about it :) Thanks again :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900