boblied 26 Newbie Poster

Hello
... It is not taking the inputs of name and address as it should. Got confused how to solve it.

After the operator>>() , the input is left pointing at the end-of-line character. At the next call of get() , there is already input available (the end-of-line character), so it reads up to the end-of-line delimiter, which is zero characters.

A solution is to discard the rest of the line after you read the values, using cin.ignore() -- check the manual for how to use it.

cout << "Enter customer id:";
cin >> x->id;
cin.ignore(999, '\n');

Also, instead of <iostream.h> , you should be using <iostream> . iostream.h is an old version that is only kept for backwards compatibility with very old code.

boblied 26 Newbie Poster

How about using cin.get() to get one character at a time, verifying that the order is digit, digit [convert to int and check range], colon, digit, digit [convert to int and check range], end-of-line. Maybe ignore leading or trailing white space to be nice. Not sure how you're going to indicate an error -- throw an exception maybe, or just exit() after an error message?

boblied 26 Newbie Poster

I very specifically have to have my Day class call this Appointment class, which is how it is now, but then wouldn't I need some sort of cout statement before I call the >> operator? In which case, wouldn't I then have cout statements in both my makeAppt() function and also in my overloaded operator? It seems messy to me. Or I'm missing something.

Glad it's been helpful; one more guess before I call it a day. You could delegate makeAppt to operator>> and leave the appointment prompting in operator>> .

void Appointment::makeAppt()
{
    cin >> *this;
}
boblied 26 Newbie Poster

I need to read in both sobject and location from this, but I'm really not sure how to go about it. With that code, calling the >> operator will always store it into appt.subject, but what about storing information to appt.location? Any suggestions on an approach?

Each time you use operator>>() , it will read one word. For the purposes of the exercise, you could assume that subject will be one word and location will be another word. If either one could be a line of multiple words, you will have to use the getline() function (see standard library documentation).

istream& operator>>(istream& is, Appointment& a)
{
    cout << "Enter subject and location:" << endl;
    is >> a.subject >> a.location;
    return is;
}

or

istream& operator>>(istream& is, Appointment& a)
{
    cout << "Enter subject, end with carriage return:" << endl;
    is.getline(a.subject, BiggestLikelyString);
    cout << "Enter location, end with carriage return:" << endl;
    is.getline(a.location, BiggestLikelyString);
}
boblied 26 Newbie Poster

You've just moved the uninitialization. ptr is a random number.

ptr, subject, and location are just 4 tiny little bytes that are the address of some other memory. If you leave them uninitialized or set to NULL, then they're pointing at nothing, or at least nothing useful. They have to become the address of something, or the program will blow up when trying to use them.

This is the basic problem with char*. You have to guess what the maximum size might be and allocate enough space. If you guess wrong, then hackers overflow your buffers and steal your bank account. If you don't guess at all, your program crashes.

To solve this problem, in the constructor of Appointment, allocate space for subject and location, and recover it in the destructor.

const size_t BiggestLikelyString = 2048;
Appointment::Appointment()
{
    subject = new char[BiggestLikelyString];
    location = new char[BiggestLikelyString];
    subject[0] = location[0] = '\0';
}
Appointment::~Appointment()
{
    delete[] subject;
    delete[] location;
}
boblied 26 Newbie Poster

appt.subject is a char*. What does it point at? Abandon char* and use std::string.

boblied 26 Newbie Poster

The most direct way, since you've already included the algorithm header, is to use std::sort().

boblied 26 Newbie Poster

At line 6, you declared showMedian to take a double parameter, but
you implemented it to take a double *.

At line 84, are you trying to call arrSelectSort again? If so, it doesn't need the types in its arguments.

At line 90, you are checking a pointer to see if it's odd or even. Probably you meant to check size, not array.

At line 92, it looks like you have become confused between the size parameter and the numValues variable used elsewhere.

In the case for even number of elements, remember that array will be indexed from 0 to size-1, not 1 to size.

Nick Evan commented: sounds like good advice to me! +22
boblied 26 Newbie Poster

Hello,

bool Set::operator==(const Set& b) const {
   if(b.header <= header && header <= header)
       //codes....
   return true;
}

Inside my overload for == which I use <= it doesn't use my overloaded version of <= instead it uses the standard version. Do I have to write a certain way to use my overloaded operator? or is it some other problems?

Inside your operator==, you're comparing header, not Set. You'd have to have an overload for whatever type b.header is.

boblied 26 Newbie Poster

Have you looked into the standard library? There's a std::unique and std::unique_copy function.

std::unique requires that the duplicate elements are next to each other in the vector. The vector would have to be sorted and you end up back at defining a predicate for ordering the elements. It might be more expressive of the intent, though.

boblied 26 Newbie Poster

You declared alpha and beta as int instead of an array in the function definition.

boblied 26 Newbie Poster

For the purpose of sorting, any complete ordering will do. You could define a sort that uses x as primary key, y as secondary, and z as tiebreaker.

// Comparator function object to define less-than for Points
struct PointLess
{
    bool operator()(const Point& p1, const Point& p2) const
    {
        if ( p1.x < p2.x )
            return true;
        else if ( p2.x < p1.x )
            return false;
        // assert(p1.x == p2.x )
        if ( p1.y < p2.y )
            return true;
        else if ( p2.y < p1.y )
            return false;
        // assert(p1.x == p2.x && p1.y == p2.y )
        if ( p1.z < p2.z )
            return true;
        else
            return false;
    }
};

std::set<Point, PointLess> sp( pointVec.begin(), pointVec.end() );

std::vector<Point> noDupVec( sp.begin(), sp.end());
daviddoria commented: Good idea. +2
boblied 26 Newbie Poster

You need a blocking call that waits for input. Look up the select() system call.