943,983 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2005
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 7th, 2007
0

Help printing on screen

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8.  
  9. //hides your program from the desktop
  10. HWND hwnd1 = FindWindow(NULL,argv[0]);
  11. ShowWindow(hwnd1,SW_HIDE);
  12.  
  13. //array for your messge
  14. char text[75]= {'i','m',' ','w','a','t','c','h','i','n','g'
  15. ,' ','y','o','u','.','.','.'
  16. ,' ','i','m',' ','w','a','t','c','h','i','n','g'
  17. ,' ','y','o','u','.','.','.'
  18. ,' ','i','m',' ','w','a','t','c','h','i','n','g'
  19. ,' ','y','o','u','.','.','.'
  20. ,' ','i','m',' ','w','a','t','c','h','i','n','g'
  21. ,' ','y','o','u','.','.','.'};
  22.  
  23. //this allows your program to execute a minute later
  24. Sleep(15000);
  25.  
  26.  
  27. //plays the scary file - must link libwinmm.a
  28. PlaySound("C:/scary1.wav",NULL, SND_FILENAME);
  29.  
  30.  
  31. //Sleep(3000);
  32. //opens Microsoft Word
  33. ShellExecute(NULL, "open","C:/WINDOWS/system32/notepad.exe"
  34. , NULL, NULL, SW_SHOWNORMAL);
  35.  
  36. HWND hwnd2= FindWindow(NULL,"Untitled - Notepad");
  37. //makes Word the active window
  38. SetActiveWindow(hwnd2);
  39.  
  40. Sleep(2000);
  41. //simulates a mouse left click
  42. mouse_event(MOUSEEVENTF_LEFTDOWN,200,200,0,0);
  43. mouse_event(MOUSEEVENTF_LEFTUP,200,200,0,0);
  44.  
  45. // Sleep(1000);
  46. //writes your scary message
  47. for(int i=0;i<75;i++){
  48. keybd_event(VkKeyScan(text[i]),0,0,0);
  49. Sleep(rand() %200);
  50. }
  51.  
  52.  
  53.  
  54. return 0;
  55. }

Many thanks

HLA91
Similar Threads
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Nov 7th, 2007
0

Re: Help printing on screen

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 7th, 2007
0

Re: Help printing on screen

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
Last edited by HLA91; Nov 7th, 2007 at 4:37 pm.
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Nov 7th, 2007
0

Re: Help printing on screen

>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.
Last edited by Narue; Nov 7th, 2007 at 4:49 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 7th, 2007
0

Re: Help printing on screen

What am I supposed to count becasue at the moment im counting anyhting between the ' ' marks that includes spaces what are you counting?
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Nov 7th, 2007
0

Re: Help printing on screen

>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:
C++ Syntax (Toggle Plain Text)
  1. const char *text =
  2. "i'm watching you... "
  3. "i'm watching you... "
  4. "i'm watching you... "
  5. "i'm watching you...";
Then instead of counting the number of characters, just traverse until you find '\0'. Much easier.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 7th, 2007
0

Re: Help printing on screen

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
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8.  
  9. //hides your program from the desktop
  10. HWND hwnd1 = FindWindow(NULL,argv[0]);
  11. ShowWindow(hwnd1,SW_HIDE);
  12.  
  13. //array for your messge
  14. const char *text =
  15. "i'm watching you... "
  16. "i'm watching you... "
  17. "i'm watching you... "
  18. "i'm watching you...";
  19.  
  20. //this allows your program to execute a minute later
  21. Sleep(15000);
  22.  
  23.  
  24. //plays the scary file - must link libwinmm.a
  25. PlaySound("C:/scary1.wav",NULL, SND_FILENAME);
  26.  
  27.  
  28. //Sleep(3000);
  29. //opens Microsoft Word
  30. ShellExecute(NULL, "open","C:/WINDOWS/system32/notepad.exe"
  31. , NULL, NULL, SW_SHOWNORMAL);
  32.  
  33. HWND hwnd2= FindWindow(NULL,"Untitled - Notepad");
  34. //makes Word the active window
  35. SetActiveWindow(hwnd2);
  36.  
  37. Sleep(2000);
  38. //simulates a mouse left click
  39. mouse_event(MOUSEEVENTF_LEFTDOWN,200,200,0,0);
  40. mouse_event(MOUSEEVENTF_LEFTUP,200,200,0,0);
  41.  
  42. // Sleep(1000);
  43. //writes your scary message
  44. for(int i=0;i<75;i++){
  45. keybd_event(VkKeyScan(text[i]),0,0,0);
  46. Sleep(rand() %200);
  47. }
  48.  
  49.  
  50.  
  51. return 0;
  52. }

thanks for help

HLA91
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Nov 8th, 2007
0

Re: Help printing on screen

You neglected to make all of the changes I suggested. Change this:
C++ Syntax (Toggle Plain Text)
  1. for(int i=0;i<75;i++){
To this:
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; text[i] != '\0'; i++ ) {
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2007
0

Re: Help printing on screen

OK thanks its runs fine for about 9 messages and then it just prints '''''''''''''''''''''''''''''''''''''''''''''
here is the code now
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8.  
  9. //hides your program from the desktop
  10. HWND hwnd1 = FindWindow(NULL,argv[0]);
  11. ShowWindow(hwnd1,SW_HIDE);
  12.  
  13. //array for your messge
  14. const char *text=
  15. "I'm Watching you..."
  16. "I'm Watching you..."
  17. "I'm Watching you..."
  18. "I'm Watching you...";
  19.  
  20. //this allows your program to execute a minute later
  21. Sleep(15000);
  22.  
  23.  
  24. //plays the scary file - must link libwinmm.a
  25. PlaySound("C:/scary1.wav",NULL, SND_FILENAME);
  26.  
  27.  
  28. //Sleep(3000);
  29. //opens Microsoft Word
  30. ShellExecute(NULL, "open","C:/WINDOWS/system32/notepad.exe"
  31. , NULL, NULL, SW_SHOWNORMAL);
  32.  
  33. HWND hwnd2= FindWindow(NULL,"Untitled - Notepad");
  34. //makes Word the active window
  35. SetActiveWindow(hwnd2);
  36.  
  37. Sleep(2000);
  38. //simulates a mouse left click
  39. mouse_event(MOUSEEVENTF_LEFTDOWN,200,200,0,0);
  40. mouse_event(MOUSEEVENTF_LEFTUP,200,200,0,0);
  41.  
  42. // Sleep(1000);
  43. //writes your scary message
  44. for ( int i = 0; text[i] != '\0'; i++ ) {
  45. keybd_event(VkKeyScan(text[i]),0,0,0);
  46. Sleep(rand() %200);
  47. }
  48.  
  49.  
  50.  
  51. return 0;
  52. }
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
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Nov 8th, 2007
0

Re: Help printing on screen

>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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: overloading operator
Next Thread in C++ Forum Timeline: printf and reference issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC