PDA

View Full Version : C++ type/talking...



anonymity
08-23-2009, 02:08 AM
Stuck on type/talking... I am working on a C++ script... I just started to get this portion of programming... I thought that it might help me with SCAR programming. Well.. to the point.. this is my problem..

1. What is the difference between:

cout << " text goes here" << endl;and
std::cout <<"text goes here\n";

2. Is there a way I can make the words type across the screen?
example: the typesend('text goes here'); command in SCAR

3. I can make the system pause with:

system("pause");
but I want it to pause until someone hits a key... without saying that... I want it to be my specified text..

Any constructive help would be greatly appreciated.

JAD
08-23-2009, 03:15 AM
1) You only need to use the std:: if you did not declare it above with a using statement:



#include<iostream>
using std::cout;


Or



#include<iostream>
using namespace std; //includes pretty much everything


As far as the new lines go, endl flushes the input buffer of characters and creates a new line for output, while \n just creates a new line. Not much different for beginners.

2) I don't remember. I will look it up and edit this post. But I think it uses windows.h commands

3) cin.get(); will wait until somebody presses the enter key:



#include <iostream>
using namespace std;

int main()
{
cout << "Press Enter Key to continue...";
cin.get();
return 0;
}

GoF
08-23-2009, 03:23 AM
1. Can't really remember any "indepth theory" about that.

2. I think I've done something like that (if you mean type the text letter by letter to the command prompt), I can edit it here if I find it if you like.
E: Because its 11pm, raining outside and I have 6000ms latency on WoW I whipped up something after like 3 months of not even looking at C++.



#include <windows.h>
#include <iostream>

using namespace std;

void typeSend(char message[255]) {
for(int t = 0; t < strlen(message); t++) {
cout << message[t];
Sleep(rand() % 250);
}
}

int main() {
char test[255];
cin >> test;

typeSend(test);
system("PAUSE");

return 0;
}


3. You could use getch(); from conio.h library.


#include <conio.h>

...
*your text or whatever*
getch();

E: Oh, it was cin.get();.. Was thought there was some cin.* thingie too but there's something funny with my internets (takes 1-2 mins to load google) so I couldn't check.

boberman
08-23-2009, 07:49 AM
1. Jad pretty much answered it. It is all about namespacing. For big programs it is useful to be able to put related functions in the same namespace to avoid overlap. That way you could have someFunction(blah b) in namespace abc and someFunction(blah b) in namespace def and each would be able to do something completely different (Somewhat like operator overloading, but not quite).

2. GoF, I don't think thats what he was looking for. That will send letters to the console, but not anywhere else. To do more then that requires a bit of WinAPI trickery. Check out Scar++'s sendkeys for more information on how to do it.

3. Again, jad explained it, a simple cin.get(); will pause a program.

anonymity
08-23-2009, 07:51 AM
Hey thanks so much for the help so far. It is very useful.

1. I think that is what I was looking for.
[ANSWERED]

2. GoF, that is really close. I don't mean me typing... I want the program to type what I tell it to in the cout when I execute the program. I don't what the text just to appear line by line.. I want it to look like it is being typed out..
[UN-ANSWERED]

3. I think that will work with the cin.get().... I should have thought of that... darn..
[ANSWERED]

Ok I have one more.. I want the lines to slowly appear... giving the illusion that the computer is processing a bunch. ideas?

Edit: thanks boberman.. I will look more into the trickery..

boberman
08-23-2009, 07:59 AM
Hey thanks so much for the help so far. It is very useful.

1. I think that is what I was looking for.
[ANSWERED]

2. GoF, that is really close. I don't mean me typing... I want the program to type what I tell it to in the cout when I execute the program. I don't what the text just to appear line by line.. I want it to look like it is being typed out..
[UN-ANSWERED]

3. I think that will work with the cin.get().... I should have thought of that... darn..
[ANSWERED]

Ok I have one more.. I want the lines to slowly appear... giving the illusion that the computer is processing a bunch. ideas?

Edit: thanks boberman.. I will look more into the trickery..

Make the computer look like its doing a bunch? Easy something like

timer = GetTickCount() + busytime (in ms);
while(GetTickCount() < timer);

will waste all the cycles you need for however long you need it to.

If you just want to type to the console, then Gofs code will work, the only problem his code has is that it has a max time of 250ms. Instead something like this


void typeSend(string message) {
for(int t = 0; t < message.size(); t++) {
cout << message[t];
Sleep(250 + rand() % 250);
}
}

will give a range of 250ms to 500ms for each char to print out. That should seem pretty human.

And yes, I changed it to string, no reason to use a char when a string class will do just fine.

JAD
08-23-2009, 08:00 AM
I believe you can use a Sleep(1000); command. Make sure you include windows.h though.

boberman
08-23-2009, 08:01 AM
I believe you can use a Sleep(1000); command. Let me double check.

Sleep doesn't rev up the CPU, so if you looked at task manager it would look like the computer isn't doing anything (I assume thats what the OP wants) Instead, doing a loop like I did above would keep the CPU occupied.

anonymity
08-23-2009, 08:17 AM
2. GoF, I don't think thats what he was looking for. That will send letters to the console, but not anywhere else. To do more then that requires a bit of WinAPI trickery. Check out Scar++'s sendkeys for more information on how to do it.
I think that is what I was going for.. Like I said, I will look into it more.. Thanks.



Make the computer look like its doing a bunch? Easy something like

timer = GetTickCount() + busytime (in ms);
while(GetTickCount() < timer);

will waste all the cycles you need for however long you need it to.

If you just want to type to the console, then Gofs code will work, the only problem his code has is that it has a max time of 250ms. Instead something like this


void typeSend(string message) {
for(int t = 0; t < message.size(); t++) {
cout << message[t];
Sleep(250 + rand() % 250);
}
}

will give a range of 250ms to 500ms for each char to print out. That should seem pretty human.
1. would you have a suggestion as to how much time to use? Was there an include I needed for that?

2. I don't want to type in the console.. I want to have my .cpp text show up like it is being typed... the best way I can explain that would be the typesend('text here'); command in srl SCAR.


I believe you can use a Sleep(1000); command. Make sure you include windows.h though.
That also was very useful... I will be using that....