Narue 5,707 Bad Cop Team Colleague

Change this:

int numb, tot, count, sum;

to this:

int numb, tot, count, sum = 0;

sum was uninitialized for the first run, but after the first run you set it to 0, so the second and third runs work properly.

Comatose commented: You Rock ;) +1
Narue 5,707 Bad Cop Team Colleague

You need to define an operator>> that looks for an object of your enum:

#include <iostream>
#include <stdexcept>

using namespace std;

enum X { A, B, C };

istream& operator>> ( istream& in, X& x )
{
  int val;

  if ( in>> val ) {
    switch ( val ) {
    case A: case B: case C:
      x = X(val); break;
    default:
      throw out_of_range ( "Invalid value for type X" );
    }
  }

  return in;
}

int main()
{
  X x;

  try {
    cin>> x;
    cout<< x <<endl;
  } catch ( out_of_range& ex ) {
    cerr<< ex.what() <<endl;
  }
}
Narue 5,707 Bad Cop Team Colleague

>But I want to know the proper way if you can kindly tell me
I did kindly tell you. Use extern declarations in your header files, provide a single source file for definitions, and the linker will do the rest. You only need a declaration for the code to compile.

Narue 5,707 Bad Cop Team Colleague

>When i do a for loop yes it prints the string but every word becomes a capital letter.
If you only want the first letter to be capitalized, only call the function on the first character:

char s[] = "this is a test";

s[0] = toupper ( s[0] );
printf ( "%s\n", s );
Narue 5,707 Bad Cop Team Colleague

>By the way, is there some sort of parent class that all STL containers descend from?
No. In fact, all of the standard containers are concrete classes and can't be safely used as base classes because they lack a virtual destructor.

>Does this make sense?
Sorted and not sorted are only minor details. For example, a set doesn't support random access iterators while a vector does. So your container's public interface would change drastically because non-sorted is a sequence container and sorted is an associative container. If you want a sorted vector then the standard sort algorithm can be used periodically, or you can create your own sorted sequence class that contains a vector and supports the operations that you need for it.

Narue 5,707 Bad Cop Team Colleague

>So is a set like any other list/vector except that it's sorted?
Sure, you can think of it like that for the time being. :)

Narue 5,707 Bad Cop Team Colleague

>Hmmm...you lost me.
Joy.

>Where did this default value T() where T is the type of the array stuff come from?
It's a generic way of saying that all of the ints are set to 0. However, you can do this:

int x = int();

And set x to the default value of an int, or 0. To make the description generic, one would say T() instead of int() where T refers to an arbitrary, but unknown type.

>I don't own a computer remember?
That makes teaching you C++ a reall pain in the butt. There are just some things you have to experiment with to understand.

Narue 5,707 Bad Cop Team Colleague

>*sPtr = toupper(*sPtr);

*sPtr = toupper ( (unsigned char)*sPtr );

Since you can't be sure that *sPtr was a character returned by a standard input function. Better safe than sorry in these situations. ;)

Narue 5,707 Bad Cop Team Colleague

In a set, the value is the key. In a map, the key and value are distinct.

Narue 5,707 Bad Cop Team Colleague

>Please let me know if you think this is a poor scheme
I think it's a poor scheme, but since it works for you and you can't seem to figure out how to set up your file dependencies properly, go ahead and use it.

Narue 5,707 Bad Cop Team Colleague

>But what about graphics and using Assembly?
What about graphics and using assembly? RTFM first if you have an compiler specific question.

Narue 5,707 Bad Cop Team Colleague

I've seen excellent attempts to get other people to do homework, but this isn't one of them. Thread locked.

Narue 5,707 Bad Cop Team Colleague

What happens if you try to use a standard container, such as map? Same errors? Or is it specific to the extended containers and algorithms?

Narue 5,707 Bad Cop Team Colleague

>^^^does that mean the array size is 100 and the first element is equal to 0?
An array of size 100 and all elements are initialized to 0. If you privide an initializer list and there aren't enough values, all remaining elements are initialized to the default value T() where T is the type of the array.

>If so, does that mean you array will look like this a[0, 100, 200,..., 99,000]?
Try it and see.

>^^^Does this mean p points to array a and the array's size equals 5?
Yes.

>why isn't the output 0 100 200 300 400 instead of 0 10 20 30 40?
It is. The comment and code don't match.

Narue 5,707 Bad Cop Team Colleague

>I've got to implement Prolog in C++ for an assignment.
Let me get this straight. You're new to C++ and you've been given an assignment to write a Prolog interpreter in C++? I find that hard to believe. It's easier to believe that you slacked off for most of your C++ course and now you're feeling the hurt because you don't know jack and you have a challenging project that's due soon.

Narue 5,707 Bad Cop Team Colleague

>if(!last)
This will always be true.

Narue 5,707 Bad Cop Team Colleague

>printf( msg );
Bad idea. If msg contains any sequences of characters that look like a format modifier, you'll invoke undefined behavior. Do this instead:

printf( "%s", msg );

>sometimes some of threads freezes
Sounds like a deadlock to me. It also sounds like a perfect opportunity to search google for similar problems and solutions.

Narue 5,707 Bad Cop Team Colleague

Show your attempt. Tell us what you learned while doing it. Otherwise, we'll assume that you want a handout and nobody will help you.

Narue 5,707 Bad Cop Team Colleague

>Is it just defining the number of characters assigned for the message?
Yes.

Narue 5,707 Bad Cop Team Colleague

Lose the static qualifier. extern should be placed on the array in the header file to ensure that it's a declaration. Then include file1.h in both file1.c and file2.c. Make sure that the code to fill the array is called first if you define it in file1.c.

Narue 5,707 Bad Cop Team Colleague

>The base class is point .
Inheritance isn't the right solution for point. A shape is not a point. A shape has one or more points, so containment is a better solution. point should be a stand-alone class. I really hate the shape abstraction as a means of introducing inheritance, but consider this:

struct Point {
  int x, y;
};

class Shape {
  /* No points */
public:
  virtual void draw() const { cout<<"Shape"<<endl; }
};

class Ellipse: public Shape {
  Point center;
  int radius;
public:
  virtual void draw() const { cout<<"Ellipse"<<endl; }
};

class Rectangle: public Shape {
  Point top_left;
  Point bottom_right;
public:
  virtual void draw() const { cout<<"Rectangle"<<endl; }
};

It's much harder to work out a coherent hierarchy if all of the shapes derive from Point.

Narue 5,707 Bad Cop Team Colleague

>Anybody can help to explain why "m_line.replace(idx, m_search.size(), replace);" doesn't compile in cygwin
Perhaps because replace isn't a valid third argument? How about:

m_line.replace(idx, m_search.size(), m_replace);
Narue 5,707 Bad Cop Team Colleague

>Uh, yeah. Parse error between monitor and chair.
Wetware compilers are so unreliable. Sounds like you need an upgrade. ;)

Narue 5,707 Bad Cop Team Colleague

>if( first >=0)
This is never true since you initialize first to -1. Initialize it to 0 and the vector should work more like you expect.

Narue 5,707 Bad Cop Team Colleague

>what will happen if we free the same memory two times in C
Undefined behavior. Anything could happen.

Narue 5,707 Bad Cop Team Colleague

>why set final value to '\0'
Because that's the definition of a string in C. A sequence of zero or more characters terminated by a null character ('\0').

>and why are there two printf's
On is inside the loop and prints all but the complete string, and the last is outside the loop and prints the complete string. You can print the value of i to see why:

#include <stdio.h>

int main()
{
  char *p = "HELLOWORLD";
  int i;

  for ( i = 1; p[i] != '\0'; i++ )
    printf ( "%d\t%.*s\n", i, i, p );
  printf ( "%d\t%.*s\n", i, i, p );

  return 0;
}
Narue 5,707 Bad Cop Team Colleague

>This is not likely ever to be true.
You mean false? I'd say it's highly likely to be true since EOF must be a negative quantity and characters gathered from a narrow stream must fit within an unsigned char. Fortunately, the other half of the condition will cause the loop to terminate properly.

Narue 5,707 Bad Cop Team Colleague

>Well how come you couldn't consider that an answer?
Because your explanation was so convoluted I didn't understand it, but it didn't smell right. ;)

Narue 5,707 Bad Cop Team Colleague

Daniweb's IRC channel is boring, and it's everybody's fault. The more people that go there, the more fun it will be, so I order everyone to get an IRC client (because Java clients suck) and go to irc.daniweb.com, port 6667 and join #daniweb.

This goes double for people who visit the Software Development forums because I'll be there and you'll get smarter just by associating yourselves with me in real-time.

We now return you to your regularly scheduled programming.

Narue 5,707 Bad Cop Team Colleague

>In that case, you would be instantiating a class to get to the static
Huh? No, not at all. The following is legal:

class Outer {
  static class Nested {}
}

class Test {
  Outer.Nested obj = new Outer.Nested();
}

But this is not:

class Outer {
  class Nested {}
}

class Test {
  Outer.Nested obj = new Outer.Nested();
}

The difference is that a static class is somewhat like a static data member. An instance isn't required to access it.

Narue 5,707 Bad Cop Team Colleague

>Would both abstract and static be correct answers?
No, abstract is the correct answer. static only means that a nested class doesn't designate an inner class.

Narue 5,707 Bad Cop Team Colleague

>What should i do with b?
How should I know, it's your program. ;) You put b there for a reason, right?

Narue 5,707 Bad Cop Team Colleague

>That one is more advanced than I am
Aroo? And then you post a manual attempt at it? :eek:

>while (x - (a)(a)(a)) < 0.001)
(a)(a)(a) likely doesn't do what you think it does. ;) Perhaps you were trying to do:

while (x - (a * a * a)) < 0.001)

>a = b;
b doesn't have a value yet. You'll probbly get strange results.

Narue 5,707 Bad Cop Team Colleague

>it would greatly help me to see the entire program from start to finish

#include <cstdlib>
#include <cmath>
#include <iostream>

int main()
{
  int val;

  std::cout<<"Enter a number: ";
  
  if ( std::cin>> val ) {
    std::cout<<"Cube root of "<< val <<": "
      << std::pow ( std::abs ( val ), 1.0 / 3.0 ) <<std::endl;
  }
}
Narue 5,707 Bad Cop Team Colleague

IIRC:

pow ( abs ( val ), 1.0 / 3.0 )
Narue 5,707 Bad Cop Team Colleague

>Everyone misunderstood his question.
No, apparently only paradox misunderstood. Everyone else seemed to get the joke.

Narue 5,707 Bad Cop Team Colleague

Lazy people who want hand outs deserve what they get.

Narue 5,707 Bad Cop Team Colleague

>and the insults not so much
I can live with that.

>Why do you think I need both?
Users of your class will rightfully expect Dan + string to work just as well as string + Dan. Of course, in hindsight I should have written:

string operator+ ( const Dan& d, const string& s )
{
  return d.toString() + s;
}

Instead of pasting the body of the other operator+. This way users will get the expected behavior for string concatenation.

>I changed it to return the first + ", " + last directly
Not a good idea. Eventually you'll learn that public data members are a bad thing and make them private, then your code will break. You'll either have to use a public interface for the operators, or make them friends of your class. Both of those entail work on your part, so it's better to do it right from the start.

>since first and last are not const so I couldn't return them using the toString.
That shouldn't matter. A non-const object can be converted to a const object implicitly. The inverse is not true, but that's not the case here. Show me the code that's causing the error, preferably something I can compile directly so I don't accidentally fix something by writing framework code.

Narue 5,707 Bad Cop Team Colleague

>I tried what you posted
No, you didn't. You tried some oddball variation of what I posted after filtering it through your n00b brain. Neither my code nor my comments suggested that operator+ should be a member function. Since toString is a public member function of Dan, neither of the overloads for operator+ should even be friends of Dan. Like this:

class Dan {
  // Data members
public:
  string toString() const;
  // No operator+ here
};

string operator+ ( const string& s, const Dan& d )
{
  return s + d.toString();
}

string operator+ ( const Dan& d, const string& s )
{
  return s + d.toString();
}

>but it complains
I'm not surprised. When implemented as a member function, operator+ expects only one argument. When implemented as a non-member function, operator+ expects two arguments. The two are not interchangeable.

Narue 5,707 Bad Cop Team Colleague

>lol, i thought you was the boss?
Not the boss boss, just the boss. I can still get fired by any of my superiors.

Narue 5,707 Bad Cop Team Colleague

>actually I don't believe they are...
The mad literalists answered the only question that was posed:

Does ne one know how to answer this question?

Since it's clearly a homework assignment, nobody with half a brain would just give away the answer. ;) Unless of course the answer is given in such a way as to be too cryptic to be useful, such as my answer.

Narue 5,707 Bad Cop Team Colleague

>>traversing
>?
Going from one end to the other.

>wanna play chess real quick?
Of the many things that I can do at work, playing chess would raise too many eyebrows.

Narue 5,707 Bad Cop Team Colleague
string operator+ ( const string& s, const Dan& d )
{
  return s + d.toString();
}

Since Dan doesn't have a suitable conversion to std::string, you also would need to provide another operator+ with the commutative operation:

string operator+ ( const Dan& d, const string& s )
{
  return s + d.toString();
}

toString should also be const since it doesn't modify the state of your object:

string toString() const
{
  return first + ", " + last; 
}
Narue 5,707 Bad Cop Team Colleague

I answered your question in the original thread that you asked it, but for convenience I'll copy the answer here:

>Can you please clarify the difference between Sequential Access Files and Random Access Files?
Humor my love for simile. A sequential access file is like traversing a linked list, while a random access file is like indexing an array. The same advantages and disadvantages apply.

Narue 5,707 Bad Cop Team Colleague

>Can you please clarify the difference between Sequential Access Files and Random Access Files?
Humor my love for simile. A sequential access file is like traversing a linked list, while a random access file is like indexing an array. The same advantages and disadvantages apply.

Narue 5,707 Bad Cop Team Colleague

>but how can i do it using the previous program i sent
I'm not going to do your homework for you. My code serves as a template that you can use to figure out the solution to your problem.

Narue 5,707 Bad Cop Team Colleague

I want to know what threads are being viewed, if anyone is replying to a thread, or creating a thread, all without leaving the forum. ;)

Narue 5,707 Bad Cop Team Colleague

Just reverse the counter so that it goes from back to front instead of front to back:

#include <stdio.h>

int main ( void )
{
  const char *p = "HELLOWORLD";
  int i;

  for ( i = 10; i >= 0; i-- )
    printf ( "%.*s\n", i, p );

  return 0;
}
Narue 5,707 Bad Cop Team Colleague

>I think each sub-forum displays active members at the bottom of the forum
It does, but not nearly as detailed as the global active members list.

Narue 5,707 Bad Cop Team Colleague

>display teh string "HELLOWORLD" in ascending style
Riiight. Care to explain what you mean by "ascending style"?

>what does %.*s stand for???
The . is a field width modifier. Any value after it is how many characters of the string should be printed. The * is a placeholder for a value. In your call, * is replaced with the value of i. This may help you to understand a bit better:

#include <stdio.h>

int main ( void )
{
  printf ( "|%.*s|\n", 5, "helloworld" );
  return 0;
}