Results 1 to 9 of 9

Thread: [Resolved] Classy Vector Help

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Classy Vector Help

    I'm trying to run this code (I slimmed it down as much as I could):

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class trans {
        private:
            vector<double> amount;
            vector<char*> notes;
    
            double total;
    
        public:
    
            //constructor:
            trans() {
                total = 0;
            }
    
            //destructor:
            ~trans() { }
    
            //add a transaction:
            void add(double a, char note[]) {
                amount.push_back(a);
                notes.push_back(note);
    
                total += a;
            }
    
            //get number of transactions:
            int getnumtrans() {
                return amount.size();
            }
    
            //get transaction notes:
            char *getnote(int index) {
                return notes[index];
            }
    };
    
    int main() {
        trans deposits;
    
        char con = '\0', comment[255];
        double amount;
    
    
        do {
            cout << "Amount: ";
            cin >> amount;
    
            cout << "Comment: ";
            cin >> ws;
            cin.getline(comment, 255, '\n');
    
            deposits.add(amount, comment);
    
            cout << "Add another? (y) or (n): ";
            cin >> con;
    
        } while (con != 'N' && con != 'n');
    
        for (int i = 0; i < deposits.getnumtrans(); i++) {
            cout << deposits.getnote(i) << endl;
        }
    
    	return 0;
    }
    For a bank account program. Insert deposit amount and deposit comment, continue, etc.

    I do this a few times, entering unique comments such as 'Birthday Money', 'Paycheck', and 'None'

    However, when it runs the loop to repeat all the comments, it just repeats 'None' three times.

    Where am I going wrong with this?
    Last edited by The Hobo; Nov 12th, 2002 at 04:40 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Simple. You just push_back ed the address of comments 3 times. Whatever comments is holding at the time you print stuff is going to print out.

    The simplest solution is, of course, to use the string class.

    Z.

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    How come the same doesn't happen with other data types?

    I'm not sure if we're allowed to use the string class. I thought of that as a solution, but at the beginning of the semester, my anal teacher said she didn't want us using it (don't ask why, she's that way with a lot of things).

    I'll ask her if that stance has changed. We're learning OOP programming stuff, so we should be able to use something like the string class. I'll give her hell about it tomorrow.

    Thanks, Z.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Another question, then. Why does this work?:

    Code:
    int main() {
        trans deposits;
    
        char con = '\0';
        char comment[255];
        double amount;
    
    
        deposits.add(45.4, "Test1");
        deposits.add(44.3, "Test2");
    
        for (int i = 0; i < deposits.getnumtrans(); i++) {
            cout << deposits.getnote(i) << endl;
        }
    
    	return 0;
    }
    Does it have something to do with text being passed, rather than a variable?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    "Text1" and "Text2" are stored at different locations, so you push two separate memory addresses.

    The reason it works with other data types is because a C string is simply a chunk of memory, described by a memory address. You throw around that address, not the actual string itself.

    Z.

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I see.

    Yet Another Question:

    Code:
    cout << "Comment: ";
    cin.ignore();
    getline(cin, comment);
    
    deposits.add(amount, comment);
    Why do I have to push enter twice after entering the comment?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    You shouldn't store char*s, use strings instead:
    Code:
    vector<string> notes;
    A string will make a separate copy of the string, and store the copy. If the original string changes, the copy stays the same.

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by twanvl
    You shouldn't store char*s, use strings instead:
    Code:
    vector<string> notes;
    A string will make a separate copy of the string, and store the copy. If the original string changes, the copy stays the same.
    Read the rest of the thread. It's already been discussed.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by The Hobo
    I see.

    Yet Another Question:

    Code:
    cout << "Comment: ";
    cin.ignore();
    getline(cin, comment);
    
    deposits.add(amount, comment);
    Why do I have to push enter twice after entering the comment?
    Nevermind. I found a fix here: http://support.microsoft.com/default...;en-us;240015&
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width