Code not working

This is a discussion on Code not working within the C++ Programming forums, part of the General Programming Boards category; No it won't. You will have to do something like this. Code: if (file.is_open()) { while (file.getline( array, sizeof(array) )) ...

  1. #16
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No it won't. You will have to do something like this.
    Code:
        if (file.is_open()) {
            while (file.getline( array, sizeof(array) )) {
                char * ch = array;
                while((ch = strstr( ch, "hello james")) != 0) {
                     count++;
                     ch++;
                }
            }
         }
    Kurt
    Last edited by ZuK; 02-17-2006 at 01:30 PM.

  2. #17
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by ZuK
    No it won't. You will have to do something like this.
    Code:
        if (file.is_open()) {
            while (file.getline( array, sizeof(array) )) {
                char * ch = array;
                while((ch = strstr( ch, "hello james")) != 0)
                     count++;
                     ch++;
                }
            }
         }
    Kurt
    Thanks how come the pointer doesn't have the new and delete key words? and what does the ch variable do?

    Is this how the code would be developed to work with other words?

    Code:
       
     if (file.is_open()) {
            while (file.getline( array, sizeof(array) )) {
                char * ch = array;
                while((ch = strstr( ch, "hello james")) != 0)
                     count++;
                     ch++;
            
    //uses same char pointer?
    while((ch = strstr( ch, "hello david")) != 0)
                     count++;
                     ch++;
    Thanks.

  3. #18
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by 182
    Is this how the code would be developed to work with other words?
    Code:
       
     if (file.is_open()) {
            while (file.getline( array, sizeof(array) )) {
                char * ch = array;
                while((ch = strstr( ch, "hello james")) != 0) {
                     count++;
                     ch++;
                }
    //uses same char pointer?
    //yes the pointer can be recycled but it has to be reset to the beginning 
    //of the array before the next string can be searched
               ch = array;
               while((ch = strstr( ch, "hello david")) != 0) {
                     count++;
                     ch++;
              }
        }
    }
    >> how come the pointer doesn't have the new and delete key words? and what does the ch variable do?

    ch is just a helper variable the points to where stsstr() should start searching.
    No space was allocated for ch so it doesn't have to be deleted.
    Kurt
    Last edited by ZuK; 02-17-2006 at 01:29 PM.

  4. #19
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for that and sorry if these newbie questions are annoying but how come the ch variable is incremented as well as the count variable?

    Thanks again.

  5. #20
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    by incrementing the pointer ch it will point to the character after the position where the string was found. Otherwise it would not stop to find the same location over and over again.
    Kurt

  6. #21
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I see, thanks

    So even with the pointer variable is it still ok to keep the old code and just close and open the file again with the same ifstream variable and add this new code or would I need a new ifstream variable?

  7. #22
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It's no problem to recycle variables. Works fine with streams if you properly close them before opening them again. That has noting to do with what you do with the data that you read from the stream.
    I really don't understand what you are worried about.
    Kurt

  8. #23
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks again for all your help

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 07:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

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