deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I like them both, actually.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

C# or VB.NET are both freely available. There are free compilers for C++ and Java as well. Those are among the most popular languages.

Mya:) commented: Thanks for fast reply! +2
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As you point out the struct would get passed as a value, and if the struct got bigger so the passing would become more heavy.

This is true, but there's also the question of how heavy the object is and how often it's copied around. If it's not that heavy relative to a reference or you don't pass it often, the performance difference will be negligible.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's really the difference between a value type (struct) and reference type (class). For types that are suitable to both, I tend to prefer reference types for consistency. But that's more of a stylistic choice than anything.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's the other way. Reputation adds a vote, but votes don't add reputation. Endorsements are a separate feature entirely, where people endorse you (hopefully) when you show expertise in a certain forum.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The first thing I notice is that the following accomplishes nothing (unless by "nothing" you wanted an infinite loop):

} while(*str != '\n'); // if empty str inputed -> stop do-while

You're using the >> operator to populate str, and that operator is delimited by whitespace. In other words, '\n' (which constitutes whitespace) will never be stored in str.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Don't use eof() as a loop control because it introduces a fencepost error. eof() returns true only after you try and fail to read from the stream. Instead, the >> operator will return a value that can be used as the condition without any operation ordering issues:

while (openfile >> number)
{
    cout << number << end;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

While swapping if overflow occurs how to manage it....?

Use a temporary variable of the same type, no overflow will occur in that situation. If you try using the arithmetic tricks, overflow is a risk and greatly increases the complexity of the swap for the negligible benefit of a few bytes saved memory. If you try using the XOR swap, swapping with self is a risk and slightly increases the complexity of the swap. XOR also demands that the swapped type be integral.

These tricks put undue restrictions on you, and the savings aren't worth it if you're writing a general purpose swap. So the answer is to just bite the bullet and use a temporary variable; they're not that expensive.

Don't try to be clever, write code that works.

ddanbe commented: Good point! +15
Mayukh_1 commented: That's why using temp variable is always safest..good point +1
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The second is more correct. When you say new[], you should use delete[], when you say new, you should use delete. They can't be mixed and matched.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

my problem: is that the values are showing up in the file after excution

What behavior were you expecting?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your interface methods don't match the implemented methods of the same name. The implementations have three arguments, the interface states that those methods should take no arguments. Therefore, these are unrelated methods to those declared in the interface.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The method belongs to the class and not any single instance of the class.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

See my original post -- it doesn't likeImports System.Numerics

Imports only opens up the namespace for you so that you don't need a fully qualified name every time. You still need to reference the assemby, otherwise the namespace simply doesn't exist in the project.

And as you can see from my second post .NET 4.5 is installed, which meets the requiremenet of 4.0 or higher.

Installed and targeted are two completely different things. I have 4.5 installed, but if I only target 2.0, I can't use anything in the 4.5 framework. Look at your project properties.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The target framework for the project needs to be 4.0 or higher. With Visual Studio 2013, unless you changed the framework when creating the project, it'll be 4.5. Also, be sure to reference System.Numerics.dll in your project as well, since it's not a default reference.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Assuming the code is a snippet and not complete, there are only two direct errors (braces instead of brackets on line 6 and endline instead of endl on line 12).

If there are supposed to be 10 errors, you can't make the assumption that headers were included properly, namespace std was opened, that the functions exist, or that the functions are called correctly.

If that's all the code you were given, that should give you a hint. Otherwise, please post the full code.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

num3 isn't a float, try using the correct specifier in printf.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

File operations are inherently slow, relative to in-memory operations. I'd probably go with writing smaller blocks than dumping all of the file contents to a line for a single WriteAllText call. The framework can handle its own buffering for performance and you can limit the amount of string processing you're doing in memory as well as how much memory you're using. A big hit to performance often involves crossing cache lines, which large strings are wont to do.

The absolute fastest you can get would probably force you to drop down to WriteFile using PInvoke, but WriteAllText is one of the fastest options from the framework from benchmarks I've seen.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I wonder if that rogue semicolon one line 1 has anything to do with it. ;) My recommendation in general is to always use braces around a compound statement to avoid this and the next related error you're sure to encounter just by deleting that semicolon.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Iterators were designed based off of pointers, so a pointer is a form of iterator, but an iterator isn't necessarily a pointer.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I don't see people use const this way, adding const everywhere they can, let alone const pointers. Is there a reason for it?

Probably best to ask the author of such code their reasoning. I can think of a few reasons ranging from not understanding best practice, to encountering errors when adding const and then removing it rather than fixing the errors, to outdated programming habits.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

maybe @Dani could add a 'Please Mark as Solved' button that users could click to notify the owner of the thread that they should mark it as solved.

The chance of that notification reaching the thread starter doesn't justify adding that feature. As mentioned, the majority of threads are one-off where the one who started it never returns. If you feel a thread should be marked as solved, any moderator can do it. Just flag the first post to let us know.

However, note that to be solved doesn't mean the right answer was given, but that the creator of the thread clearly showed that his or her question was answered. That's one reason why moderators don't mark threads as solved all willy nilly just because we see the right answer in one of the replies.

Further, there's an argument in favor of not cleaning up unsolved threads, despite the feature being available. If the thread is solved, helpful people who have something to add may be less likely to respond. It's a tricky balance between notifying readers that the thread has an accepted answer and encouraging helpers to continue the discussion in a constructive manner.

I don't disagree with your feature suggestion, but I like to play devil's advocate. ;)

Doogledude123 commented: Definitely some good points here. +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Another potential issue with hardware changes is you may need to reactivate Windows. Typically this only happens with significant changes like a motherboard replacement, but it's not unheard of for minor hardware changes or even non-hardware changes to trigger a reactivation notice.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I answered to his request, or should I say, solved his problem, right there...

I'd wager that's precicely why you got downvoted. Solving homework problems for people without any effort on their part is generally considered to be unhelpful because there's no practical learning involved.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Works for me, once I fix the error of trying to define an array with a non-const size (my compiler doesn't support C99+).

Since I can't reproduce the stated problem, you'll need to follow debugging practices such as tracing through the code in a debugger and watching the size that's calculated. Presumably the issue is in StrLen, and that's where I'd start after confirming exactly where 6 originates in your main function.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A static data member belongs to the class rather than to an individual instance of the class. Likewise with a static member function. A reasonable way to think about them at a high level is to treat the class as acting like a namespace for those members:

class Foo
{
public:
    static int bar;
    static void baz();
};

...

int main()
{
    cout << Foo::bar << '\n';
    Foo::baz();
}

Comparable in many cases, but not identical is the namespace concept:

namespace Foo
{
    int bar;
    void baz();
}

int main()
{
    cout << Foo::bar << '\n';
    Foo::baz();
}

In both cases you can use the name of the class or namespace as a qualifier to access the member. With classes you can also access static members through an object:

int main()
{
    Foo obj;

    cout << obj.bar << '\n';
    obj.baz();
}

Under the hood, the Foo class can be imagined something like this:

int static_cls_Foo_public_bar;
void static_cls_Foo_public_baz();

class Foo {};

bar and baz are actually closer to global objects that are internally checked for correct visibility and access by the compiler. The objects themselves are not baked into an instance, but still tied closely to the class.

When would you use a static member? Well, whenever you need an object or function that applies to all instances or to the class as a whole. One good example is reference counting. If you're counting the number of instances, then each instance having an …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You've encountered a common beginner issue. Allow me to point it out more clearly:

// This is a pointer to a string literal. You can't modify it.
char *text = "GOOOD TEXT ALL TIME VERY POOR ";

// This is an uninitialized pointer, you can't read from or write to
// it until it points to a valid block of memory that you own.
char *copy_text;

// This is undefined behavior because you're trying to write
// to an uninitialized pointer. If you're lucky, the program
// will crash fantastically so that the error is obvious, but
// there's no guarantee of that.
StrCpy( copy_text, text );

Now let's fix it:

char *text = "GOOOD TEXT ALL TIME VERY POOR ";
char copy_text[50];
StrCpy( copy_text, text );

In the above correction, copy_text is an array. This means you own the memory, and it's large enough to hold all of the characters in text.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

My recommendation would be to use the string class:

string names[] = {"monitor", "cpu", "mouse", "joystick", "system"};

The problem with your code is that the "strings" aren't string literals, so they're treated as identifiers that don't exist. Further, the type of your array is char, which means each element of the array can hold one character and no more.

To hold multiple characters, the type of the array needs to be a char* that points to a string literal or dynamic memory, an array of char (ie. a 2D array), or an array of std::string as in my example above. The latter provides the most flexibility by far, and that's why it's recommended.

dennis.ritchie commented: thank you it worked perfectly +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm not aware of any standard controls that will do this for you. You'd essentially need to combine a textbox and some form of list control, where a button or the Enter key on the textbox fires a back-end search. It should go without saying that you'd be writing the search logic.

You can wrap the whole thing up in a user control, but the initial build is more than just dropping a control and setting a few properties.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
public static class Globals
{
    public static readonly string Foo = "DAFOO";
}

...

Console.WriteLine(Globals.Foo);
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

On a side note, it's harder to read posts and titles in all caps.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What exactly are you trying to accomplish? "Lookup search control" is too vague.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

An array is a collection of items. To access individual items you need to specify which index in the collection you want (ranging from 0 to N-1):

for (int i = 0; i < months30.Length; i++)
{
    if (months30[i] == month) // Now you can test against the entered month
    {
        Console.WriteLine("{0} has 30 days", month);
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The first error that I immediately noticed is you're not initializing your variables to 0. This means that they'll have some random value, and incrementing that value (on top of being undefined behavior) will accomplish nothing meaningful.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The closest you can get would be a static class in C# with those variables as members. Place that class in a shared assembly (if your project uses more than one assembly) and you're good to go.

GagaCode commented: thaks +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I can't set it in the designer too, because that code can/will be regenerated.

If the class is obsolete, why would you be changing it such that the generated code is updated? Last I checked, generated code was only updated when you changed the form from the designer.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It seems like there's some confusion with the overloading of the & operator. In the context of a type, it means "reference to", not "address of".

More details

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why would you want its address? That's meaningless to the caller of the method. Anyway, you can get the address on the caller's side using the updated code from my previous post:

cout << "Reference of last element in stack: " << &P.top() << endl;
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That makes much more sense, a realistic example is far more helpful than a broken example. You simply need to remove the address-of operator and returning a reference will work in this case because the array has the same lifetime as the stack object:

int& Stack::top()
{
    return container[size-1];
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I know it can be solved if function is defined as "int* example()"

The error will go away, but the underlying problem will remain. You're referring to an object that no longer exists. This is the dangling pointer or dangling reference problem.

If int a is global constant, how to do it then?

The real question is what do you want that function to accomplish? Because right now it's completely nonsensical.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

&a evaluates to a pointer to int rather than int, therefore a reference to int is an incompatible type. Further, returning a reference to a non-static local variable is an extremely bad idea because the local variable is destroyed after the function returns; the reference will refer to a non-existent object.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Two years ago I'd say no way in hell. These days I'd say that appropriately designed tablets can kill the netbook market, though not the laptop market due to simple scalability limits. My current tablet (ASUS T-100TA) is a perfectly suitable netbook as well as a nice tablet.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It largely depends on your compiler and switches. C++11 (the latest standard) added this feature, and some compilers prior to that offered it as an extension.

Your book is old, by the way, and also written by an author who's routinely mocked for making grievous beginner mistakes. You might consider getting a different one.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Usually you specify what exception type(s) it may throw.

Noting of course that exception specifications have been deprecated because they're stupid. Support for exceptions themselves is still standardized and unlikely to change significantly in the forseeable future.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can not test two floats or doubles for equality due to possible rounding errors in memory variable.

You can, but it gets hairy very quickly.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What other languages support is irrelevant, this is C. While I can understand the confusion, that doesn't change the importance of making it clear what's standard C and what's not.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think it is possible to do that, only that, the nested function is only been seen within the function in which they are defined.

Not in standard C. If nested functions works for you then you're relying on a compiler extension.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'd actually recommend against learning your first two languages simultaneously because you're also learning programming in general at that time. Too much information, some of it guaranteed to be conflicting due to varying syntax rules, will prove to be confusing and slow your progress.

So if you're happy with Python so far, keep learning just Python until you feel sufficiently learned or have need for a different language. That's my advice.

Once you reach that point, unless you need something different, of course, my suggestion would be Java, C#, or VB.NET.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Okay, so let's examine why Python isn't doing it for you. Since you seem unsatisfied, why is that?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

By second programming language do you mean a language you know but don't really use? It seems kind of silly to think in terms of first, second, third, etc...; you should learn as many languages as practical to benefit your skillset.

So let's start with this: what language do you already know?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but can an abstract class be instantiated ?

No. That's the point, you're forced to derive from an abstract class.