[Solved] Function receiving different value than passed

At least part of the problem is that you are not understanding how operators work in C++. for(i=1;i<=n,completed==0;i++){ The expression i<=n,completed==0 has the effect of evaluating i <= n, discarding the result, then evaluating completed == 0, and giving the result of that. So the end condition of the loop is essentially completed == 0. … Read more

[Solved] Template to determine the size in bytes of various variables

Solution in C++14, assuming I understood what you meant: template <class Container> constexpr auto byte_size(const Container& container) { using std::begin; using std::end; return (end(container) – begin(container)) * sizeof(container[0]); } Note that this will work even if the container is empty, since sizeof does not evaluate its operand. It won’t work for std::vector<bool> though—guess you’ll have … Read more

[Solved] C++ vector of strings segfault

As panta-rei correctly pointed out, it looks like you’re trying to contain a string of the form “string” + string form of (i) but you’re actually doing pointer arithmetic which is illogical in this case (you’re just passing a pointer incremented i from some location – who knows what’s in that memory?). In order to … Read more

[Solved] how I can compile templates c ++ 11, I can c ++ 14

Remove #include “ar.cpp” from main. You’re defining insert two times. There is no difference between the relevant rules in C++11 and C++14. You did something different between your test runs that has nothing to do with the language version. I also recommend not calling it .cpp. Conventionally, nothing you include should be called .cpp. Since … Read more

[Solved] file handling I/O c++ error

You have p as some pointer. Who is going to allocate the memory that pointer points to? In C it is almost always the responsibility of the caller to allocate any buffers before the call. If you don’t want to, then use a std::string instead. solved file handling I/O c++ error

[Solved] How to specify type of a constexpr function returning a class (without resorting to auto keyword)

The actual type is hidden as it’s local inside the function, so you can’t explicitly use it. You should however be able to use decltype as in decltype(create<int>()) v = create<int>(); I fail to see a reason to do like this though, when auto works. 1 solved How to specify type of a constexpr function … Read more

[Solved] N-Queens Algorithm using c++

The part in is_attaced in which you check the diagonals is not working. It has two basic problems, first you dont check all diagonals all rows – you just check the previous row. And, second, you are doing out of bound accesses, when you are in the first or last column. An implementation that is … Read more

[Solved] why is cin/cout slower than scanf/ printf

The speed difference is largely due to the iostream I/O functions maintaining synchronization with the C I/O functions. We can turn this off with a call to std::ios::sync_with_stdio(false); By default standard C++ streams are synchronized to the standard C stream after each input/output operation. Once synchronization is turned off, the C++ standard streams are allowed … Read more

[Solved] Dynamic Memory allocation fault

The issue is that n keeps growing, but your array does not. This code invokes undefined behavior, which thankfully caused a segfault for you: while(ch==’y’) { n++; cout<<“Enter 1 more element: “; cin>>arr[n]; cout<<“Want to enter more? “; cin>>ch; } arr has only been allocated to store n elements. Simply writing past the end will … Read more