istream and ostream problem – C++

Make sure you include fstream. Also, put “std::” before ostream or put “using namespace std” somewhere. It would help if you posted the code, as right now I’m just guessing based on common mistakes. I would guess you forgot to include fstream because different compilers may use different header files and it may be the … Read more

How to implement Java “Scanner” in C++?

You seem to be using Scanner to read one integer at a time from the standard input stream. This is easily accomplished with the extraction operator, operator>>. Replace this code: With this: You should check the value of std::cin after the >> operations to ensure that they succeeded. Refs: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

C++ – Print Out Objects From Set

In C++11, why use a for loop when you can use a foreach loop? In C++98/03, why use a for loop when you can use an algorithm instead? Note that this works with any pair of iterators, not only those from std::set<t>. std::copy will use your user-defined operator<< to print out every item inside the set using this single statement.

Does static constexpr variable inside a function make sense?

The short answer is that not only is static useful, it is pretty well always going to be desired. First, note that static and constexpr are completely independent of each other. static defines the object’s lifetime during execution; constexpr specifies that the object should be available during compilation. Compilation and execution are disjoint and discontiguous, both in time and space. So once the program is … Read more

cmath vs math.h (And similar c-prefixed vs .h extension headers)

I’ve seen some information about differences between things like iostream vs iostream.h. [iostream.h] is not a standard header. it is not an example of the issue you’re raising. [cmath] defines symbols in the std namespace, and may also define symbols in the global namespace. [math.h] defines symbols in the global namespace, and may also define symbols in … Read more

What is a `char*`?

It is a pointer to a char. When declaring a pointer, the asterisk goes after the type and before the identifier, with whitespace being insignificant. These all declare char pointers: To make things even more confusing, when declaring multiple variables at once, the asterisk only applies to a single identifier (on its right). E.g.: It is primarily for … Read more

Converting string to unsigned int returns the wrong result

You should instead use std::strtoul, found in <cstdlib>, which is designed for unsigned numbers, has a larger range, and reports errors better. If you want to use std::string for input and exceptions for error handling, use std::stoul. A short, highly efficient implementation would be as follows: This will be much faster than istringstream, culture-invariant (so no unexpected changes to behavior when … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)