This program keeps skipping over the cin>>m>>n statement. I can't figure out why. I've tried adding commands like cin.ignore() of cin.clear() before that cin statement, but nothing seems to work. If I put something like "cout<<"hi";" at the end of the program, that line of code will still print. Any help is appreciated. Thanks!
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>
usingnamespace std;
vector<int>swap(vector<int>list, int m, int n);
int main(){
int n,m,input;
vector<int>list;
vector<int>swaplist;
cout<<"Enter an integer list, ending with a non-integer: ";
while(cin>>input){
list.push_back(input);
}
for(int i=0;i<list.size();i++)
cout<<list[i]<<" ";
cout<<endl;
cout<<"Which entries would you like to switch? Separate by space: ";
cin>>m>>n;
swaplist=swap(list,n,m);
for(int i=0;i<swaplist.size();i++)
cout<<swaplist[i]<<" ";
}
vector<int>swap(vector<int>list, int m, int n){
vector<int>replace;
replace=list;
replace[m]=list[n];
replace[n]=list[m];
return replace;
}
After control leaves the body of the loop, std::cin is in a failed state. Reset it by dumping out everything still in the stream until a newline, and clearing the error state.
Thanks for the reply. I added both of those lines after that while loop, but when I run the program I get the same problem. Could something else be causing it?