int vector outputs wrong numbers

This is a discussion on int vector outputs wrong numbers within the C++ Programming forums, part of the General Programming Boards category; Maybe I have a casting problem or something not sure. I enter 1 into my vector and then 2, 3 ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    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!!!
    [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]

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    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, 06:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21