why do i have to enter a extra character
Mar 4, 2013 at 12:43am UTC
vector/vector square reversing program
every time i input something i have to enter a extra character after the required amount. run it and see for yourself
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#include <iostream>
#include <vector>
using namespace std;
void fillVector(vector<int >&);
void reverse(const vector<int >&);
int main(){
vector<int >myVector;
fillVector(myVector);
reverse(myVector);
return 0;
}
void fillVector(vector<int >& newMyVector){
int amount;
cout<<"Enter amount: " ;
cin>>amount;
cout<<"Type in a list of nums: " ;
int input;
cin>>input;
for (int j=0; j<amount; j++){
if (input!=j){
newMyVector.push_back(input);
cin>>input;
}
cout<<endl;
}
}
void reverse(const vector<int >& newMyVector){
for (unsigned int i=newMyVector.size() - 1; i>0; i--){
cout<< newMyVector[i]*newMyVector[i]<< " " ;
}
for (unsigned int i=0; i<newMyVector.size(); i--){
cout<< newMyVector[i]*newMyVector[i]<< " " ;
}
for (unsigned int i=newMyVector.size() - 1; i>0; i--){
cout<< newMyVector[i]<< " " ;
}
for (unsigned int i=0; i<newMyVector.size(); i--){
cout<< newMyVector[i]<<" " ;
}
}
Mar 4, 2013 at 2:36am UTC
Because you're reading input 11 times.
You're inputting a number at line 39.
I don't understand what the if statement at line 46 is supposed to accomplish. You're checking if the number entered doesn't match the loop index ???
At line 48, you're prompting for input for each of the 10 times through the for loop
(assuming input never matches the loop variable) for a total of 11 inputs. The last input is never pushed on the vector.
Topic archived. No new replies allowed.