 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Apr 16, 2008 3:55 am Post subject: binary convert bug |
|
|
I was about to release my binary converter when I found a bug in it. It won't let you convert more than once. It makes no sense to me. It's perfectly fine working code. There's nothing wrong with the loop. It basically just doesn't allow input the second time (so you have to exit then reopen to do it again). I even tried using recursive functions because I've seen a guy do it that way, but it still didn't work. Heres the code. Any suggestions?
| Code: |
#include <iostream>
using namespace std;
char *input, letter, choice;
int length, asciival, binary[8], total, i;
bool bExit = false;
int main()
{
do {
input = new char[351];
cout<<"Enter a string to convert to its binary form (350 char max): ";
cin.getline(input, 500);
length = strlen(input);
for(i = 0; i<length; i++) {
letter = input[i];
asciival = letter;
while(asciival>0) {
if((asciival%2)==0) {
binary[total] = 0;
asciival = asciival/2;
total++;
}
else {
binary[total] = 1;
asciival = asciival/2;
total++;
}
}
total--;
while(total>=0) {
cout<<binary[total];
total--;
}
}
delete [] input;
cout<<"\n\nWould you like to convert a string again (y/n)?\n";
cin>>choice;
if(choice == 'y') {
continue;
}
else {
bExit = true;
}
} while(bExit == false);
return 0;
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Apr 16, 2008 4:22 am Post subject: |
|
|
A wild guess: there's an end line in your buffer. Try syncing std::cin before reading an another string. | Code: | #include <iostream>
using namespace std;
char *input, letter, choice;
int length, asciival, binary[8], total, i;
bool bExit = false;
int main()
{
do {
input = new char[351];
std::cin.sync(); // THIS ONE ADDED BY MEH!
cout<<"Enter a string to convert to its binary form (350 char max): ";
cin.getline(input, 500);
length = strlen(input);
for(i = 0; i<length; i++) {
letter = input[i];
asciival = letter;
while(asciival>0) {
if((asciival%2)==0) {
binary[total] = 0;
asciival = asciival/2;
total++;
}
else {
binary[total] = 1;
asciival = asciival/2;
total++;
}
}
total--;
while(total>=0) {
cout<<binary[total];
total--;
}
}
delete [] input;
cout<<"\n\nWould you like to convert a string again (y/n)?\n";
cin>>choice;
if(choice == 'y') {
continue;
}
else {
bExit = true;
}
} while(bExit == false);
return 0;
} | Works fine?
EDIT: Erm.. Solves your problem ignoring the "second input", but the result gets messed up? Imma write my own.
Might not be the most efficient way to do this, but w/e, at least it's a bit shorter than yours :P | Code: | #include <iostream>
#include <string>
void printBin(int num);
int main(int argc, char *argv[])
{
std::string sInput;
do {
std::cout << "Enter a string: ";
std::getline( std::cin, sInput );
for(int i = 0; i<sInput.length(); ++i)
printBin( (int)sInput[i] );
std::cout << std::endl << "An another (y/n): ";
std::getline( std::cin, sInput );
} while( sInput == "y" );
return EXIT_SUCCESS;
}
void printBin(int num)
{
if( num > 0 ) {
printBin( num / 2 );
std::cout << num % 2;
}
} | EDIT: Oops.. It also reversed the string, heh heh, doesn't do that anymore.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|