Maybe I have a casting problem or something not sure. I enter 1 into my vector and then 2, 3 etc... then when I print I get 49, 50, 51. I use the iterator and a regular loopto test output. What did I do wrong? Thanks for your help! I'm looking forward to the day I won't need so much!!!
[CODE]
Code:====================================================== =====================LOOK FOR THESE FLAGS IN CODE ====================================================== #include <iostream> #include <vector> #include <string> using namespace std; // This takesa an address and returns one for updating the int vector int& addElement(vector<int>& myIntVec, int myInt); int main() { vector<int> myIntVec; vector<int>::const_iterator iter; //int intElement; char end; //ends do while loop char myInt; //user inputs an integer to be added to myIntVec do // loops as long as user wants to add integers to vector { cout << "Address of myIntVec: " << &myIntVec << endl; cout << "Size of myIntVec: " << myIntVec.size() << endl; cout << "Would you like to add an elment? y n\n"; cin >> end; if(end == 'y') { cout << "What integer would you like to add?\n"; cin >> myInt; // function in call to cout cout << "\n&(addElement(myIntVec,myInt)) " << &(addElement(myIntVec,myInt)) << endl; } //====================================================== //=====================HERE IS WHERE IT PRINTS THE WRONG THING //====================================================== for(iter = myIntVec.begin(); iter != myIntVec.end(); ++iter) cout << *iter << " at address: " << &(*iter) << " &(*iter)" << endl; }while(end == 'y' || end == 'Y'); //====================================================== //=============================ALSO PRINTS WRONG THIS WAY //====================================================== cout << "See if I have a casting problem\n"; for(int i = 0; i < myIntVec.size(); ++i) cout << myIntVec[i] << " at address: " << &myIntVec[i] << " &myIntVec[i]" << endl; cin >> end; //so screen doesn't get destroyed by Windows return 0; } int& addElement(vector<int>& myIntVec, int myInt) { int lastElement; cout << "In addElement function\n"; myIntVec.push_back(myInt); lastElement = myIntVec.size(); cout << "&myIntVec.end() " << &myIntVec.end() << endl; return myIntVec[ lastElement - 1 ]; } [\code]



LinkBack URL
About LinkBacks


