C++ code not working right
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"
Re: C++ code not working right
Quote:
Originally posted here 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.