Correct way to use cin.fail()

cin.fail() returns true if the last cin command failed, and false otherwise. An example: Now suppose you have a text file – input.txt and it’s contents are: When you will run above short program on that, it will result like: it will not continue after 6th value as it quits after reading the seventh word, because that … Read more

What’s the difference between char and char* in C++?

The variables with the * are pointers. A ‘normal’ variable, for example a char or an int, contains the value of that datatype itself – the variable can hold a character, or an integer. A pointer is a special kind of variable; it doesn’t hold the value itself, it contains the address of a value in memory. … Read more

Does C++ have a Garbage Collector?

Native C++ by default has no such thing (the closest thing to this are the smart pointers, but that’s still something entirely different), but that doesn’t prevent you from writing your own garbage collection solution (or using third party solution). Managed C++ (and its successor C++/CLI) of course use .NET garbage collection for managed resources (though … Read more

cin >> “no operator matches these operands”

I’ve been working on a C++ project in visual studio 2012 console mode and I keep getting this strange persistent error with the cin function. Under the >> I get a red line and the program tells me no operator matches these operands. I have initialized all the array elements in a separate method. Here’s a snippet example (The actual … Read more

Correct way of looping through C++ arrays

In C/C++ sizeof. always gives the number of bytes in the entire object, and arrays are treated as one object. Note: sizeof a pointer–to the first element of an array or to a single object–gives the size of the pointer, not the object(s) pointed to. Either way, sizeof does not give the number of elements in the array (its length). To get the … Read more

Sorting Characters Of A C++ String

There is a sorting algorithm in the standard library, in the header <algorithm>. It sorts inplace, so if you do the following, your original word will become sorted. If you don’t want to lose the original, make a copy first.