[Solved] Project Euler 8 C++ Debug [closed]

Adding to Igor Tandetnik’s answer: There is another problem, (n[i]-‘0’)*(n[i+1]-‘0’)*(n[i+2]-‘0’)*(n[i+3]-‘0’)*(n[i+4]-‘0’)*(n[i+5]-‘0’)*(n[i+6]-‘0’)*(n[i+7]-‘0’)*(n[i+8]-‘0’)*(n[i+9]-‘0’)*(n[i+10]-‘0’)*(n[i+11]-‘0’)*(n[i+12]-‘0’) is calculated as an int and therefore it will overflow for example at i == 88. You have to cast the first value to unsigned long long to have the whole calculation as unsigned long long. #include <iostream> #include <string> using namespace std; int main(){ … Read more

[Solved] C++ : Why it dont work?

as the other answer, initialize k, move if outside of loop vector<int> arr(n); int k = 0; for(int arr_i = 0;arr_i < n;arr_i++){ cin >> arr[arr_i]; k = k + arr[arr_i]; //cout << “arr = ” << arr[arr_i] << ” k ” << k << endl; // [0] } cout << k; as your question, … Read more

[Solved] This code is not compiling c++

The keyword constexpr was introduced in C++11 and improved in C++14. It means constant expression. Like const, it can be applied to variables so that a compiler error will be raised if any code attempts to modify the value. Unlike const, constexpr can also be applied to functions and class constructors. constexpr indicates that the … Read more

[Solved] This code is not compiling c++

Introduction If you are having trouble getting your C++ code to compile, you have come to the right place. In this article, we will discuss some of the common causes of compilation errors and how to fix them. We will also provide some tips and tricks to help you debug your code and get it … Read more

[Solved] Error while passing vector to instance of object in main

You are passing the wrong types to your constructor: “Jack” is of type const char[5] and the second argument 28.2 is of type double Your constructor though is expecting a std::vector<string> and a std::vector<double>. So the problem is your constructor is expecting a “list” of strings and doubles, what you are not giving him. Considering … Read more

[Solved] Cast pointer to reference pointer

This will do what you think you want, but it’s totally NOT recommended. Templates (or std::swap) really are the right answer here. First, define an inline function to take void ** inline void SWAP_POINTERS2(void** p1,void** p2) { void* temp = *p1; *p1 = *p2; *p2 = temp; } Then, define a macro to perform unpleasant … Read more

[Solved] C++ How to remove 0 values from array without using vector

I still prefer using std::vector although this question mentions “without using vector”. But let’s just try doing so with array anyway. int int_array[20] = {/*…*/}; int* last_ptr = std::remove(std::begin(int_array), std::end(int_array), 0); for (int* it = int_array ; it != last_ptr ; ++it) cout << *it << endl; As convention, the resulting last_ptr points to the … Read more

[Solved] std::map crashes when doing m[0] [duplicate]

Unfortunately (thanks C!) it is “possible” to construct a std::string from the integer 0, because it counts as a null pointer literal. However, it’s not really possible: Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s. The length of the string is determined by the … Read more

[Solved] Does using the iterator member function “empty()” only work on certain vector types?

empty() is not “an iterator member function”. You are not calling empty() on the iterator – you are dereferencing the iterator, and calling empty() on the vector element that the iterator refers to. It might be clearer if you replace iter->empty() with an equivalent form (*iter).empty() In still other words, auto iter = vec.begin(); iter->empty(); … Read more

[Solved] fstream read behavior upon hitting eof

From the documentation: If the input sequence runs out of characters to extract (i.e., the end-of-file is reached) before n characters have been successfully read, the array pointed to by s contains all the characters read until that point, and both the eofbit and failbit flags are set for the stream. … The number of … Read more