Thread: int vector outputs wrong numbers

  1. #1
    Registered User
    Join Date
    Mar 2008

    int vector outputs wrong numbers

    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!!!
    =====================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";
         lastElement = myIntVec.size();
         cout << "&myIntVec.end() " << &myIntVec.end() << endl;
         return myIntVec[ lastElement - 1 ];

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    The char '0' has the value of 48, '1' has the value of 49, and so on in ASCII. Instead of declaring myInt as a char, declare it as an int.

    Also, it would be good to declare near first use, so move the declaration of iter from the top to the for loop where it is used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Ahhh, stupid mistake! This is what I get for programming late at night. I didn't even see it though. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM