| View previous topic :: View next topic |
| Author |
Message |
Mattraks How do I cheat?
Reputation: 0
Joined: 21 Oct 2007 Posts: 0 Location: IRAQ!
|
Posted: Mon Sep 01, 2008 11:39 am Post subject: [C++] [Help] sprintf not working I guess |
|
|
Well, I'm going to make a bot for a certain channel on IRC and so far, it isn't working how I want it to. The code I'm having problems with is:
| Code: | char nick[] = "";
char tempnick[] = "";
char identify[] = "";
char tempidentify[] = "";
int isregistered = 1;
char idleident[] = "";
char tempidleuser[] = "";
char tempidlepass[] = "";
int main() {
std::cout << "Enter a nick: ";
std::cin >> tempnick;
sprintf(nick, "NICK %s\r\n", tempnick);
std::cout << "Enter the pass with nickserv(0 if not registered): ";
std::cin >> tempidentify;
sprintf(identify, "PRIVMSG nickserv IDENTIFY %s\r\n", tempidentify);
if (strcmp(tempidentify, "0") == 0) {
isregistered = 0;
}
std::cout << "Enter your idlerpg username(cAsE sEnSaTiVe): ";
std::cin >> tempidleuser;
std::cout << "Enter your idlerpg password(cAsE sEnSaTiVe): ";
std::cin >> tempidlepass;
sprintf(idleident, "PRIVMSG idlebot login %s %s\r\n", tempidleuser, tempidlepass);
printf("%s = NICK\n %s = identify\n %s = idleident\n", nick, identify, idleident);
Sleep(10000);
exit(0); |
That outputs:
| Quote: | Enter a nick: nick
Enter the pass with nickserv(0 if not registered): passwithnickserv
Enter your idlerpg username(cAsE sEnSaTiVe): idleuser
Enter your idlerpg password(cAsE sEnSaTiVe): idlepass
NIPRPRIVMSG idlebot login RIVMSG idlebot login RIVMSG idlebot login RIVMSG i IVM
SG idlebot login RIVMSG idlebot login RIVMSG idlebot login RIVMSG i
= NICK
PRPRIVMSG idlebot login RIVMSG idlebot login RIVMSG idlebot login RIVMSG i IVMS
G idlebot login RIVMSG idlebot login RIVMSG idlebot login RIVMSG i
= identify
PRIVMSG idlebot login RIVMSG idlebot login RIVMSG idlebot login RIVMSG i IVMSG
idlebot login RIVMSG idlebot login RIVMSG idlebot login RIVMSG i
= idleident |
I filled out what it asks and that's what happens.
Does anyone know why its happening and/or can tell me how to fix it? Thanks. _________________
FOR ALLAH! |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Mon Sep 01, 2008 12:53 pm Post subject: |
|
|
Personally, with so many cout/cin's i would just use namespace std;
| Code: | | sprintf(nick, "NICK %s\r\n", tempnick); |
Nothing is stored in yet
Why switch between cout/sprintf?
| Code: | | sprintf(identify, "PRIVMSG nickserv IDENTIFY %s\r\n", tempidentify); |
Nothing is stored in identify yet either.
On second thought, I'm not familiar enough with sprintf() to be positive but if nothing else consider this a bump for you. _________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Sep 01, 2008 1:04 pm Post subject: |
|
|
just do this
Then at the start of your code (or when you need a clean buffer)
| Code: | | memset(nick, 0,255); |
_________________
|
|
| Back to top |
|
 |
Mattraks How do I cheat?
Reputation: 0
Joined: 21 Oct 2007 Posts: 0 Location: IRAQ!
|
Posted: Mon Sep 01, 2008 1:04 pm Post subject: |
|
|
| manc wrote: | | Why switch between cout/sprintf? |
cout just prints out a message.
sprintf sets the destination (first parameter) to what you have for the second parameter and then sets the %'s to what you have for the third, fourth, fifth, etc. to the value of those. Similar to printf in the way of the %'s.
@blankrider: Thanks, that worked _________________
FOR ALLAH!
Last edited by Mattraks on Mon Sep 01, 2008 1:08 pm; edited 1 time in total |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Sep 01, 2008 1:21 pm Post subject: |
|
|
Main problem is that you are not allocating memory for those strings, and it is not being allocated locally. You need to make it an array of given size or allocate it dynamically (malloc). _________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Mon Sep 01, 2008 5:26 pm Post subject: |
|
|
you cannot do
char tempidleuser[] = "";
etc.. it has to be allocated yah..
like char tempidleuser[2] = "";
that will be a array for 2 characters..
you think using tempidleuser[]
will give you any size you want? and that is the case it does work.. when you do tempidleuser[] in a function.. you can *Copy* another array and it will work but you just copied it but the allocation still happened but you just don't see it..
| Code: |
int main(int a, char*b[]) {
playerChat("test test 123 testing, oh yah up the ...");
return 0;
}
void playerChat(char chat[]) {
printf("chat = %s", chat);
}
|
_________________
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Mon Sep 01, 2008 7:44 pm Post subject: |
|
|
Add
using namespace std;
So you can just do cout instead of std::cout. |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Sep 01, 2008 8:25 pm Post subject: |
|
|
| _void_ wrote: | Add
using namespace std;
So you can just do cout instead of std::cout. |
So we see who is a lazy coder.
I Don't recommend doing what _void_ told you to do. It's pointless and stupid for only using a mere 2 functions. Especially when the application is small. _________________
|
|
| Back to top |
|
 |
|