Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to create a little game in c++, and therefore I need a function to return random numbers in a specific range.
Most of the answers I found were similar to this one https://stackoverflow.com/a/19728404/5780938, and I think this is the solution I'm looking for.
To test if the function does, what I want it to, I tried outputting the results in several different ways.
At the moment my code looks like this:
#include "stdafx.h"
#include <iostream>
#include <random>
int zufälligeZahl();
int main()
{
using std::cin;
using std::cout;
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
return 0;
}
int zufälligeZahl()
{
std::random_device rd;
std::mt19937 zGenerator(rd());
std::uniform_int_distribution<int> uni(1, 13);
int random_integer = uni(zGenerator);
return random_integer;
}
I've tried this in many different ways, but no matter what I do, it doesn't work. Either the output is something like 00A8106E, or I don't get any output at all.
I'm using Visual Studio Community 2015.
You are not calling the function zufälligeZahl, you are printing out the address of the function.
Fix your code by actually calling the function:
cout << zufälligeZahl() << "\n";
You forgot the parentheses.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm playing around with the Sleep() function in C++ right now, and I am not understanding why this code operates the way it does.
I made a program to find the difference between two times, but it is not working as expected.
#include <iostream>
#include <chrono>
#include <Windows.h>
using namespace std;
int main() {
int timeA = (int)chrono::system_clock::now;
cout << timeA << "\n";
for (int i = 0; i < 5; i++) Sleep(1000);
int timeB = (int)chrono::system_clock::now;
cout << timeB << "\n";
int timeDifference = timeB - timeA;
cout << timeDifference;
cin.get();
return 0;
}
It seems as if the program is setting the variables at the same time, and then sleeping. Is this the case? If so, help me to understand why, please.
now is a function, not an attribute or variable. You failed to call it, and are casting the function pointer itself to int, which will always produce the same value for a given run (on a typical 64 bit system, the low 32 bits of the address where now is located).
Change both lines to use chrono::system_clock::now(), not chrono::system_clock::now.
Note that this is one of the reasons to avoid C-style casts, as well as a reason to compile with warnings turned up; it protects you from casting to wildly incorrect end results, without at least some sort of alert.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Im just starting to learn C++. While writing a simple calculator, I found that when calling a void function, it doesn't print when it should. I have simplified the code to better represent my problem.
#include <iostream>
using namespace std;
void helloguys()
{
cout << "test";
}
int main()
{
cout << "This is a ";
void helloguys();
cout << " guys.";
}
I expected to get "This is a test guys.", but all I got is "This is a guys."
The compiler never reported any kind of problems.
Try removing void before your function call:
int main()
{
cout << "This is a ";
helloguys();
cout << " guys.";
}
helloguys(); will call the function.
void helloguys(); is a function prototype, not a call.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Consider the following code written in C++11:
#include <iostream>
#include <bitset>
#include <cstdint>
int main() {
std::uint64_t a = 0000000000000000000000000000000000000000000000001111111100000000;
std::bitset<64> b(a);
std::cout << b << std::endl;
return 0;
}
The output of the code is :
0000000000000000001001001001001001001001000000000000000000000000
Why this output does not correspond to the a value?
If you want to write a binary number you need to use the 0b prefix.
std::uint64_t a = 0b0000000000000000000000000000000000000000000000001111111100000000;
std::bitset<64> b(a);
Your example fixed and working live
As mentioned your "binary" string is actually an octal representation of a much larger number. Alternative methods would be converting from a string, from the decimal representation or prefixing the string with '0b' to denote that a binary representation follows
#include <iostream>
#include <bitset>
#include <cstdint>
int main() {
std::bitset<64> foo (std::string("0000000000000000000000000000000000000000000000001111111100000000"));
std::uint64_t bar = foo.to_ulong();
std::uint64_t beef = 0b0000000000000000000000000000000000000000000000001111111100000000;
std::bitset<64> dead (beef);
std::cout << foo << std::endl;
std::cout << bar << std::endl;
std::cout << dead << std::endl;
std::cout << beef << std::endl;
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm newbie in C++ and I'm trying to evaluate a square root. I've written the following:
#include <math.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
double a;
a=sqrt(2.0);
printf("Square root from 2 is %d\n",a);
return 0;
}
But output is Square root from 2 is 1719614413. I really don't understood this. Please explain me.
You are using C (which mostly compiles as C++ as well), not C++ and you made a mistake doing so. You can either learn C or use C++, where it's harder to make those mistakes:
#include <cmath>
#include <iostream>
int main()
{
double a = std::sqrt(2.0);
std::cout << "Square root from 2 is " << a << std::endl;
return 0;
}
Additional explanation:
printf("Square root from 2 is %d\n",a);
This prints a string and expects an integer (%d) to be passed. You passed a double, wich comes out as garbage. You can use (%lf) for doubles.
If you still want to stick to our example, please consider using %f rather than %d:
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
The following Code is not working. I get an error at the command cin >> h. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
string h = " ";
cout << "hi" << endl;
cin >> h;
cout << h << endl;
system("pause");
return 0;
}
Random guessing:
You forgot to #include <string>
You forgot to include <string> and C++ punished you for that.
Ah, but every man and his dog should know that by not including <string>, you were using the default >> operator, that has well know issues with strings.
C++'s "leave the progammers free to shoot themselves in the foot" philosophy at its best.
C++ lore tells the unfortunate wandereds should use getline instead of cin >>, but there have been heated debates among scholars on this fine doctrine point.