[question] char to const CHAR*

Hi all,
I got a problem with my c++ code. I'm still verry new and i did not made the following code all my self.I only try to edit it but .. i ran into a problem i cant seem to figure out.

Error:
13 - cannot convert `char (*)()' to `const CHAR*' for argument `2' to `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'


I got this code from what i'm trying to make an function: ((Made by Smith))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <windows.h>
#include <stdio.h>
#include <string>

const char* c_str();
using namespace std;

char RandomFileName()
{
srand(GetTickCount());//Seeeeeeed
const char *letter[] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n",
                        "o","p","q","r","s","t","u","v","w","x","y","z","1","2",
                        "3","4","5","6","7","8","9","9", 0 };//Chars to randomize
                       
char filename[MAX_PATH]="";
int rnd;
int length=0;


for(;;)//Infinte Loop
{
rnd = rand() % 36;// The amount of chars to randomize
strcat(filename,letter[rnd]);//Bind the random char to the buffer

if(strlen(filename)==12)//Adjust this for a bigger or smaller filename
{
strcat(filename,".bmp");//Bind ".bmp" onto the finshed product
break;//Escape the loop
}
length++;//Add more chars if not finished

}    



  return *filename;
}


This code makes an random file name.

When i try to use this function in my project main.cpp file (9the function is seperated into RandomFileName.h for a better vieuw)

this is my code where the error occurse:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <windows.h>
#include <stdio.h>
#include "RandomName.h"
#include <string>

using namespace std;



int main()
{

 MessageBoxA(NULL, RandomFileName,"RandomFileName",MB_OK);
 system("PAUSE");   
        
}


i want to use the random filename in the messagebox, but than i get the error shown above. I lookd for an sellution and found somethings like .C_str but i dont really know how it workd so i messed a bit with it but it wouldnt work.
((And i has spend an whole day at it))

SO if anyone can help me out my missery. Please do!

Thanx,
Matthijs



Well, that's not what I would call C++ (char[]? strcat()? strlen()?), but anyhow...

You cannot return *filename. You probably want to return just filename. Or, since this is C++, return a string rather than a char[].
well, i think your right.

I putted the Astrix (*) there because it seemd to solve an error.
But i could also do:
filename[MAX_PATH] there that also removed an error. But i don't know if that was right so thats why i used the astrix.

Any idea how to make this working?
You're function is declared to return a single character. Is that what you want?
no i want it to send back the whole text. but i got an new code that works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <windows.h>

const char* letters = "abcdefghijklmnopqrstuvwxyz0123456789";

char* randomize(int size, char* ext)
{
    char* result = (char*) malloc(size+strlen(ext)+1);
    
    for (int i=0; i<size; i++) 
        result[i] = letters[rand()%36];
    
    strcat(result, ext);
    return result;
}

int main()
{
    char* random = randomize(12,".bmp");
    MessageBoxA(NULL, random, "RANDOM",MB_OK);
    free(random);
    getchar();
}


thanx for the help!
Topic archived. No new replies allowed.