grumpier 149 Posting Whiz in Training

break statements can be used in looping constructs, and in switch/case blocks. They cannot be used to break out of an "if" block, or as an alternative means of returning from a function.

Also, <iostream.h> is non-standard. #include <iostream> instead.

grumpier 149 Posting Whiz in Training

::close() just means the function close() is within the global (unnamed) namespace.

In C++, if a function is not explicitly declared with a namespace, it is placed in the global (unnamed) namespace. That's what happens, by default, with functions from C standard headers (<stdio.h>, etc) and all third-party library functions that can be accessed from C.

If you declare a function at file scope, and don't explicitly place it in a namespace, it will also be within the unnamed global namespace. So, a function declared at file scope.

void f();

has a fully qualified name of ::f().

grumpier 149 Posting Whiz in Training

You're using the function; presumably you can work out which header it came from. close() is not a standard function in C or C++.

I'm guessing it is a function related to sockets handling under win32 or unix (typically in a header like <io.h>, but that varies). If my guess is correct, it may be unambiguously called using "::close(sockFd);"

If my guess is wrong (eg it's a macro) then your only choice would be to rename your MyClass::close() function .... for example to MyClass::finish().

grumpier 149 Posting Whiz in Training

I'll wager a bet that he means "The most efficient" means by which to add a value to all items in an array.

I'd be betting on your side. But that just means he then needs to define the criterion for "The most efficient". For example, does it mean "fastest", "smallest number of machine instructions", "least memory consumption", "least heat output from the CPU" :)

He probably also needs to specify the type of array elements. Adding a value to elements of an array of strings might conceivably require a technique that differs from adding a value to elements of an array of int.

Incidentally, I despise irrelevant questions that put carts before horses. ;)

grumpier 149 Posting Whiz in Training

You're developing a realtime application, and don't know how to add values to elements of an array?????

Pull my other leg: it'll play a tune for you.

What is the criterion by which you would describe a method as "best"? If you are truly designing a realtime application, you will realise the significance of that question.

Salem commented: Jingle bells, jingle bells, jingle all the way? +26
grumpier 149 Posting Whiz in Training
switch (choice) 
{
 case 'p' || 'P': // do this
 break;
case 's' || 'S': // do that
}

won't work

Indeed. 'p' || 'P' would expand to a bool value of true, as will 's' || 'S'. Compilers will generally refuse to compile the above, as particular cases of a switch statement are not allowed to be duplicated.

Try this instead;

switch (choice) 
{
 case 'p':
 case 'P': // do this
 break;
case 's':
case 'S': // do that
  break;
}
grumpier 149 Posting Whiz in Training

Mathematical operations: operators like * (multiplication), + (addition), - (subtraction or negation), / (division), etc.

Splitting up large number and assigning them to an array of ints is one way: such things can be implemented using a class.

grumpier 149 Posting Whiz in Training

You can implement a class type that has the attributes you want (or find such a class type that someone else has implemented). Internally, that type might use an array of integers or (as Freaky_Chris suggested) a string to represent larger values. You would need to define mathematical operations and also things like streaming (so you can read or write the data to a file stream).

grumpier 149 Posting Whiz in Training

If n is 1, then the set is {0, 1}.

If you have a set for n, the set for n+1 can be generated by appending a 0 and then a 1 to all elements of the set n.

For example, the set for n = 2 will be 0 + {0, 1} and 1 + {0, 1} which becomes {00, 01} and {10, 11} or (more simply) {00, 01, 10, 11}.

An easy method to implement that would involve recursion.

grumpier 149 Posting Whiz in Training

can i just change those values? either just by editing limits.h or by changing the value for a specific program? or can i make a variable of unlimmited size?

Generally, you cannot. The contents of limits.h generally reflects technical limits on capability of your compiler on your target operating system. Most compilers will need to be rewritten/extended/modified to support a change of those values.

grumpier 149 Posting Whiz in Training

An explicit conversion is what you're calling a cast. An implicit conversion is a conversion between types that the compiler allows or performs, even if you don't do a cast. For example;

int i = 3;
   long j = i;

The initialisation of j involves an implicit conversion of an int to a long. The compiler knows how to do that, so it happens implicitly (as far as the programmer is concerned).

Conversion between basic types are built in: for example, the compiler knows how to add an int to a double.

Class types support conversions in two ways: by the constructors they supply, and by operator functions.

class X
{
    public:
       X(const char *);
};

allows a const char pointer to be converted to an X. Given a function;

void func(const X &);

the call

func("Hello");

will create a temporary X by passing "Hello" to its constructor, and then pass that temporary to func(). That process of creating the temporary X from "Hello" is a conversion.

The other way is that;

class Y
{
     public:

         operator const char *() const;  // conversion to const char *
};

declares an operator that allows a Y to be converted to a const char *. So, given;

void another_func(const char *);

it is possible to do this;

Y y;   / assume we have a default constructor
    another_func(y);

as the compiler will implicitly invoke the operator const char *() for the object y, …

grumpier 149 Posting Whiz in Training

Should I avoid casting between data types?

As a general rule, yes you should.

I haven't really seen much about the use of this, but it seems like one of those last-minute-no-way-around kind of methods... but, for example, how would I use the length of a string in a for loop? The length property of a string is size_t, and if I had an integer i in a loop, how could I do that?

An implicit conversion exists between int and size_t so, in rough terms, you shouldn't need an explicit conversion between them.

Note "explicit conversion" is actually the more correct name for a "cast".

In practice, the conversion can lose information (signed to unsigned, and different variable sizes), so several compilers will emit a warning when doing that conversion. If (and I repeat if) you know that conversion is valid, you can safely ignore the warning messages. If you can't tolerate warning messages from compilers, then an explicit conversion generally has the side effect of stopping the compiler complaining. That has the disadvantage that it also stops the compiler from emitting complaints that you should really address properly.

However, if you can guarantee the conversion is safe (i.e. you know that the value of the size_t variable can be stored in an int) a technique is;

size_t end = whatever();
   for (int i = 0; i < (int)end; ++i)
      loop_body();

Where would casting be useful?

Explicit conversions are useful when you know a conversion …

grumpier 149 Posting Whiz in Training

Try this. Comments added explaining the reason for change from your code.

#include <iostream>      // <iostream> is standard, <iostream.h> is not
#include <cstring>        //   need functions to work with C strings

using namespace std;
class Dummy{
char Name[256];

public:    // for main() to use a member function, it must be public
void setName(char *nam)       // arrays are passed to function as pointers
    {strcpy(Name, nam);}   // and char arrays are copied using string functions like strcpy()
};

int main(){
char temp[256];           //   the declaration char [256] temp is invalid
cout << "Name?\n";
cin >>  temp;
Dummy some_object;   //  non-static member functions act on objects
some_object.setName(temp);  // this is how non-static member functions act on objects
}

You also need to put some effort into interpreting and understanding error messages from your compiler. Your compiler would have complained bitterly about your code and, if you'd taken time to understand them, you could worked out the changes I've commented in red.

grumpier 149 Posting Whiz in Training

Can you please paste here exact code for doing it

You have a description of one possible approach; consider coding it up as an exercise.

grumpier 149 Posting Whiz in Training

The whole point of Dijkstra's algorithm is to find a least cost (or shortest, in your case) path.

Once you have a first path, eliminate one of the nodes (C2, C5, or C10 in your example) on that path from the graph, and run Dijkstra's algorithm again. If the resultant path has a higher cost than your first one, put the node back, eliminate another node, and try again until the resultant path has same (or similar) cost to the first. Repeat this as often as necessary.

This isn't exactly inexpensive for large graphs (necessary to run Dijkstra's algorithm a number of times) and needs to be done systematically, but it will work.

grumpier 149 Posting Whiz in Training

but I didn't understand the example, if you can give me actual example plz.

I can, but I won't. As horrifying as the concept clearly is, you need to apply some effort to understand the answers you've been given.

grumpier 149 Posting Whiz in Training

You need to put some effort into understanding the answers you receive, rather than giving up if you're not given code examples.

The approach with using pointers is;

SomeTypeA Function( <arguments>,   SomeTypeB *x)
{
       *x = whatever_data_needed_other_than_return_value();
       return return_value();
}

The "SomeTypeB *x" argument is what I was referring to when I said "or the function to accept a pointer that receives additional data from the function".

grumpier 149 Posting Whiz in Training

A common way of return multiple values is by returning a struct which has multiple members, or for the function to accept a pointer that receives additional data from the function.

You don't actually need to return multiple values though: return 0 if there is no operation, '*' if multiplication is the operation, '+' if addition, etc etc. '*', '+', '-', and '/' are non-zero characters with all character sets that I know of.

grumpier 149 Posting Whiz in Training

My guess is that IMG_GetError() is throwing an exception.

However, it's hard to say, as you haven't provided a complete example. You've paraphrased where your think the problem is.

Try providing a small but complete example that illustrates your problem (with a main() function, and everything someone else would need to be able to get the same results you are).

grumpier 149 Posting Whiz in Training

Hi having void ** worked, I had to typecast the &outputbuffer to void **.

Maybe so, but that doesn't make it a good idea.

If your function assigns *outputbuffer to something to something that is not a char *, and then main() dereferences it, the result is undefined behaviour.

As you say at the end of the post, it is better to make the argument char **. Then, at least, the compiler can detect if you try to do a suspicious assignment in the function, or pass something incorrectly to it.

grumpier 149 Posting Whiz in Training

As a matter of fact, your code isn't valid C either. If you are going to ask for help in converting C code to C++, it helps if you post working C code.

You need to read this thread before posting again.

grumpier 149 Posting Whiz in Training

I might think the pythagorean therom would be better easier to use than trig.

You're making a distinction that doesn't exist. The Pythagorean Theorem is basic trigonometry.

grumpier 149 Posting Whiz in Training

The cleaning lady?

Seriously, it is necessary to assume the houses are ordered 1-5 (first to fifth) from left to right. Then a bit of persistence with eliminating anything that contradicts any statement, but it's not that hard. Basic application of Occam's razor: eliminate the impossible, and only the possible is left.

grumpier 149 Posting Whiz in Training

Firstly, there is no space between the closing bracket at the end of FOR(x,n) and preceding the rest. That's a stylistic concern, but badly hurts readability.

Second, and more significantly, typeof() is not a standard function or operator in C++. It is an extension specific to g++ (GNU c++) in which typeof(x) expands to be type of x and will trigger a compiler error with every compiler that does not support that extension. Within a for loop (assuming you're using g++), if x is an int, then FOR(x,n) would expand to for (int; x < n; x++) which is invalid code.

grumpier 149 Posting Whiz in Training

Wow, grumpier. That was total BS. :icon_rolleyes:

What can I say? I was tired. My sig also applies.

grumpier 149 Posting Whiz in Training

The first line puts a space into the first char of a, and a zero into the second char. All other elements of a are uninitialised. Hence the compiler diagnostic (which will usually be a warning, not an error).

The second line initialises the array b with a zero byte (that's what "" yields in memory). If one element of an array is initialised to zero, all elements are (i.e. the whole array is initialised)

grumpier 149 Posting Whiz in Training

I'd look in the glut header files before looking in the cstdlib. The fact there is even a macro GLUT_BUILDING_LIB to define to "stop a clash between glut and stdlib" says that the incompatibility is the glut header file and library, rather than the standard library.

Generally, suppliers of third-party libraries (like glut) are responsible for ensuring compatibility with compilers and standard libraries. Presumably, in your case, the glut library people have not ensured compatibility with your version of Visual Studio.

grumpier 149 Posting Whiz in Training

Of more relevance, try posting a small sample of your code that illustrates the problem. Also provide information about your compiler settings (eg any settings you've tweaked in the VC IDE to non-standard settings).

My guess is that you've been playing with some #define's somewhere (either before you've include'd some headers, or in the IDE settings) and they interact with the headers in strange ways.

grumpier 149 Posting Whiz in Training

love is blind

It has to be. If guys and gals actually paid attention to what the other is really like, they would run a mile. Love is therefore nature's way of bringing members of the opposite sex together so they can procreate before they ask too many questions.

grumpier 149 Posting Whiz in Training

A starting point for you would be to specify what atomic_add_int_nv() actually does under unix. Forum readers are not mind-readers, and not generally all that good at working out what arbitrarily named non-standard functions do.

If you look up the documentation for atomic_add_int_nv(), you may also be able to work out the answer for yourself.

grumpier 149 Posting Whiz in Training

A pure virtual method is one that is declared (return type, arguments, etc.) but not defined (no function body).

That's not true. A pure virtual function can be defined (i.e. it can have a function body).

Something like virtual int draw() = 0;

In C++, the "=0" in the declaration is what makes the function pure virtual. In C++, a class with one or more pure virtual function is abstract, and cannot be instantiated. The purpose is that the base class provides a defined interface, and derived classes are forced to override and supply their own versions of the pure virtual functions. In this way, derived classes implement (or extend) the interface that is specified by the abstract base class.

grumpier 149 Posting Whiz in Training

To give you a clue about what's happening, 100! is about 9.3 by 10^157. To represent that, a 525 bit integer is needed (and that increases 530 bits to represent 101!). 64 bit is nowhere near enough: an unsigned 64 bit integer can only represent a maximum value of about 18446744073709551615 (approx 1.8 by 10^19).

Salem commented: Good analysis of the underlying problem that the numbers are huge +26
grumpier 149 Posting Whiz in Training

And you can use an executable compressor like nPack, see the attachment.

You need to be careful with executable compression. Compressed executables have some characteristics associated with self-modifying code (the decompression stub unpacks code and data into memory). Because of this, they can trigger false positives from anti-virus and anti-malware scanners. Also, to make it worse, a few products out there have actually carried virus or trojan horse payloads.

They can also make some programs run very inefficiently on operating systems that work with virtual memory (eg windows, unix). On those systems, executable compression is not a good idea if multiple instances of an executable are likely to be run concurrently.

If you accept these limitations, one that I've found rather useful is UPX.

StuXYZ commented: Thanks for comment about UPX +2
grumpier 149 Posting Whiz in Training

std::cout and the streaming to it involve functions from the C++ standard library.

As to libraries to use/avoid to minimise executable size, there's no single correct answer. As Salem said, when you write a substantial program, the size of the executable is more often influenced by the code you write than the libraries you use.

Unless you have severe constraints on available disk space (which is unlikely for people writing a 5 line program) your time would be better spent worrying about getting the program functionality correct than in optimising its performance or disk usage.

Once you've got your program doing what it needs to do, you can turn your attention to optimisation concerns (executable size, runtime speed, etc etc). It is generally more productive to do such optimisations in one hit, after you've got your program working, because the real "hot spots" (the things you need to focus attention on to productively optimise) are not easy to predict without the aid of tools like profilers. One of the most effective and, unfortunately, common ways to become an unproductive programmer is to spend lots of time worrying about optimisation before you need to.

grumpier 149 Posting Whiz in Training

If the content of header A relies on content of header B, then it is generally a good idea for header A to #include header B. Examples of "relies on" are;

- header B declares a base class, and a class in header A inherits from it.
- header B defines a type (eg classes, enums, typedefs) and any function in header A accepts arguments or returns that type.

grumpier 149 Posting Whiz in Training

If by ``"c" programme'' you mean something that is compliant with one or both of the C standards, then there's only one answer: it's impossible. Your not wanting such an answer does not make it less true or correct.

If you wave a magic wand and decree that standard compliance doesn't matter, then the correct answer is whatever you (or anyone else) might define it to be. Microsoft have done this by introducing the WinMain() entry point which, for win32 applications, replaces the main() function. But that solution is specific to Microsoft operating systems and relies on specific compiler extensions that are outside the scope of the C standards.

grumpier 149 Posting Whiz in Training

The short answer is that you can't. The system() function (it's not a command) supported by win32 compilers/libraries starts up a command shell, and that command shell shows the "black command prompt window".

If you actually need a command shell (eg you're running an inbuilt shell command or executing a .BAT file), then you have no choice. However, you might be able to find a way to implement your required functionality without using a command shell.

If you want to run a third-party program (eg another win32 application) with more control than is possible with the system() command, look up the CreateProcess() function in the win32 API.

grumpier 149 Posting Whiz in Training

So, just to check, int number::num; clears a place to store data at a certain data location-just like when you declare a class variable you give all of the variables in the class a place in physical memory and an adress however the adress holds NULL.

Sorry, that statement strikes me as gibberish.

The declaration "static int num;" within the body of class number simply tells the compiler that a single integer named number::num exists somewhere in memory.

The definition "int number::num;" ensures that memory is actually allocated to hold (or represent) that single integer.

grumpier 149 Posting Whiz in Training

You need to understand the difference between registering a callback function and actually calling it.

Setting TButton's OnClick event egisters a function that will be called when the user - eventually - presses the button. It does not call the function directly.

If, when the user presses the button, you want a particular function called, then call your function within the callback function (eg in the body of OnClick1). If you register the callback (eg TestBtn->OnClick = OnClick1;), then OnClick1 callback will be called whenever a user presses TestBtn. If OnClick1 calls myfunction(parameter) ......

grumpier 149 Posting Whiz in Training

My real question now is why do I need the syntax number::num; - what does it do physically in the program.

Narue answered that already.

The "static int num;" within the body of class number declares the static member. The statement outside the class body "int number::num;" defines it.

Declaring a static member of a class means that it is known to exist, so code can reference it. Defining a static means allocating storage for it so, when a program is linked, all references in code to that static member resolve to the same object (i.e. the same area of physical memory).

grumpier 149 Posting Whiz in Training

Look up the standard C functions in the C standard library, through <stdlib.h> or (in C++, through the <cstdlib> header). srand(int) is used to initially seed the generator. rand(), when subsequently called in a loop, produces pseudorandom integers in the range 0 to RAND_MAX.

Convert those integers returned from rand() to type double, and divide by RAND_MAX to get values in the range 0 to 1.

grumpier 149 Posting Whiz in Training

Does it matter where you do this (int number::num) because I tried it out in the main and it works now but why? It doesn't seem to do anything other than maybe introduce it into a new block however you put it in the header and it still works. So what does it really do ?

It doesn't matter where you do it, as long as you obey the one-definition rule (ODR). Typical projects link together object multiple object files to create an executable, and each object file is created from a single source file. If you have two or more source files in your project, then only one of them can define number::num.

Placing the definition in your header file will violate the ODR if more than one source file in your project #include's it.

grumpier 149 Posting Whiz in Training

TButton's constructor needs to be passed a pointer to a TComponent (eg a parent form) that is responsible for managing the button.

It is also necessary to set various attributes: position, caption, visibility, the function to be called when the newly created button is created, etc etc. TButton is derived from TWinControl, so some inherited attributes probably also need to be set.

It would probably be easier to create a button on your form at design time, and have your function make that button visible as needed.

grumpier 149 Posting Whiz in Training

scizj: Look at my previous post. Although I wrote it without having seen your full code, you will find the solution in to your problem within that post.

grumpier 149 Posting Whiz in Training

In one of my classes, there is one person named James Bond, and you can noticeably see how it affects his life (not in a good way).

Naming a child James Bond is actually defendable: there were families named Bond for several generations who had a long-standing tradition of alternating children's names (a kid gets his grandfather's christian name) for several generations before Ian Fleming was even born.

The Royal Australian Navy, about 20 years ago, had three Commanders all named James Bond. Each of those gentlemen were in their 40's or 50's at the time i.e. they were born before Ian Fleming's "Casio Royale" (the book that introduced the character James Bond) was published.

I don't consider that naming children in trendy/controversial ways is child abuse, but it is irresponsible parenting if knowingly done in a way that will cause backlash to the children. Doing it as a political statement is the height of irresponsibility.

It is more an indictment on societies that they can allow (or, worse, endorse) children being made to suffer because of how they are named. I know some families who immigrated to other countries, and ran afoul of local customs because their children's names happened to translate into something unpopular within the culture of their new country.

It may also say something about the children themselves, if they do suffer when given such names. It is quite popular these days to blame others for one's problems. An assertive child will …

grumpier 149 Posting Whiz in Training

Presumably you have

#include <iostream>
using namespace std;

or something equivalent in the code before your class declaration. The #include directive is needed (if you don't have it, put it in). The using directive (if you have it) must go, and then replace "ostream" in your class declaration and implementation of the function with "std::ostream".

The reason you need #include <iostream> is to ensure that the compiler sees a definition of std::ostream.

The reason for not having the using directive and using "std::iostream" instead of "ostream" is to remove ambiguity that tends to confuse compilers.

There is also the concern that std::complex (in the standard header <complex>) is a template in the STL - which means more ambiguity if you have a "using namespace std;" in effect.

grumpier 149 Posting Whiz in Training

Hi,
Thanks, using resetiosflags worked. I was under the impression that they are mutually exclusive. Anyways, how is it working for the first one correctly?(just want to understand what is happening)

On the first loop iteration, the stream flags are set to defaults. That is not true for subsequent iterations.

grumpier 149 Posting Whiz in Training

The short answer is that ios::left and ios::right (and some others) are not mutually exclusive, but your code reflects an assumption that they are. It is possible to have both set simultaneously and, of so, it is up to the implementation to decide which one (or a combination) applies.

Try using resetiosflags() manipulators to restore the various flags to their defaults before each line of output.

grumpier 149 Posting Whiz in Training

Don't confuse efficiency with effectiveness. They are different beasts, who often do not serve the same master

Not if efficiency is defined as competence in achieving the intended effect. This is a definition that may be found in english language dictionaries. ;)

But, seriously, ArkM is right: increasing efficiency (or increasing anything for that matter) implies having a quantitative measure that depends on your objectives.

grumpier 149 Posting Whiz in Training

I have four concerns with your code, in decreasing order of importance.

1) Your input requires five integers separated by spaces. What do you expect to happen if the user enters 26 27 88 72 62? Better to load a single integer (if you're happy to be limited to only checking values that can be stored in an int) or read a string of digits without spaces and then see if you have a palindrome by using string operations.

2) The for loop construct

for(int i=0,j=4;((i>=0 && i<2) && (j<=4 && j>2 ));i++,j--)

can be simplifed considerably. With i initialised to zero, and only ever incremented, it will always be >= 0 ... no need to check that. Similarly, with j initialised to 4 and only ever decremented, you're guaranteed that it will always be <= 4.

3) If you need the value of i before incrementing to be available, then use the i++ operation. However, if you don't need to access the original value, use ++i instead. Similar comment for pre and post decrement. Your code is using post-increment and post-decrement unnecessarily. [Technically, a high-quality optimising compiler be unfussed by the difference, but there is no point in code that relies on side-effects being optimised away - and some coding standards explicitly disallow such things, as it interferes with code verification activities].

4) It is no need to have comments that state the obvious. A declaration "int arry[5];" does not need a comment that …