Setting a random window title in C++ - visual-c++

i wanna set my window title to a random string but i cant figure out how.
I tried to use a random string generator but i dont get how to implement it into the window title.
The random string generator i tried.
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}

You have to use the _T macro to make sure that the string is in the correct format and set your title using the SetConsoleTitle function :
SetConsoleTitle(_T(*YOUR_RANDOM_TITLE_VAR*));

Related

Convert string to double without parse or tryparse in C#

How can I convert a string to a double using a method that does not include parse or tryparse? I have the program for converting a string to a long, would it be the same for a double? I am a complete newbie.
Code snippets from OP comment below:
public static bool isLong(string s) {
bool n = true;
int a = 0;
s = s.Trim();
for (a = 0; (a < s.Length); a = a + 1) {
n = n && ((s[a] >= '0') && (s[a] <= '9'));
}
return (n);
}
public static long toLong(string s) {
long ret = 0;
int a;
s = s.Trim();
if (isLong(s)) {
for (a = 0; (a< s.Length); a = a + 1) {
ret = (ret * 10) + (s[i] - '0');
}
} else {
}
return (ret);
}
I think I now understand the question. If so, the answer is yes, sort of.
long is an integer type, so processing one digit at a time is fairly straight forward.
double is a floating decimal type, so you have to figure out a way to deal with the decimal period in the middle.
Is this a class assignment or something where you absolutely must write this code on your own? If not, please consider using the library functions that already exist for this purpose, such as stod: http://www.cplusplus.com/reference/string/stod/

String to Int Conversion in Arduino

I' am trying to convert string to int(like Integer.parseInt() in java) in arduino in order to make some operation's on the numbers. Unfortunately none of my solution's worked.
Until now I tried:
Create char Array and call atoi function:
String StringPassword;
uint8_t *hash;
//Here I define hash
int j;
for (j = 0; j < 20; ++j) {
StringPassword.concat(hash[j]);
}
//Checking String Size
Serial.println("Size");
//Checking String
Serial.println(StringPassword.length());
Serial.println(StringPassword);
int jj;
char PasswordCharArray[StringPassword.length()];
StringPassword.toCharArray(PasswordCharArray, StringPassword.length());
awa = atoi(PasswordCharArray);
Serial.println(awa);
Output:
Size
48
168179819314217391617011617743249832108225513297
18209
Create char Array for null terminated string and call atoi function:
String StringPassword;
uint8_t *hash;
//Here I define hash
int j;
for (j = 0; j < 20; ++j) {
StringPassword.concat(hash[j]);
}
//Checking String Size
Serial.println("Size");
//Checking String
Serial.println(StringPassword.length());
Serial.println(StringPassword);
int jj;
char PasswordCharArray[StringPassword.length()+1];
StringPassword.toCharArray(PasswordCharArray,StringPassword.length()+1);
awa = atoi(PasswordCharArray);
Serial.println(awa);
Output:
Size
48
168179819314217391617011617743249832108225513297
-14511
use toInt Function:
String StringPassword;
uint8_t *hash;
//Here I define hash
int j;
for (j = 0; j < 20; ++j) {
StringPassword.concat(hash[j]);
}
//Checking String Size
Serial.println("Size");
//Checking String
Serial.println(StringPassword.length());
Serial.println(StringPassword);
awa = StringPassword.toInt();
Serial.println(awa);
Output:
Size
48
168179819314217391617011617743249832108225513297
-14511
What is the proper way of changing String to Int so:
awa = 168179819314217391617011617743249832108225513297 ?
And could someone explain to me why my solution's didn't worked? I tried to use the function's that were mentioned on Stackoverflow and Arduino forum to solve this.
The number 168179819314217391617011617743249832108225513297 reaches the maximum integer value limit so therefore this will not convert into an integer.
Try using atol() instead of atoi(). Long numbers can hold more data like the number shown above.

QT. Is any way to multiplicate string several times?

In python i can write
s = "dad" * 3
Result will be: s = "daddaddad"
I want to append "tabs" to my string. Something like:
QString tabs = "\t" * count;
What would be a simple, idiomatic way to do it?
You can do it quite simply with a loop:
QString mystring("somestring");
QString output;
for (int i = 0; i < 3; ++i)
output.append(mystring);
//'output' will contain the result string
Please note that the code I provide is in C++, not Python, but the concept still applies (and should be easily ported).
EDIT:
If you need to concatenate single characters, you could do it more easily like this:
int size = 5;
QString output(size, QChar('\t'));
//'output' contains 5 tab characters
Or, if you need to assign to another string (output is already created):
int size = 5;
output.fill(QChar('\t'), size);
//'output' contains 5 tab characters
#include <QString>
QString s;
for(int i = 0; i < 3; ++i)
{
s << "dad";
}

Generate string of random characters cocoa?

I know how to generate random numbers, but what I really need is a string of random characters. This is what I have so far:
NSString *letters = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789?%$";
char generated;
generated = //how do i define this?
NSLog(#"generated =%c", generated);
[textField setStringValue:generated];
-(NSString*)generateRandomString:(int)num {
NSMutableString* string = [NSMutableString stringWithCapacity:num];
for (int i = 0; i < num; i++) {
[string appendFormat:#"%C", (unichar)('a' + arc4random_uniform(25))];
}
return string;
}
then Call it like this for a 5 letter string:
NSString* string = [self generateRandomString:5];
See this SO Q & A.
Generate a random alphanumeric string in Cocoa

Converting a 1D pointer array (char) into a 2D pointer array (char) in Visual C++.

I am new to c++ programming I have to call a function with following arguments.
int Start (int argc, char **argv).
When I try to call the above function with the code below I get run time exceptions. Can some one help me out in resolving the above problem.
char * filename=NULL;
char **Argument1=NULL;
int Argument=0;
int j = 0;
int k = 0;
int i=0;
int Arg()
{
filename = "Globuss -dc bird.jpg\0";
for(i=0;filename[i]!=NULL;i++)
{
if ((const char *)filename[i]!=" ")
{
Argument1[j][k++] = NULL; // Here I get An unhandled
// exception of type
//'System.NullReferenceException'
// occurred
j++;
k=0;
}
else
{
(const char )Argument1[j][k] = filename [j]; // Here I also i get exception
k++;
Argument++;
}
}
Argument ++;
return 0;
}
Start (Argument,Argument1);
Two things:
char **Argument1=NULL;
This is pointer to pointer, You need to allocate it with some space in memory.
*Argument1 = new char[10];
for(i=0, i<10; ++i) Argument[i] = new char();
Don't forget to delete in the same style.
You appear to have no allocated any memory to you arrays, you just have a NULL pointer
char * filename=NULL;
char **Argument1=NULL;
int Argument=0;
int j = 0;
int k = 0;
int i=0;
int Arg()
{
filename = "Globuss -dc bird.jpg\0";
//I dont' know why you have 2D here, you are going to need to allocate
//sizes for both parts of the 2D array
**Argument1 = new char *[TotalFileNames];
for(int x = 0; x < TotalFileNames; x++)
Argument1[x] = new char[SIZE_OF_WHAT_YOU_NEED];
for(i=0;filename[i]!=NULL;i++)
{
if ((const char *)filename[i]!=" ")
{
Argument1[j][k++] = NULL; // Here I get An unhandled
// exception of type
//'System.NullReferenceException'
// occurred
j++;
k=0;
}
else
{
(const char )Argument1[j][k] = filename [j]; // Here I also i get exception
k++;
Argument++;
}
}
Argument ++;
return 0;
}
The first thing you have to do is to find the number of the strings you will have. Thats easy done with something like:
int len = strlen(filename);
int numwords = 1;
for(i = 0; i < len; i++) {
if(filename[i] == ' ') {
numwords++;
// eating up all spaces to not count following ' '
// dont checking if i exceeds len, because it will auto-stop at '\0'
while(filename[i] == ' ') i++;
}
}
In the above code i assume there will be at least one word in the filename (i.e. it wont be an empty string).
Now you can allocate memory for Argument1.
Argument1 = new char *[numwords];
After that you have two options:
use strtok (http://www.cplusplus.com/reference/clibrary/cstring/strtok/)
implement your function to split a string
That can be done like this:
int i,cur,last;
for(i = last = cur = 0; cur < len; cur++) {
while(filename[last] == ' ') { // last should never be ' '
last++;
}
if(filename[cur] == ' ') {
if(last < cur) {
Argument1[i] = new char[cur-last+1]; // +1 for string termination '\0'
strncpy(Argument1[i], &filename[last], cur-last);
last = cur;
}
}
}
The above code is not optimized, i just tried to make it as easy as possible to understand.
I also did not test it, but it should work. Assumptions i made:
string is null terminated
there is at least 1 word in the string.
Also whenever im referring to a string, i mean a char array :P
Some mistakes i noticed in your code:
in c/c++ " " is a pointer to a const char array which contains a space.
If you compare it with another " " you will compare the pointers to them. They may (and probably will) be different. Use strcmp (http://www.cplusplus.com/reference/clibrary/cstring/strcmp/) for that.
You should learn how to allocate dynamically memory. In c you can do it with malloc, in c++ with malloc and new (better use new instead of malloc).
Hope i helped!
PS if there is an error in my code tell me and ill fix it.

Resources