|
-
Nov 12th, 2002, 02:28 PM
#1
Thread Starter
Stuck in the 80s
[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.
-
Nov 12th, 2002, 02:41 PM
#2
Frenzied Member
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.
-
Nov 12th, 2002, 02:54 PM
#3
Thread Starter
Stuck in the 80s
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.
-
Nov 12th, 2002, 02:56 PM
#4
Thread Starter
Stuck in the 80s
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?
-
Nov 12th, 2002, 03:04 PM
#5
Frenzied Member
"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.
-
Nov 12th, 2002, 03:10 PM
#6
Thread Starter
Stuck in the 80s
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?
-
Nov 12th, 2002, 03:12 PM
#7
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.
-
Nov 12th, 2002, 03:14 PM
#8
Thread Starter
Stuck in the 80s
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.
-
Nov 12th, 2002, 04:39 PM
#9
Thread Starter
Stuck in the 80s
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&
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|