Results 1 to 2 of 2

Thread: I have a question about classes

  1. #1
    NOMADMAN
    Guest

    Arrow I have a question about classes

    I've been working on this project and this problem for a long time. I got understand the istream and ostream of classes well enough, my book is ****, this question will probably be simple for a c++ vet.

    I have a class, customer. Its templated. Declare a varible of type customer(newCustomer). When I do cin >> newCustomer it goes through its routin about gathering info, like it should. In the istream function of the class I have it cout << some of the data the user just inputed (for a test). After the istream finishes it goes back to main, then I cout << newCustomer (another test). It prints out like it should except the user information is a bunch of null crap. Any idea whats going on? I just don't understand how c++ works.

    Heres the code:

    Important segment of main:
    Code:
    	auto customer<void>			newCustomer;
    
    	cin >> newCustomer;
    	cout << newCustomer;
    	custTree.insert(newCustomer);
    istream and ostream fnctions in customer class:
    Code:
    template <class person>
    ostream & operator << (ostream &o, const customer<person> c)
    {
    	o << c.ccNum << "\t"
    	<< c.fName << " "
    	<< c.lName << "\t"
    	<< c.status << "\t"
    	<< c.prevRentals << "\t"
    	<< c.cardDiscount << "\t"
    	<< c.prevRentDiscount << "\t"
    	<< c.fedTax << "\t"
    	<< c.netCost << "\t";
    
    	return o;
    }
    
    template <class person>
    istream & operator>> (istream i, customer<person> c)
    {
    	auto int tempStatus = 3;
    
    	cout << "Enter Credit Card #: ";
    	i >> c.ccNum;
    	cout << c.ccNum;
    
    	cout << "Enter first name: ";
    	i >> c.fName;
    	cout << "Last name: ";
    	i >> c.lName;
    
    	cout << "1. Platinum  2. Gold  3. Regular\n";
    	cout << "Card status: ";
    	i >> tempStatus;
    	switch (tempStatus)
    	{
    	case 1:
    		c.status = Platinum;
    		c.cardDiscPerc = 0.06;
    		break;
    	case 2:
    		c.status = Gold;
    		c.cardDiscPerc = 0.03;
    		break;
    	default:
    		c.status = Regular;
    		c.cardDiscPerc = 0.0;
    		break;
    	}
    	
    	cout << "# of Previous Car Rentals: ";
    	i >> c.prevRentals;
    
    	cout << c.ccNum;
    	
    	return i;
    }
    custTree is a tree data class. I'm sure it works as I got it from my teacher. The problem lies within myself.
    I will include a zip of my entire project if this for some reason isn't enough. Please keep in mind I'm new to C++.

    Thanks to anyone who attempts this question!

    NOMAD

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The zip would be good, I'd like to see where exactly it fails.
    One thing up front:
    const customer<person> c
    should be
    const customer<person> &c


    I've just realized that this is surely the problem you have. It's not that grave for the << operator, but it makes the >> operator useless (learn a bit about call-by-value and call-by-reference and you'll understand why)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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