Narue 5,707 Bad Cop Team Colleague

Do some research on strcat.

Narue 5,707 Bad Cop Team Colleague

>If you base your philosophy on not cheating, then you become to be cheated
Cheat everyone else before they can cheat you, eh? That sounds like the perfect way to rationalize immoral behavior.

Narue 5,707 Bad Cop Team Colleague

>Who do you think cares quality in todays world?
You fail. My opinion of you just dropped considerably.

>you are American, you know what i mean..
Apparently you only hang around with lowlifes and drains on society. I prefer to surround myself with people who wouldn't embarrass me if they met my parents.

>I think this second person doesn't have a higher productivity.
You asked how cheating works when you have a job. There was no requirement that the person doing the cheating had to be a leech who shouldn't have gotten the job in the first place. There are three kinds of "cheating":

1) Cheating because you don't have the time to write something yourself. This is where quality slips because you could be pinching a bad piece of code and won't be able to maintain it. This is lazy cheating, but it has its place.

2) Cheating to see how other people did something. This is what I encourage because not only do you learn, you also benefit from the effort of other people without expending the same amount of effort yourself.

3) Cheating because you're too incompetent to do the job without cheating. This is typically the result of cheating in school.

Narue 5,707 Bad Cop Team Colleague

For the sake of getting you doing the right thing, I'll suggest a mnemonic. Conditional and loop statements can be treated syntactically the same as functions. The "tag" isn't terminated with a semicolon, and the body is surrounded with braces:

void reverse(string& st)
{
  int i; 
  int j = 1;
  for(i=0; i<strlen(str); i++, j++)
  {
    rev[strlen-[j]=st[i];
  }
  for(i=0; <strlen(st); i++)
  {
    str[i]=rev[i];
  }
  st="New World";
  return;
}

This mnemonic is useful for following the best practice while avoiding the particular error that you have, which is accidentally ending the loop tag with a semicolon:

for ( i = 0; i < n; i++ ); // Wrong!

Note that loops don't have to have braces, and the first statement after the loop tag will be the body of the loop. The above wrong code is treated like so:

for ( i = 0; i < n; i++ )
{
  ;
}

Hopefully you can locate the error in the code you posted now.

iamthwee commented: using strlen within a for loop, naughty naughty! -4
William Hemsworth commented: iamthwee should read the OPs code +9
Narue 5,707 Bad Cop Team Colleague

>How does this cheating thing works when you have an actual job? Tell me, I'm curious..
Cheating when the goal of an assignment is to learn only hurts yourself. "Cheating" when the goal is to get a job done helps productivity, though not necessarily quality.

Narue 5,707 Bad Cop Team Colleague

After you close the file for the first time, the eofbit is still set. You need to clear it with fis.clear(); before the second loop will work. Apparently your Linux compiler chose to clear the state either on close or open.

Narue 5,707 Bad Cop Team Colleague

Thanks for that skatamatic. You achieved the desired effect of me chat raping you without any action on my part. :icon_rolleyes:

If either of you have any further comments for me, I'll direct you to my PM so that we don't drive the OP's thread too far off topic.

Narue 5,707 Bad Cop Team Colleague

>I can't find a reason why not to thrash any one like that !!.
I can think of a few likely ones, otherwise I wouldn't have commented. Not the least of which is the usual belief beginners have that the rest of us can divine the exact problem they're having without being given specific information. That's not a crime by any means, and bitching about effort is the wrong response.

p.s. You can't take back something once it's said. I've tried, and it doesn't work.

Narue 5,707 Bad Cop Team Colleague

>And show some freakin effort!
>Read your text book!!
>I mean please at least TRY.
If it's simply a matter of being vague, there's no point in biting heads off when you could just as easily ask for more information, like "what have you tried so far?" or "what are you finding difficult?". Please reserve the snarky comments until you're sure the OP deserves it or you'll just come off as a jackass.

Narue 5,707 Bad Cop Team Colleague

>gets(char);
FYI, booze in fact does not improve your ability to write code. You'd have to be quite confused to write char when you meant firstname, and any experienced C programmer would have to be drunk to use gets.

Narue 5,707 Bad Cop Team Colleague

>Is that the exactly same C++ compiler used in Turbo C++ Explorer Professional?
Assuming you mean the latest version of C++ Builder, no. Turbo Explorer is a stable code freeze and the bleeding edge has moved on since then. Though in my opinion you're unlikely to notice a difference unless you're a rather advanced C++ developer keeping up with the work on C++0x.

>Is it really true that it's * fully * compatible with the ANSI/ISO C++ standard?
I don't believe anybody has claimed that it is (along with every other compiler vendor except perhaps Comeau). Generally the unimplemented features are so obscure or problematic that there isn't sufficient demand to justify the cost of adding them. This is especially true with tricky and expensive implementations like export.

Narue 5,707 Bad Cop Team Colleague

>Can someone please give his opinion about the free
>available borland compiler, is it good to use nowadays?
I assume you're talking about the 5.5 command line compiler, which is good, but it's starting to get dated. The latest and greatest free compiler is a part of Borland's Turbo C++ Explorer project. However, if you think Code::Blocks is too bloated, you probably won't be able to find any IDE that isn't.

If you like MinGW, you don't have to use it with an IDE. The IDE is not the compiler, in pretty much every case. Even with Microsoft you can use cl.exe directly from the command line.

>Ok, agreed with that Microsoft crap
That "crap" happens to be an excellent IDE. You might be surprised to learn that other people find the "bloated" features terribly useful.

>this applies also to the M$ ones
You seem like the kind of person that would be better off switching to Linux. At least that way your name calling will seem less clueless and hypocritical.

Narue 5,707 Bad Cop Team Colleague

>I need to do this because there is code where I call
>a function that exists in A2 but not in A1 for instance.
Template specialization is what you want.

>After all, the static constant used is known by the compiler before running...

#include <iostream>

int main()
{
  int x = 10;

#if x == 10
  std::cout<<"fizz\n";
#else
  std::cout<<"buzz\n";
#endif
}

Why doesn't this work? Both x and the value it's initialized with are known by the compiler before running... Do you realize how silly your rationalization sounds?

Narue 5,707 Bad Cop Team Colleague

>Is it possible to create your own data type? Not a class, or a
>structure, that builds from predefined C++ types, but your very own.
What do you think a class is supposed to be? It's a user-defined type. What you seem to want is magic: a magic type that magically does something useful without any underlying logic. It doesn't work that way. There's no magic in programming. There's always a base that you build on top of to create something new. In the case of C++, you create new types by leveraging the power of existing types. I find your question to be misguided and nonsensical.

>Also, I would like to know what is the smallest size you can address in C++
char is the smallest addressable unit in C++.

Narue 5,707 Bad Cop Team Colleague

It sounds disturbingly like you're looking for a problem to fit the solution you recently learned. That's silly.

Narue 5,707 Bad Cop Team Colleague

>This compiles
Probably not cleanly. Check your warnings.

>Can someone explain me why does this happen
Aside from the fact that you're trying to access a run-time value at preprocessing time (ie. long before it exists)?

Narue 5,707 Bad Cop Team Colleague

When you overload the () operator, it's used as if it were a function call operator and nothing more:

#include <iostream>

struct foo {
    void operator()()
    {
        std::cout<<"FOO!\n";
    }
};

int main()
{
    foo bar;

    bar();
}

If that's not what you want, you're SOL.

Narue 5,707 Bad Cop Team Colleague

>It is incorrect to say that a is "converted" to a pointer.
Quite the contrary, my confused friend. It's incorrect to say that an array name "is" a pointer. I'm happy to quote chapter and verse from the C++ standard if you'd like.

>That's what an array name is, a constant pointer to the first element.
You haven't thought this through, have you? If an array name is a constant pointer, how do you explain the behavior of sizeof? If an array name is a constant pointer then there's a lot of voodoo that needs to take place for sizeof to produce the total bytes in an array. On the other hand, if an array name is converted to a pointer only in value context[1], everything falls neatly into place.

Calling an array a pointer is largely harmless as long as you don't try to teach it to others, because there are really only a few cases where an array is not converted to a pointer to the first element. Being wrong in this case generally won't make your code worse in practice. But it makes perfectly sensible concepts seem like magic when you have a fundamental misunderstanding about how things work.

[1] The standard talks about using arrays in value context versus object context, where the conversion applies to value context. The majority of uses are value context, thus the rule nearly always applies.

Narue 5,707 Bad Cop Team Colleague

>how much memory does the following take?
>const int a[];
>answer: I don't think it is defined.
It's quite well defined. You're referring to a function parameter, which (due to the conversion of arrays to pointers) is equivalent to const int *a .

>+ Outside class: It outputs 3 (correct)
>+ Inside class: It outputs 1 (incorrect)
The result is different because the type is different inside and outside of the member function you're calling. In main, the type you give to sizeof is int[3] . In the member function, the type you give to sizeof is int* .

Why is that? Because in just about every case when using arrays, the array name is converted to a pointer to the first element. For example, when you subscript an array (ie. a[2] ), first a is converted to a pointer to the first element, then 2 is added to that pointer, finally, the whole construction is dereferenced to give you the value at a[2] . You may have learned that a[i] is equivalent to *(a + i) . Now you know why.

The problem is that passing an array to a function (or member function) applies this rule but sizeof does not. When you pass an array to a function, the array is converted to a pointer to the first element. When you give an array to sizeof, it doesn't perform any conversion because that wouldn't make sense. What use would sizeof …

Narue 5,707 Bad Cop Team Colleague

>Can somebody explain why in the code below, when i use free(), it crashes.
That's easy. You corrupted the memory and the dynamic memory manager is choking because the corruption removed something it expects to see. A common situation that causes this problem is failing to allocate enough memory for the entire string and the terminating null character, but you'll need to do some index counting to find out exactly where the corruption is (you're writing beyond the boundaries of the memory you allocated).

Narue 5,707 Bad Cop Team Colleague

The cast is bad in C. It hides potential errors such as forgetting to include <stdlib.h> and makes maintenance more difficult if you change the type of the pointer, so you'd best get into the habit of not casting the return value. Pointers are implicitly converted to and from void* with no complaints, so there's no good reason to cast anyway.

Here's a much improved form:

/* Allocate one object */
p = malloc ( sizeof *p );

/* Allocate N objects */
p = malloc ( N * sizeof *p );

The cast is a throwback from the dark ages when char* was the generic pointer rather than void* and there wasn't an explicit conversion rule in place.

Freaky_Chris commented: Thanks +4
Narue 5,707 Bad Cop Team Colleague

At a glance I'd say it's because you're seeking when you don't need to seek, and your position calculations are incorrect. Let's review:

>f.seekg(0, ios::beg);
>fnew.seekp(0, ios::beg);
You just opened these files, they're already at the beginning.

>fnew.write(reinterpret_cast<char *>(transferTest), thisSize);
>fnew.seekp(testNo*thisSize, ios::beg);
When you write to an output stream, it updates the put pointer automatically. If testNo*thisSize doesn't equal what the pointer is at currently every single time (which brings up the question of redundancy and why you're even bothering with seekp), you're looking at either holes in the file or corrupted data.

Here's a more intuitive solution:

ifstream in ( "input", ios::binary );
ofstream out ( "output", ios::binary );

if ( in && out ) {
  int testNo = 0;
  Test record;

  while ( in.read ( (char*)&record, sizeof record ) ) {
    if ( !pending_delete ( ++testNo ) )
      out.write ( (char*)&record, sizeof record );
  }

  in.close();
  out.close();
}

pending_delete would, of course, be a wrapper for the current hard coded loop you have:

bool pending_delete ( int row )
{
  for (int i = 0; i < noOfSelectedRows; i++) {
    if (row == selectedRows[i])
      return true;
  }

  return false;
}

On a side note, unless your Test class is a POD type, using direct I/O is unsafe. A better all around solution would be to support either overloaded << and >> operators for stream I/O, or a to_string member function for serializing object of Test into a string.

Narue 5,707 Bad Cop Team Colleague

This is a link to the draft of the next C++ standard:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2691.pdf

It's a PDF, so you can search for key phrases such as "order of evaluation".

Narue 5,707 Bad Cop Team Colleague

>The order of operands evaluation (from left to right) is defined
>for comma operator, operator && and operator || only...
But only the built-in version. When you overload them yourself, you lose the order of evaluation guarantee, which is why it's not recommended that you overload those particular operators.

>I think that the 2 cases I mentioned are a bugs in cout
Q: How many legs does a dog have if you call the tail a leg?
A: Four. Calling a tail a leg doesn't make it a leg.

Narue 5,707 Bad Cop Team Colleague

>Could you please mention some of those significant differences?
I could, but because I would be listing something that's equally well listed in any decent book on C++, I'll direct you there instead. Or you could check the C++ FAQ Lite, which I recall referring you to before.

Narue 5,707 Bad Cop Team Colleague

>why does the syntax of the language gives me two ways to the same thing?
Pointers were inherited from C and references were added with C++ to support other C++ features. It's basically the same principle as structs and classes, except pointers and references are actually different in significant ways.

Narue 5,707 Bad Cop Team Colleague

Start by searching the forum so you can discover that this question has been asked and answered quite a few times already.

Narue 5,707 Bad Cop Team Colleague

http://www.cplusplus.com/reference/clibrary/ctime/

Here's an unrelated example to get you started:

#include <stdio.h>
#include <time.h>

const char *suffix ( int thing )
{
  switch ( thing ) {
    case 1: return "st";
    case 2: return "nd";
    case 3: return "rd";
    default: return "th";
  }
}

int main ( void )
{
  time_t now = time ( NULL );
  struct tm *date = localtime ( &now );

  printf ( "Today is the %d%s day of the %d%s month in the year %d\n",
    date->tm_mday, suffix ( date->tm_mday ),
    date->tm_mon + 1, suffix ( date->tm_mon + 1 ),
    date->tm_year + 1900 );

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

There's no "date" function in C. Be more specific or don't be surprised when nobody answers your question.

Narue 5,707 Bad Cop Team Colleague

>Do you do the flow chart first or go straight to the code?
This question assumes that the only alternative to diving into code is drawing a flow chart. I'm anal about careful design before writing code, but I rarely draw flowcharts unless some management type asks for it.

>I find it easier to do the code first for some reason, maybe this is bad?
You'll discover that past a certain level of complexity, this hacker mentality begins to break down quickly. This is the biggest problem I have with hiring rookies. They don't have sufficient experience to appreciate the critical role design has in a successful project.

Narue 5,707 Bad Cop Team Colleague

What compiler are you using?

Narue 5,707 Bad Cop Team Colleague

Perhaps you could explain what you expect your code to do and what it's doing that fails to meet your expectation.

Narue 5,707 Bad Cop Team Colleague

>why do i see this?
You've indirectly discovered a limitation of using arrays as class members. You can't initialize them with arbitrary data. The only way to "initialize" an array member is through assignment as Sky Diploma suggested.

The closest you can get is default initialization:

test::test(): denemeler() {}
Narue 5,707 Bad Cop Team Colleague

>An aggregate class is [...]
Also known as a POD (plain old data) type.

>how will i fill the items of that array?
If you want to initialize it with a list like with the single C object, it's just the nested equivalent (really no different in principle from how you initialized d):

struct C c[4] = {
  {1, 2},
  {3, 4},
  {5, 6},
  {7, 8}
};

After the array is initialized (whether you use an initializer or not), you can't use an initializer later on because they're only allowed in definitions, so this is illegal:

C c[4];

c[0] = {1, 2};

In such a case you would need to manually fill out the data members:

C c[4];

c[0].a = 1;
c[0].b = 2;
serkan sendur commented: without her it would be so hard +3
Narue 5,707 Bad Cop Team Colleague

Perhaps a stream manipulator might make the most sense:

class xml_format {
  xml_data _data;
  vector<format_info> _info;
public:
  xml_format ( const xml_data& data, const vector<format_info>& info )
    : _data ( data ), _info ( info )
  {}

  friend ostream& operator<< ( ostream& out, const xml_format& fmt )
  {
    // Write the formatted data
    return out;
  }
};
Narue 5,707 Bad Cop Team Colleague

Can you be more specific about what you're trying to do and why?

Narue 5,707 Bad Cop Team Colleague

>why is the compiler designed not to accept
>any non constant static initialization of types?
You can (technically) initialize static data members with non-constant values, just not directly within the class definition:

#include <cstdlib>
#include <iostream>

class foo {
public:
  static int x;
};

int foo::x = rand();

int main()
{
  std::cout<< foo::x <<'\n';
}
Narue 5,707 Bad Cop Team Colleague

You can't call a constructor after the object is created. By the time you get to class2's constructor body, myClass will already have been constructed. However, because class1 doesn't have a default constructor, this won't compile because there's no constructor to call. You can fix it by using an initialization list:

class2::class2(): myClass(5) {}

Also note that class2 myFinalClass(); is probably not doing what you think it's doing. It's actually a prototype for a function taking no parameters and returning an object of class2.

serkan sendur commented: i respect her knowledge so much +2
Narue 5,707 Bad Cop Team Colleague

>Queue q = (Queue)malloc(sizeof(Queue));
malloc always returns a pointer, so the type you're storing the result in must also be a pointer. For future reference, you don't need the cast in C (in fact, it hides potential errors). A much more convenient way to use malloc is:

/* For allocating a single object of type T */
T *p = malloc ( sizeof *p );

/* For allocating N objects of type T */
T *p = malloc ( N * sizeof *p );
Narue 5,707 Bad Cop Team Colleague

>So which context is this...
You answered your own question, it's value context. Why? Because the subscript operator arr[2] is syntactic sugar for *(arr + 2). Clearly (arr + 2) is a subexpression which results in a value: the address offset by two elements. The indirection operator then takes that address value and retrieves the object that's required by the assignment operator.

Narue 5,707 Bad Cop Team Colleague

>My question is: "Is array name a pointer (rather a constant pointer)"??
No, but the reality is similar. An array name is converted to a pointer to the first element most of the time. The official standard terminology is that an array name is converted to a pointer to the first element when used in a value context as opposed to an object context. A value context is when you're retrieving and subsequently using the value of an object, such as the right hand side of x = y; . An object context is when you're modifying or querying the object itself, not the value it contains. The left hand side of x = y; is one example.

People try to make sense of the array-to-pointer conversion rule by saying that the array is converted to a constant pointer, and that's why you can't assign to it or perform modifying arithmetic such as ++a . What really happens is that operators such as ++ see the operand in an object context, and those operators aren't defined for array types. The conversion is not made at all, so pretending that you're working with a constant pointer rather than an array is a fundamental misunderstanding.

There are a few places where an array can legally be used in an object context, which is why saying an array name is a pointer is wrong. Two of the common places an array is used in object context are:

  1. As an …
me_ansh commented: Was an eye opener +1
Narue 5,707 Bad Cop Team Colleague

>Which is in higher demand in the job market right now???
Which job market? If you want a job where Java excels, clearly Java will be more in demand. Vice versa with C++.

Narue 5,707 Bad Cop Team Colleague

What you want is a multi-key search. If the grades match, compare another key. One way is to do it directly. That way you have more control over ascending and descending. For example if you want to sort the grade descending, but the other key (like a name) ascending:

#include <algorithm>
#include <iostream>
#include <string>

struct Student {
  std::string name;
  int avg_grade;
};

bool multikey_compare ( const Student& a, const Student& b )
{
  if ( a.avg_grade == b.avg_grade )
    return a.name < b.name;

  return a.avg_grade > b.avg_grade;
}

int main()
{
  Student list[] = {
    {"Joe Blow", 80},
    {"Jane Doe", 80},
    {"Fonzie", 60},
    {"The Dude", 70},
    {"Some Guy", 80},
    {"Z Man", 90},
    {"John Doe", 90},
    {"A Man", 70}
  };

  std::sort ( list, list + 8, multikey_compare );

  for ( int i = 0; i < 8; i++ )
    std::cout<< list[i].avg_grade <<'\t'<< list[i].name <<'\n';
}

If both keys are to be sorted the same way, you can concatenate them into a string and then compare the string:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

struct Student {
  std::string name;
  int avg_grade;
};

bool multikey_compare ( const Student& a, const Student& b )
{
  std::ostringstream aout;
  std::ostringstream bout;

  aout<< a.avg_grade << a.name;
  bout<< b.avg_grade << b.name;

  return aout.str() > bout.str();
}

int main()
{
  Student list[] = {
    {"Joe Blow", 80},
    {"Jane Doe", 80},
    {"Fonzie", 60},
    {"The Dude", 70},
    {"Some Guy", 80},
    {"Z Man", 90},
    {"John Doe", 90},
    {"A Man", 70}
  };

  std::sort ( list, list + 8, …
Narue 5,707 Bad Cop Team Colleague

>can you please give me an article address which tells when to use stack and when to use heap
Oh, if only it were as simple as "use the stack here and use the heap here". :icon_rolleyes:. The problem is that often it's not easy to decide. Generally, a good guideline is to use the stack when you can and the heap when you must, because doling out heap memory is a relatively expensive task.

Examples of when you have no choice are large objects that won't fit on the stack, dynamic data structures whose size you can't predict, and when the scope of your object doesn't mesh with how the stack works. As with everything in programming, knowing when to use something comes down to experience trying it both ways.

>how to use them properly
As long as you remember to free your heap memory, there aren't really any special rules for using either stack or heap memory "properly".

>and the performance issues related to usage
The only performance issue is that requesting memory from the heap manager is vastly slower than the stack. Beyond that you pretty much only have copying issues to consider, but that has more to do with passing data around in with pointers vs. passing by value, which isn't really a stack vs. heap issue.

Narue 5,707 Bad Cop Team Colleague

char *argv[] and char **argv represent the same type, it's just two ways to say the same thing. Think about how char a[] turns into char *a and apply the same logic.

Narue 5,707 Bad Cop Team Colleague

>can you be more specific, about which language you
>are talking about as the word "language" is broad
Specifics are irrelevant. The question is about programming language design/implementation in general, and being more specific won't change the answer.

Narue 5,707 Bad Cop Team Colleague

I get correct results with the following code. The only changes were the ones you claim to have made:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

/* Example.txt
23
test
4.5
*/

int main(int argc, char *argv[])
{
	string Filename = argv[1];
	cout << "Filename: " << Filename << endl;

	int Integer;
	string String;	
	double Double;

	ifstream fin(Filename.c_str());

	string line;
	stringstream linestream;
		
	getline(fin, line);
	linestream << line;
	linestream >> Integer;	

	getline(fin, line);
	linestream.clear();
	linestream << line;
	linestream >> String;

	getline(fin, line);
	linestream.clear();
	linestream << line;
	linestream >> Double;

	fin.close();

	cout << "Integer: " << Integer << endl;
	cout << "String: " << String << endl;
	cout << "Double: " << Double << endl;
	
	return 0;
}
Narue 5,707 Bad Cop Team Colleague

A stringstream is still a stream. When you read to end-of-file, you need to clear the state before you can do anything substantial with the stream again:

linestream.clear();
linestream << line;
...
Narue 5,707 Bad Cop Team Colleague

The first compiler for a language is written using another language, and often the first real test of a new language's usability and power is to bootstrap itself by writing the second compiler in the language itself.

A common question is "how was the first compiler created if there were no other compilers to use?", and the answer is that it was written directly in machine code without the aid of a higher level language.

Narue 5,707 Bad Cop Team Colleague
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ( void )
{
  int r;

  srand ( (unsigned)time ( NULL ) );
  r = rand();

  printf ( "random number = %d\n", r );

  return 0;
}