Author Topic: C++ Help Please!  (Read 5259 times)

0 Members and 1 Guest are viewing this topic.

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
C++ Help Please!
« on: January 14, 2012, 12:28:21 am »
Im using this topic to ask questions! yay!
1. How would I clear the screen (CMD) ?
Answer: system("CLS"); <-- Works like a charm! :D

2. How would I freeze the program and request that the user press a button to continue without the program displaying, "Press any key to continue"
Answer: After importing "windows.h" at the top of the program, Sleep(//milliseconds//); works fine! :D

3. How would I set a timer for the program to wait for so long before displaying the next message?

4. How would I check to see if a string is equal to another string?
I want the user to type in a direction, (East, north, west, south) and have the program move him that way (Text based game)
« Last Edit: January 14, 2012, 01:38:29 am by hellninjas »

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: C++ Help Please!
« Reply #1 on: January 14, 2012, 01:33:07 am »
2. cin.get() ?
3. There's a sleep method, but I only know of the Windows-only one. You would need to import the windows.h header and then you could use Sleep(time_in_milliseconds);
« Last Edit: January 14, 2012, 01:35:20 am by BlakPilar »

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: C++ Help Please!
« Reply #2 on: January 14, 2012, 01:33:49 am »
How would i import?

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: C++ Help Please!
« Reply #3 on: January 14, 2012, 01:34:32 am »
Add #include <windows.h> to the top of your file.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: C++ Help Please!
« Reply #4 on: January 14, 2012, 01:38:55 am »
1. boo, system() makes your code not cross-compatible, not to mention inefficient and slow. However it's harder to avoid than in #2 unless you're using some text rendering libs, like ncurses. (Which you're not)

2. cin.get() will work for pressing enter. See this: link

3. use the ctime lib (#include <ctime>) then the function time() will return the current time. Use a while loop or something to wait for it to get to the appropriate time, comparing the original recorded time to the current using difftime(). The code is simpler than the explanation :P

The link under 2 is a good read for any C/C++ programmer.
« Last Edit: January 14, 2012, 01:41:28 am by willrandship »

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: C++ Help Please!
« Reply #5 on: January 14, 2012, 01:42:05 am »
Hmm... I've never thought of something like that for sleeping, willrandship. Thanks ;)