Results 1 to 7 of 7

Thread: C++ code not working right

  1. #1

    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"
    You laugh because im different, i laugh because your all the same.

  2. #2
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    What error messages are you getting? Does it refuse to compile, or just not do what you want at runtime?
    Paul Waring - Web site design and development.

  3. #3
    at runtime, i get the inputed string, and a bunch of other crap written out.
    You laugh because im different, i laugh because your all the same.

  4. #4
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    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...
    Paul Waring - Web site design and development.

  5. #5
    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
    You laugh because im different, i laugh because your all the same.

  6. #6
    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.
    You laugh because im different, i laugh because your all the same.

  7. #7
    Junior Member
    Join Date
    Oct 2002
    Posts
    5

    Re: C++ code not working right

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •