loop crashes programme

This is a discussion on loop crashes programme within the C Programming forums, part of the General Programming Boards category; hi, i need help with the following part of a code... #include <stdio.h> #include <string.h> main() { int ansi[255]; char ...

  1. #1
    Unregistered
    Guest

    loop crashes programme

    hi,
    i need help with the following part of a code...

    #include <stdio.h>
    #include <string.h>
    main()
    {
    int ansi[255];
    char path[128];
    gets(path);
    FILE *file = fopen(path, "r");
    int i = 0;
    int counter = 0;
    char buffer[200];
    int chars = 0;
    char chr;
    while(!feof(file))
    {
    fgets(buffer,200,file);
    chars += strlen(buffer);

    while(counter++ < strlen(buffer))
    {
    chr = buffer[counter];
    ansi[chr] += 1;
    }

    counter = 0;
    i++;
    }
    (...)


    it compiles but then crashes after entering the filepath. works right without the second while-loop, btw. i wanna use the second loop to count how often different letters are used in a textfile. if buffer[counter] is 'a' (= 97 in ascii), for example, it shall add one to ansi['a'], the 97st element of ansi[].
    have a nice day.

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    while(counter++ < strlen(buffer))
    {
    chr = buffer[counter];
    ansi[chr] += 1;
    }

    note buffer[0] is never read
    for

    entering the loop for first time counter is 0

    after this line : while(counter++ < strlen(buffer))
    counter becomes 1

    so ch takes the value buffer[1]

  3. #3
    Unregistered
    Guest
    um, yeah. a mistake i didn't notice.. but it doesn't change anything and still crashes, heh.
    thanks anyway.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    while(counter < strlen(buffer))
    {
    chr = buffer[counter++];
    ansi[chr] += 1;
    }

  5. #5
    Unregistered
    Guest
    >while(counter < strlen(buffer))
    >{
    >chr = buffer[counter++];
    >ansi[chr] += 1;
    >}

    i fixed it this way, too. but like i said, that's not the problem! i really don't get, why the programme doesn't work... the char buffer[counter] is copied to chr and it really doesn't count if it's element 0 or 1, they both are ascii-characters. i suppose i should convert chr to another datatype while using it as index of ansi[] but i have tried that and it didn't help, too. no one any idea?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about?
    int ansi[256] = {0};

  7. #7
    Unregistered
    Guest
    i've just compiled the code with gcc under OpenBSD instead of DevCpp under Windows 98 and it works perfectly so it seems to be a compilerspecific problem or something. thanks to you both who tried to help me.
    bye.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

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