Thread: so frustrated!!!!

  1. #1
    Unregistered
    Guest

    so frustrated!!!!

    i am scanning a input file and storing it to a string array.

    char *station[][MAX]={0};
    int scount=0;

    do {
    station[scount]=gets(sstr);
    scount++;
    } while (strcmp("####",sstr) != 0);

    when i print out my array.. it is overwritten with "####"

    for(i=0;i<scount;i++) {
    printf("%s\n" station[i]);
    }

    this is my output.
    ####
    ####
    ####
    ####
    ####
    ####
    ####
    ####

    i try to fix this by making my array to a 2x2 array.. but i can't get it to work correctly.. could someone help me??

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way to do it.
    Code:
    #include <string.h>
    char *station[MAX]={0};
    int scount=0;
    
    do { 
       gets(sstr);
       station[scount] = malloc(strlen(sstr)+1);
       strcpy(station[scount],sstr);
       scount++; 
    } while (strcmp("####",sstr) != 0); 
    
    //when i print out my array.. it is overwritten with "####" 
    
    for(i=0;i<scount;i++) { 
       printf("%s\n",station[i]);
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or you can do it like this and leave out the line above with malloc().

    char station[MAX][80]={0};

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It helps if you actually provide all of the information we need to assist. Where is 'sstr' defined in your program? I see nothing eluding to it. We'll assume it's a file. What's in your file?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. frustrated bout char array
    By royuco77 in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2005, 09:10 AM
  2. Getting frustrated
    By kippwinger in forum C++ Programming
    Replies: 17
    Last Post: 07-02-2003, 03:55 PM
  3. A Bit Frustrated....
    By DirX in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2003, 10:29 PM
  4. so confused and frustrated
    By ct26torr in forum C Programming
    Replies: 2
    Last Post: 02-13-2003, 10:40 PM
  5. frustrated job!
    By Cemilia in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 11-27-2002, 09:00 AM