Strange Number Conversion C++ - c++

So I have the following code:
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{
array<long, 3> test_vars = { 121, 319225, 15241383936 };
for (long test_var : test_vars) {
cout << test_var << endl;
}
}
In Visual Studio I get this output:
121
319225
-1938485248
The same code executed on the website cpp.sh gave the following output:
121
319225
15241383936
I expect the output to be like the one from cpp.sh. I don't understand the output from Visual Studio. It's probably something simple; but I'd appreciate it nonetheless if someone could tell me what's wrong. It's has become a real source of annoyance to me.

The MSVC uses a 4Byte long. The C++ standard only guarantees long to be at least as large as int. Therefore the max number representable by a signed long is 2.147.483.647. What you input is too large to hold by the long and you will have to use a larger datatype with at least 64bit.
The other compiler used a 64bit wide long which is the reason why it worked there.
You could use int64_t which is defined in cstdint header. Which would guarantee the 64bit size of the signed int.
Your program would read:
#include <cstdint>
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<int64_t, 3> test_vars = { 121, 319225, 15241383936 };
for (int64_t test_var : test_vars) {
cout << test_var << endl;
}
}

Related

how do i fix this no matching function for call to 'stoi(int&)'|

i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);

How does this compile if std::pow is not allowed to be constexpr for compatibility with setting things like errno?

The following C++ snippet runs and compiles fine on, for example, the below platform (not sure which compiler is used under the hood). But aren't array sizes required to be known at compile time?
https://onlinegdb.com/H1qovLHGV
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int foo[(int) std::pow(3, 5)];
foo[2] = 0;
cout<<"Hello World " << foo[2] << endl;
return 0;
}

Give example of bitset as a member of structure in C++

I don't know how to use bitset as a member of structure.As I am getting this
[ERROR]: ISO C++ forbids declaration of 'bitset' with no type
code:
typedef struct
{
bitset<10> status; //bitwise status
}Status;
It's often considered courteous on Stack Overflow to give more examples of what you're tried, and where you've looked for help. For example you might say that you're tried to understand the contents of http://en.cppreference.com/w/cpp/utility/bitset
But here goes:
#include <iostream>
#include <bitset> // you'll need to include this
struct status_t {
std::bitset<11> status; // note the std - it's in that namespace
};
int main()
{
status_t stat;
for (auto i = 0; i < 11 ; i += 2)
stat.status.set(i);
std::cout << "values: " << stat.status << "!\n";
}
You can see it run at cpp.sh - Bitset example
This sort of error can be caused by either omitting the bitset include, or failing to specify the std namespace.
To rectify the problem:
1) Make sure you're including bitset:
#include <bitset>
2) Make sure the std namespace is specified:
This can be done either 'globally' within the file using the directive:
using namespace std;
or by prefixing the bitset declaration with std:
std::bitset<10> status; //bitwise status
So, your final file fragment could look something like this:
#include <bitset>
// other code ...
typedef struct {
std::bitset<10> status; // bitwise status
}Status;
// the rest of the file ...

strtoull was not declared in this scope while converting?

I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.

ostream::write not writing entire struct?

I'm trying to export various values, such as ints and simple structs, to a binary file. Here's some code:
#include &ltiostream&gt
#include &ltfstream&gt
#include &ltcstdint&gt
using namespace std;
template&ltclass T&gt void writeToStream(ostream& o, T& val)
{
o.write((char*)&val, sizeof(T));
cout &lt&lt o.tellp() &lt&lt endl; //always outputs 4
}
struct foo {
uint16_t a, b;
};
int main()
{
foo myFoo = {42, 42};
ofstream test("test.txt", ios::binary);
writeToStream(test, myFoo);
test.close();
}
The program should generate an output file 4 bytes long. But when I open it, it's only 2 bytes long. If I change myFoo.a and myFoo.b to contain values of 256 or more (requires more than 1 byte to store), then the file becomes 4 bytes long. I'm using the Visual Studio 11 Developer Preview on Win7; I haven't checked to see if the same happens on other systems or compilers. How can I make it output correctly for values of a or b under 256?
A file can only be read back by a program that understands the format in which it was stored. Notepad++ has no understanding of the format in which your file was stored, so it has no ability to read it back and render it sensibly. Either write the file in a format Notepad++ understands, such as ASCII text, or only read the file with a program that understand the format you wrote it in.
I have cleaned up your code as follows. Though I do not know why the old code output two bytes, the new code does output four.
#include <iostream>
#include <fstream>
#include <cstdint>
using std::cout;
using std::endl;
using std::uint16_t;
using std::ostream;
using std::ofstream;
using std::ios;
template <class T> void writeToStream(ostream& o, T& val)
{
o.write(reinterpret_cast<char *>(&val), sizeof(T));
cout << o.tellp() << endl; //always outputs 4
}
struct foo {
uint16_t a, b;
};
int main()
{
foo myFoo = {42, 42};
ofstream test("test.txt", ios::binary);
writeToStream(test, myFoo);
// Just let the stream "test" pass out of scope.
// It closes automatically.
//test.close();
return 0;
}
(My standard library lacks cstdint, so I used short rather than uint16_t, but I doubt that this matters.)
The std::ofstream type is derived from std::ostream. The writeToStream() function is happier, or at least more regular and more general, if passed a plain std::ostream. Also, for information: to issue using namespace std; is almost never recommended in C++.
Good luck.