Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: hellninjas on January 14, 2012, 12:28:21 am

Title: C++ Help Please!
Post by: hellninjas 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)
Title: Re: C++ Help Please!
Post by: BlakPilar 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);
Title: Re: C++ Help Please!
Post by: hellninjas on January 14, 2012, 01:33:49 am
How would i import?
Title: Re: C++ Help Please!
Post by: BlakPilar on January 14, 2012, 01:34:32 am
Add #include <windows.h> to the top of your file.
Title: Re: C++ Help Please!
Post by: willrandship 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 (http://www.gidnetwork.com/b-61.html)

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.
Title: Re: C++ Help Please!
Post by: BlakPilar on January 14, 2012, 01:42:05 am
Hmm... I've never thought of something like that for sleeping, willrandship. Thanks ;)