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 last year.
Improve this question
I am attempting to get random numbers out of the getrandom() function however attempting to use it only returns -1 the code i am using below:
#include<iostream>
#include <sys/random.h>
int main(){
void* d = NULL;
ssize_t size = 10;
ssize_t p = getrandom(d, size, GRND_RANDOM);
std::cout << p << std::endl;
}
getrandom returns the number of bytes written. The first argument is the pointer to a byte buffer (to be filled with random bytes), the second argument is the number of random bytes that you want to be written to the buffer.
Your return value (p) being -1 means that there was an error when writing the random bytes to the buffer. This error in your case is because you are passing in NULL as the pointer to the buffer to be filled.
Try this instead:
#include<iostream>
#include <sys/random.h>
int main(){
unsigned char random_bytes[2]; // buffer where getrandom will store the random bytes
ssize_t size = 2;
ssize_t p = getrandom((void*) &random_bytes[0], size, GRND_RANDOM);
std::cout << "First random value: " << random_bytes[0] << std::endl;
std::cout << "Second random value: " << random_bytes[1] << std::endl;
}
source:
https://man7.org/linux/man-pages/man2/getrandom.2.html
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 4 years ago.
Improve this question
I have searched on this a lot, and have not found any satisfactory response. I am codding a small mathematical algorithm in order to learn a few programming languages, and as such, this is part of my first C++ program. However, I can't seem to fix this array. Although people say random values in arrays are due to the arrays being uninitialized, and that you should initialize them to the value you want like so: int array[sizeOf] = {0}, it only sort of works in this example. Here as you can see, all values of the array get set to 0, exept the very last one, which does not seem to be getting initialized: what is causing this, and how can I fix it?
#include <iostream>
using namespace std;
int initialNumber;
int main()
{
cout << "Enter Range value:" << endl;
cin >> initialNumber; //get size
cout << "Solving..." << endl << endl;
//initialize basic ints to be used for calculations
int limiter = initialNumber/2; //the max value we will ever have to multiply by
int arraySize = initialNumber++; //size of the array
int resultingFactorsArray[arraySize] = {0}; //main array
//print each array item value
for (int x = 0; x <= arraySize; x++) {
cout << resultingFactorsArray[x] << endl; //print all array values
//all values printed should be 0, but the last one is uninitialized
}
}
Arrays are 0 based. Meaning, in your for loop, for (int x = 0; x <= arraySize; x++), the part x <= arraySize should be x < arraySize.
An example:
If you declare your array size 10, the valid array indices are 0,1,2,3,4,5,6,7,8,9 (10 total indices).
If you were to throw that in a for loop with x <= 10, your last index would be array[10] (undefined/uninitialized).
So in short, your initialization is correct, it just that your for loop is going out of bounds.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've been reviewing my C++ lately. But I am running into a puzzle about printing a char array. The code is below:
int n = 5;
char *array1 = new char[n];
for (unsigned int i = 0; i < n - 1; i++)
array1[i] = (char)i;
cout << array1 << endl;
cout << array1[3] << endl;
cout << *array1 << endl;
None of the three cout lines works. Could anyone tell me why?
array1[0] == 0. cout << array1 interprets array1 as a pointer to a NUL-terminated string, and since the very first character is in fact NUL, the string is empty.
cout << array1[3] does print a character with ASCII code 3. It's a non-printable character, not visible to a naked eye. Not sure what output you expected to see there.
As a separate answer, it seems you're trying to get a string which has the following : array = "1234....(n-1)"
Try :
for (int i = 0; i<(n-1); i++)
array1[i] = (char)i - '0';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I am trying to get only the alphabetic characters from an array of characters entered by the user. Here is a snippet:
const int SIZE(100);
int main()
{
char* entryTextArray = new char[SIZE];
char* adjustedTextArray= new char[SIZE];
int i, j;
cout << "Enter text, and I will tell you if it is a palindrome!" << endl;
cin.get(entryTextArray, SIZE);
cout << "Length of char array is " << strlen(entryTextArray) << endl;
for(i=0, j=0; i <= (strlen(entryTextArray)); i++) {
if(isalpha(entryTextArray[i]) && (entryTextArray[i] != '\0')) {
adjustedTextArray[j] = entryTextArray[i];
cout << adjustedTextArray[j] << endl;
j++;
}
}
cout << adjustedTextArray << endl;
}
When I compile, the cout of the adjustedTextArray displays the proper individual entrys, but the cout outside of the loop is the entry text, followed by garbage. I have no idea what is wrong! Help?!
You have the condition:
if (something && (entryTextArray[i] != '\0'))
so you are explicitly avoiding to copy the NUL terminating value from entryTextArray to adjustedTextArray. So you need to place it manually.
But since you are working in C++ using std::string just makes more sense.
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 have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have defined a struct data and assigning it to a char*. However the size of strlen function always gives result as 1 and cout doesn't show anything. Here is my code:
struct data
{
int x;
int y;
};
int main()
{
data d;
d.x = 10;
d.y = 20;
char *ch = (char*)&d;
cout <<"Length:" strlen(ch) << endl;
cout << ch << endl;
}
The output is:
Length: 1
Please help me in understanding what's going on ?
The strlen function counts all bytes in the passed string until it finds the character '\0'. This character is the same as the integer 0.
So what the call to strlen is count the number of bytes until it finds a zero, and in your case the zero happens to be in the second byte of the structures binary representation, meaning that the "string length" is one.
The only thing that you can deduce from this is that you are on a little endian system. Other than that, the call is undefined behavior as the "string" isn't actually a string.
The reason it returns 1 is because you are treating the struct as a char array. Doing this you are re-interpreting the contents of the struct - the integer 10, which is probably stored in memory as 0x0A000000 - as a string. And that yields a length of 1 (only 1 non-zero value before a null-character in the array)
Even though you're trying to treat something as a string which isn't a string, if all you want to do is print out the values in the struct, you can do something like:
struct data
{
int x;
int y;
};
int main()
{
data d;
d.x = 10;
d.y = 20;
cout << "x is " << d.x << ", y is " << d.y << endl;
}
Why are you trying to treat that struct as a string?