problem with classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sarahger9
    New Member
    • Oct 2007
    • 14

    problem with classes

    I am working on a project that inputs stock information from files, then outputs the information when the customer is called upon. I am doing pretty good, but am stuck. This is what I have.

    Code:
    	for (int k = 0; k < namevector.size(); k++){     //namevector is a vector of type 
    		otherfile.close();                                             customer(defined as a class)
    		otherfile.clear();
    		otherfile.open(namevector[k].getname().c_str());
    		if (!otherfile.is_open()){
    				cout << "Can't open the customers stock file" << endl;
    		}
    		while(!otherfile.eof()){
    		otherfile >> symbol >> numshare >> price;
    		stocks.setsymbol(symbol);
    		stocks.setnumshare(numshare);
    		stocks.setprice(price);
    		stockvector.push_back(stocks);           //stockvector is vector of type stock 
                                                                              which is defined as a class
    		}
    	}
    		cout << "Enter customer name, or exit" << endl;
    		cin >> command;
    		for (int n = 0; n < 3; n++){
    	if (command == namevector[n].getname()){
    		cout << "found person" << endl;
    		for(int m = 0; m < 3; m++){
    			cout << namevector[n].getstock(m).getnumshare()<<
    				namevector[n].getstock(m).getsymbol()<<
    				namevector[n].getstock(m).getprice() <<
    				namevector[n].getstock(m).getnumshare() * namevector[n].getstock(m).getprice() << endl;
    		}
    The problem (I believe) is that when I read the data in from the files (there is a seperate file for each customer) it puts all of it into the same stockvector, so that when I try to call for a specific customer, it doesn't match up with the stock for that customer. Can I create more than one stockvector, or is that impractical?
    Please let me know if you need any more code, like my class functions, but I don't believe they are the problem, although I could be wrong.
  • AHMEDYO
    New Member
    • Nov 2007
    • 112

    #2
    HI..

    i will tell you about somethings that you can care about because i cant exactly understand all things from your code.

    the command that the user will enter to search about customer is string?, and getname() function is string too? i mean it will return pointer to string?

    if so you cant compare it like that, because you will compare pointers value not the string

    [CODE=cpp] if (command == namevector[n].getname())[/CODE]

    if this is the problem you can use strcmp(str,str) to compare two strings, it will return 0 if two strings is same

    i hope this will help you!

    Best Regards

    Comment

    Working...