Hi all
My friend sent me this C++ program that opens up notepad and starts typing, its a joke he made to scare his little brother, I tried to modify it to display more of the message "Im watching you" but when I do it wont compile and I dont understand the error message, Im a noob lol
Heres the code all I did was copy and paste the spooky message a few more times underneath the original spooky messages, so instead of displaying the message 4 times it displayed it 6 times

#include <windows.h>
#include <cstdlib>
using namespace std;


int main(int argc,char *argv[])
{

//hides your program from the desktop
HWND hwnd1 = FindWindow(NULL,argv[0]);
ShowWindow(hwnd1,SW_HIDE);

//array for your messge
char text[75]= {'i','m',' ','w','a','t','c','h','i','n','g'
,' ','y','o','u','.','.','.'
,' ','i','m',' ','w','a','t','c','h','i','n','g'
,' ','y','o','u','.','.','.'
,' ','i','m',' ','w','a','t','c','h','i','n','g'
,' ','y','o','u','.','.','.'
,' ','i','m',' ','w','a','t','c','h','i','n','g'
,' ','y','o','u','.','.','.'};

//this allows your program to execute a minute later
Sleep(15000);


//plays the scary file - must link libwinmm.a
PlaySound("C:/scary1.wav",NULL, SND_FILENAME);


//Sleep(3000);
//opens Microsoft Word
ShellExecute(NULL, "open","C:/WINDOWS/system32/notepad.exe"
, NULL, NULL, SW_SHOWNORMAL);

HWND hwnd2= FindWindow(NULL,"Untitled - Notepad");
//makes Word the active window
SetActiveWindow(hwnd2);

Sleep(2000);
//simulates a mouse left click
mouse_event(MOUSEEVENTF_LEFTDOWN,200,200,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,200,200,0,0);

// Sleep(1000);
//writes your scary message
for(int i=0;i<75;i++){
keybd_event(VkKeyScan(text[i]),0,0,0);
Sleep(rand() %200);
}



return 0;
}

Many thanks

HLA91

>it wont compile and I dont understand the error message
And we're supposed to divine what that error message is? Maybe you should post it.

Sorry stupid me here they are 3 of them
In function 'int main(int,char**)':
too many initializers for 'char[75]'
[Build Error] [ghost4school.o] Error1

I thought the second message meant too many letters because I had only declared 75 but I counted the original number of letters and that went over 75 so I have no other ideas

>too many initializers for 'char[75]'
By my count you have exactly 75, so either the code you posted isn't the same code you've compiled, or you're getting a strange error. In fact, it compiles okay for me. Try removing the array size and using sizeof text in the loop instead of 75. That should sidestep the problem.

What am I supposed to count becasue at the moment im counting anyhting between the ' ' marks that includes spaces what are you counting?

>what are you counting?
I'm counting the number of character literals. Alternatively, you can count the number of commas and add one. Anyway, you're making this harder on yourself because you could just do this:

const char *text =
  "i'm watching you... "
  "i'm watching you... "
  "i'm watching you... "
  "i'm watching you...";

Then instead of counting the number of characters, just traverse until you find '\0'. Much easier.

OK i got the *text part sorted and it works now thankyou but when it prints it does im watching you 4 times but on the fourth one it only prints
i'm watching yo
and then it re opens one of the windows i minimized
here is the code now

#include <windows.h>
#include <cstdlib>
using namespace std;


int main(int argc,char *argv[])
{

//hides your program from the desktop
HWND hwnd1 = FindWindow(NULL,argv[0]);
ShowWindow(hwnd1,SW_HIDE);

//array for your messge
const char *text =
  "i'm watching you... "
  "i'm watching you... "
  "i'm watching you... "
  "i'm watching you...";

//this allows your program to execute a minute later
Sleep(15000);


//plays the scary file - must link libwinmm.a
PlaySound("C:/scary1.wav",NULL, SND_FILENAME);


//Sleep(3000);
//opens Microsoft Word
ShellExecute(NULL, "open","C:/WINDOWS/system32/notepad.exe"
, NULL, NULL, SW_SHOWNORMAL);

HWND hwnd2= FindWindow(NULL,"Untitled - Notepad");
//makes Word the active window
SetActiveWindow(hwnd2);

Sleep(2000);
//simulates a mouse left click
mouse_event(MOUSEEVENTF_LEFTDOWN,200,200,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,200,200,0,0);

// Sleep(1000);
//writes your scary message
for(int i=0;i<75;i++){
keybd_event(VkKeyScan(text[i]),0,0,0);
Sleep(rand() %200);
}



return 0;
}

thanks for help

HLA91

You neglected to make all of the changes I suggested. Change this:

for(int i=0;i<75;i++){

To this:

for ( int i = 0; text[i] != '\0'; i++ ) {

OK thanks its runs fine for about 9 messages and then it just prints '''''''''''''''''''''''''''''''''''''''''''''
here is the code now

#include <windows.h>
#include <cstdlib>
using namespace std;


int main(int argc,char *argv[])
{

//hides your program from the desktop
HWND hwnd1 = FindWindow(NULL,argv[0]);
ShowWindow(hwnd1,SW_HIDE);

//array for your messge
const char *text= 
"I'm Watching you..."
"I'm Watching you..."
"I'm Watching you..."
"I'm Watching you...";

//this allows your program to execute a minute later
Sleep(15000);


//plays the scary file - must link libwinmm.a
PlaySound("C:/scary1.wav",NULL, SND_FILENAME);


//Sleep(3000);
//opens Microsoft Word
ShellExecute(NULL, "open","C:/WINDOWS/system32/notepad.exe"
, NULL, NULL, SW_SHOWNORMAL);

HWND hwnd2= FindWindow(NULL,"Untitled - Notepad");
//makes Word the active window
SetActiveWindow(hwnd2);

Sleep(2000);
//simulates a mouse left click
mouse_event(MOUSEEVENTF_LEFTDOWN,200,200,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,200,200,0,0);

// Sleep(1000);
//writes your scary message
for ( int i = 0; text[i] != '\0'; i++ ) {
keybd_event(VkKeyScan(text[i]),0,0,0);
Sleep(rand() %200);
}



return 0;
}

here is what is printed part of one of the messages is cut becasue msn popped up and it started writing in the search box lol


Im watching you… im watching you… im watching you… im watching you… im watching you… im watching you… you… im watching you… im watching you… im watching’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

I dont understand where it is getting the ''''''''' from can somebody help me again please?

Many thanks for time

HLA91

>OK thanks its runs fine for about 9 messages
>and then it just prints '''''''''''''''''''''''''''''''''''''''''''''
It should only print four messages, because that's how many you've defined. In fact, that's precisely what it does for me. Did you post the actual code you're using?

I just tried it with the code I posted and it worked fine so maybe it got confused by the msn popping up, thats the only thing i can think of, thanks for all your help hopefully I should have no more trouble with it lol


Thanky very much

HLA91

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.