boblied 26 Newbie Poster

... Note that "time" is highlighted. It's a C++ keyword.

Sorry for the tangent, but "time" is not a C++ keyword. The fact that it's highlighted is maybe because the syntax highlighter handles multiple languages, and "time" is a keyword in some of them (for instance, in the bash shell); or maybe because there is a Unix/POSIX/C standard function called time() from time.h . But "time" is not a C or C++ keyword, and the global function time() wouldn't conflict with a member variable called "time."

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

can somebody help me with this program??
[...] find a path moving like a knight in chess game(L) that touch every point of the matrix once. ...

Head start: from the current position, make a list of all the possible next moves that go to a position that hasn't been visited yet. Try the first one. If that doesn't complete the matrix, make a list of all the new possible moves. Try the first one. Repeat. If you ever find yourself with no more moves and the matrix still not covered, back out the most recent move and try the next one on the list. If you run out of moves on the list, back out to the previous list of moves. Stacks, lists, and recursion are likely to be involved.

boblied 26 Newbie Poster

Ahh, thanks but I'm still really confused :(
If I had a teacher and a classroom what could I use as the association because there needs to be 1 :(

Any way in which the two are related can be an association. Which one applies depends on what the problem is, and whether we're starting from a Teacher and want to find out about Classroom, or the other way around. Here are some examples:

  • Teacher teaches-in Classroom
  • Teacher has-desk-in Classroom
  • Teacher uses Classroom
  • Classroom is-occupied-by Teacher
  • Classroom hosts Teacher

Any of these are going to be represented similarly in a UML diagram:

+-------+ teaches-in      +---------+     +-------+ has-desk-in     +---------+
|Teacher|---------------->|Classroom|     |Teacher|---------------->|Classroom|
+-------+                 +---------+     +-------+                 +---------+

+-------+ uses            +---------+
|Teacher|---------------->|Classroom|
+-------+                 +---------+

+---------+ is-occupied-by  +-------+     +---------+ hosts           +-------+
|Classroom|---------------->|Teacher|     |Classroom|---------------->|Teacher|
+---------+                 +-------+     +---------+                 +-------+

Any of them are also going to be implemented similarly in C++.

class Teacher {
  private:
    // Rooms in which a teacher might teach
    vector<Classroom> teaches_in;
  ...
};

class Teacher {
  private:
    // Room where teacher's primary desk is
    Classroom* room_where_desk_is;
  ...
};

class Teacher {
  private:
    // Classrooms that a teacher uses.
    vector<Classroom> rooms_used;
    ...
};

class Classroom {
  private:
    // Teachers that occupy this room during the day
    vector<Teacher> is_occupied_by;
    ...
};

class Classroom {
  private:
    // Primary teacher that this room hosts
    Teacher* host;
    ...
};
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

I can't see an obvious explanation for why your second cout statement doesn't print anything. However, one flaw in this function is that the args[] and args1[] arrays must end with a 0 as their last element (a Unix requirement, not a C++ one) -- otherwise how does execvp() know where the arguments end?. In the child's execvp(), args1 isn't terminated with a 0; and in the parent's execvp(), the second argument should be args+pipeno1+1 .

Your function works for me if I fix that, so maybe your args array and pipeno1 aren't set up the way you think they are.
You might try restructuring the code a little to do all the args[] string manipulation without doing the fork/exec. Verify that the strings are as expected, then add the fork/exec. In the current form, since the code goes on to fork/exec and starts manipulating file descriptors, you may be getting I/O interactions that are hiding your cout statements.

boblied 26 Newbie Poster

Association can have lots of meanings. Any reference from one class to another is a kind of association. "Has-a" is one; it could also be "uses."

"Has-a" is often called aggregation or composition. In terms of C++; it's commonly implemented by a member variable of the owned type, or by having a pointer or reference in the owning class to the owned class. The initialization of the member variable or pointer happens in the constructor (for example, if the teacher is permanently assigned to a room).

"Uses" can also be implemented as a pointer in the using class to the used class, but in this case, the pointer might be changed after construction by a setter method (for example, if the teacher moves from room to room).

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

Just guessing here, but rethink your break statements. Maybe you want to reorganize to find the matching ID, then break out of the loop and finish the location lookup.

bool found = false;
for ( int c = 0; c < student_max ; ++c )
{
    if ( search_id == id[c] )
    {
        found = true;
        break;
    }
}
if ( found )
{
    location = // etc
}
else
{
    cout << "invalid id;
}

Another possibility: what is binary_search() doing? Isn't it going to return the same index you already discovered in c? Do you even need the for loop? Could you just validate that search_id is in range and then use binary_search to find the location?

if ( search_id >= 0 && search_id < student_max )
{
    location = binary_search(id, student_max, search_id);
    cout << ....
}
else
{
    cout << "invalid";
}

Or is that supposed to be std::binary_search? If it is, you're using it wrong; check your documentation.

Like other people have noted, you'll need to be clearer on what you think is wrong, and possibly include some more of the program leading up to this loop.

boblied 26 Newbie Poster

You can return entire structures from functions.

Compass getCompass()
{
    Compass result;
    result.C = make_pair(3,4);
    // etc.
    return result;
}

void someFunction()
{
    Compass V = getCompass();
    int j = V.C.first;
    // ...
}
{
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
std::string s;
  // ... code that reads from file into s ...
if ( s.find("xxx") != std::string::npos )
boblied 26 Newbie Poster

Couple o' things:

Search has to return a pointer, so don't return info, return the pointer of the node you're looking at.

The recursive call of Search() has to take the same parameters as the first call, so don't forget to pass key.

That while loop at the end of search is trouble: if the condition wasn't true before calling Search(), it
still won't be true after you call search -- infinite loop. A simple if would do -- the recursion is taking the place of looping.

In FindMax, it would probably be helpful to pass the biggest value found so far as an input parameter.

blackhawk9876 commented: thx +2
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

Hello, I have a question, when does a global variable get destructed?

The compiler arranges for code to get executed both before main() is called and after it returns. Before main(), global constructors are called. After main(), global destructors are called in opposite order of their construction.

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

Another use is to hold a collection of heterogeneous objects. For instance, if you have the classic example of things derived from Shape (triangle, circle, etc). If you make an array of Shape objects, then they are just base objects (not triangle etc). But if you make it an array of pointers to Shape, then you can access the derived classes polymorphically.

boblied 26 Newbie Poster

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