why does'nt this work..

This is a discussion on why does'nt this work.. within the C++ Programming forums, part of the General Programming Boards category; The peice of the program compiles and executes on a regular C compiler, but when I use a C++ compiler ...

  1. #1
    morone
    Guest

    why does'nt this work..

    The peice of the program compiles and executes on a regular C compiler, but when I use a C++ compiler it compiles but crashes my system..
    The C compiler is pacific c the C++ compiler is dev c++.

    The program checks for brakets and deletes space between them..

    int i. j = 0;
    char file[20] = "<a h ref="sys tem.tx t">

    for (i=0;i<numchar;i++){

    if (file[i]=='<'){

    do{
    if (file[i]==' ')
    i++;

    if (file[i]!=' '){
    file[j]=file[i];
    j++;
    }

    }while (file[i]!='>'&&file[i]);

    }
    file[j]='\0';


    when i comment out the j++ it compiles and runs..

  2. #2
    morone
    Guest
    Sorry I can't I'm at work..But it's basically the algorithm..It's like I can't insert another counter in the while loop without the program crashing..
    Just set the array size bigger and initialize numchar to zero as an int..

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Code:
    int main ( ) {
        char file[] = "wibble wibble <a h ref=\"sys tem.tx t\"> zippy bong";
        int i, j, inscope = 0;
        for ( i = 0, j = 0 ; file[i] != '\0' ; i++ ) {
            if ( !inscope && file[i] == '<' ) inscope = 1;
            if ( inscope && file[i] == '>' ) inscope = 0;
            if ( !inscope || (inscope && file[i] != ' ') ) {
                file[j++] = file[i];
            }
        }
        file[j] = '\0';
        printf( "%s\n", file );
        return 0;
    }
    But "aref" should remain "a ref", so I think you need to revise your spec.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21