vector push_back and erase
i m supposed to write a program where user can enter and data from a vector file. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec;
bool quit=false;
while(!quit)
{
cout<<"vec.size():"<<vec.size()<<endl;
cout<<"vec contains:";
for(int i=0;i,vec.size();i++)
cout<<vec[i]<<"";
cout<<endl;
cout<<"1.add,2.remove,3.exit ";
int input=0;
cin>>input;
switch(input)
{
case 1:
cout<<"enter digit: ";
cin>>input;
vec.push_back(input);
break;
case 2:
cout<<"enter digit index to remove: ";
cin>>input;
if(input>0&&input<vec.size())
vec.erase(vec.begin()+input);
break;
case 3:
quit=true;
break;
}
}
}
|
problem is as soon as i try to enter a digit the program stops.
Your problem is the reuse of the variable input. use a different one for your number in the switch.
No. The problem is on line 12. i,vec.size()
should be i < vec.size()
.
MyClangers++
thanks peter87. My bad i did'nt noticed it.
Topic archived. No new replies allowed.