PDA

Click to See Complete Forum and Search --> : C++ code not working right


kryptonite0110
December 28th, 2003, 02:49 AM
I wrote this code in interest of learning stdio.h somewhat fleuently... but it decided to be bugged on me... what may be the bug??

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
int main()
{
FILE * bin;
char buffer[100];
bool test;
int a;
bin = fopen ("bin1.raw", "wb");
std::cin.getline(buffer, 100);
for (int a = 0; a < 100; a++)
{
putchar(buffer[a]);
}

for (int i = 0; i < 100; i++)
{
if (!test)
{
if (!buffer)
{
bool test = 1;
a = i;
i = 0;
}
}
else
{
if (i < a)
{
putchar(buffer[i]);
putc(buffer[i], bin);
}
}
}
fclose(bin);

system("PAUSE");
return 0;
}


i was just trying to output only the amount of text used... there are easier ways of going about this problem... but one thing led to another and this is what i got. im not that interested in the "working project" but the "why it wont work"

pwaring
December 28th, 2003, 03:22 AM
What error messages are you getting? Does it refuse to compile, or just not do what you want at runtime?

kryptonite0110
December 28th, 2003, 03:23 AM
at runtime, i get the inputed string, and a bunch of other crap written out.

pwaring
December 28th, 2003, 03:39 AM
What bunch of other crap? Posting the entire output here (attach it as a text file if it's more than 20 lines) would be very helpful...

kryptonite0110
December 28th, 2003, 03:41 AM
ok... here is the output...

jordan ¬ÂwàÿA°>Ãw0 Áwÿÿÿÿ3«ÂwH“ÃwØ7" p7"  UŠÃw 0è ðýV  0ÿA!éÁwpÿA-@ A  A hÿA

kryptonite0110
December 28th, 2003, 04:34 AM
I got it to work... what was wrong was that the computer didn't have any more input so it just wrote somthing wierd down... so i found the strlen of the buffer and created a string of that length... no loops were needed.

lbpf
December 31st, 2003, 04:58 PM
Originally posted here (http://www.AntiOnline.com/showthread.php?threadid=252792#post704337) by kryptonite0110
I wrote this code in interest of learning stdio.h somewhat fleuently... but it decided to be bugged on me... what may be the bug??

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
int main()
{
FILE * bin;
char buffer[100];
bool test;
int a;
bin = fopen ("bin1.raw", "wb");
std::cin.getline(buffer, 100);
for (int a = 0; a < 100; a++)
{
putchar(buffer[a]);
}

for (int i = 0; i < 100; i++)
{
if (!test)
{
if (!buffer)
{
bool test = 1;
a = i;
i = 0;
}
}
else
{
if (i < a)
{
putchar(buffer[i]);
putc(buffer[i], bin);
}
}
}
fclose(bin);

system("PAUSE");
return 0;
}


Couple of pointers here:

1) You create a bool variable inside a loop. One should try to avoid doing this as much as possible. In doing this, you are recreating the variable everytime the loop runs through and are destroying it equal amounts of time as well. Also, you are not using the same bool as created earlier in your program. If you want to do that, you have to remove the bool and just say test=1.

2) Don't mix input streams. It is bad, very bad. Mixing input streams causes undefined behavior. Basically, your program can do really weird stuff if you mix.