Thread: Trying to create a simple array program

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    18

    Trying to create a simple array program

    Hi all.

    Okay so first some explanation of the program.

    1. When user input some text and press enter, the program saves the array and the newline. The chars entered should be less than 10 including the newline.

    2. The input is saved into a storage that contains a maximum of 1000 chars.

    3. The program print the storage. The storage should look like this

    Code:
    array1
    array2
    array3
    array4
    and so on..
    Here's what I got so far
    Code:
    #include <stdio.h>
    
    int main()
    {
        
        char buffer[10];
        char storage[1000];
        int index = 0;
        int holder;
        int i = 0;
    
        holder = getchar();
        while (holder != EOF)
        {
            buffer[index] = holder;
            index++;
            
            if (holder == '\n')
            {
                for (i = 0; i < 1000; i++)
                {
                    storage[i] = buffer[i];
                }
            }
        }
        
        storage[i] = '\0';
        
        printf("%s", storage);
    }
    The program stops working after i input some text.

    Any idea?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,773
    buffer is only 10 chars long; you are trying to copy 1000 chars from it. This results in undefined behavior.

    Edit: And, the line below; write a zero byte beyond the end of the storage since i = 1000 is past the end.
    Code:
    storage[i] = '\0';
    Hint: Lookup fgets and strcpy functions.
    This problem is likely designed to use fgets() and might be designed to use one of the str... functions.

    Tim S.
    Last edited by stahta01; 09-16-2017 at 12:19 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    buffer is only 10 chars long; you are trying to copy 1000 chars from it. This results in undefined behavior.

    Edit: And, the line below; write a zero byte beyond the end of the storage since i = 1000 is past the end.
    Code:
    storage[i] = '\0';
    Hint: Lookup fgets and strcpy functions.
    This problem is likely designed to use fgets() and might be designed to use one of the str... functions.

    Tim S.
    I want to avoid using any str functions if possible.

    Anyway here's I am now
    Code:
    #include <stdio.h>
    
    int main()
    {
        char buffer[10];
        char storage[1000];
        int index = 0;
        int holder;
        int i = 0;
        int newidx = 0;
    
        while((holder = getchar()) != EOF)
        {
            buffer[index] = holder;
            index++;
            if (holder = '\n')
            {
                buffer[index] = holder;
                for (i = 0; i < index; i++ )
                {
                    storage[i + newidx] = buffer[i];
                }
            newidx = i + newidx;
            index = 0;
            }
        }
        storage[newidx] = '\0';    
        printf("%s", storage);   
    }
    It works but there's one problem. The buffer should not accept more than 10 chars. For example, if I input "123456789ab", 'b' should be omitted because it exceeds 10 chars(I might be wrong here, please correct me). But it does not work like that.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    UPDATE:

    So I have restructured my code and it's working.

    Code:
    #include <stdio.h>
    
    int main()
    {
        char buffer[10];
        char storage[1000];
        int index = 0;
        int holder;
        int i = 0;
        int newidx = 0;
    
        
        while((holder = getchar()) != EOF) //while the input is not = end of file. holder will only hold one char every reading
        {
            if (holder == '\n') //if the input is = newline character
            {
                if (index > 10) //if the index is more than 10, we set the index to a max of 10 only
                {
                    index = 10;
                }
                
                if (index <= 10) //this code is executed because index is now less or equal to 10
                {
                buffer[index] = holder; //the last index should equal to newline because we want to store the newline per char array
                    for (i = 0; i <= index; i++ )
                    {
                        storage[i + newidx] = buffer[i]; //this code will always store buffer[] into the last newidx value of storage[]
                    }    
                newidx = i + newidx; //update the newidx value
                index = 0; //clear the index as we want to receive new char array again
                }
            }        
            
            else //as long the first input is other than newline, this part will get executed first            
            { 
                buffer[index] = holder; //every char is put into buffer[] except newline
                index++;
            }
        }
        
        storage[newidx] = '\0'; //char array is always terminated with NULL to indicate end of string
        printf("%s", storage); //print everything inside storage[]. it is always printed from the first which is [0]
    }
    But I would like to hear comments from folks.

  5. #5
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    This program is wacky. Just from a user's perspective, I just built it with GCC and ran it and first of all, I typed more than 10 chars and it crashed. I'm guessing segfault but didn't formally check.

    Secondly, even when I did conform to the less than 10 char guideline, it seems to just collect input forever and never displays it. Maybe it does, but eve if I enter a blank line, it keeps trying to get lines... Is this how you intended it to work?

    You need to add some sort of check to make sure that the input is 10 or less lines and if not, discard it and perhaps warn the user rather than allow the program to crash like that. Even if that's not part of the assignment, nobody wants a program to segfault like that.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Thanks Asymptotic. I really value your thoughts. But it didn't crash(yet) for me? Nevermind anyway, I will try until it crashes.

    Secondly, I never set any spec for this program. What I wanted to do is to have the user input some text which is less than 10 chars, press enter(newline) to input next text and press EOF to display all input text in the form of

    Code:
    text1
    text2
    text3
    What I wanted to learn is how to store a char array("buffer" as in program) to another char array("storage" as in program). But "buffer" must be stored into the consecutive space of "storage". And the program did.

    That's all I wanted for now.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    mmmm somethig like this perhaps
    Code:
    #include <stdio.h>
    int main (int argc, char **argv)
    {
    
    char store[10];
    char hold[1000];
    int i = 0 , j = 0, k = 0;
    
    while( i  < 4)
    {
    
        //store whatever
        for(j = 0 ; j <4; j++)
        {
            store[j] = getchar();
            printf("%c\n", store[j]);
            printf("j = %d\n",  j);
        }
     
    //shove it into the holding array
         hold[i] = store[10];
        printf("i = %d\n", i );
        i++;
    }
    //print it out
    int e = 0; 
    for ( e = 0; e < 5; e++)
    {
    
            printf("%d\n", e);
    }
    
    
    return 0;
    }
    you have to fill each array1[10] first then put that into another array2[1000]; when full or on demand then print it out. inner loops controls the 10 lenth limit for each array, the outer loop controls the max limit of 1000 elements of storage[10].

    if using the same array which you should/could, you'll need to clear it first before using it again.

    that array[10] holds the input off cli then the other array[1000] holds the the input 10 count ch length each.

    Code:
    array1[1000];
    array2[10]
    
    fill array2[10] 
    
    assign it to array1 elements
    
    array1[0] = arrary2[10]
    
    clear array2[10]
    
    for (i = 0 ; i < array2.length; i++)
        array2[i] = 0;
    
    fill array2[10] again
    
    assign it to the next element 
    array1[1] = array2[10]
    
    repeat until finished
    
    then print it out when done/ or on demand
    that may or may not work because it is using the same array,
    I'm not sure of the the values what would be in the array1 element will be as expected. but that is the basic logic behind it.

    two loops:
    Code:
    int j = 0, k = 0;
    
    while (k < 4)
    {
        for (j = 0; j < 5; j++)
        {
            printf("in j = %d\n", j);
        }
        printf("while k = %d\n",k);
        k++;
    }
    printf("out of while\n");
    Last edited by userxbw; 09-21-2017 at 04:19 PM.

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Thanks @userxbw.

    Your example works great too! Eventhough mine is good enough for now, yours can be used as future reference.

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by cyberjupiter View Post
    Thanks @userxbw.

    Your example works great too! Even though mine is good enough for now, yours can be used as future reference.
    #4 you can live with that?
    run this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char buffer[10];
        char storage[1000];
        int index = 0;
        int holder;
        int i = 0;
        int newidx = 0;
    
    
        while((holder = getchar()) != EOF) //while the input is not = end of file. holder will only hold one char every reading
        {
            if (holder == '\n') //if the input is = newline character
            {
                if (index > 10) //if the index is more than 10, we set the index to a max of 10 only
                {
                    index = 10;
                }
    
                if (index <= 10) //this code is executed because index is now less or equal to 10
                {
                    buffer[index] = holder; //the last index should equal to newline because we want to store the newline per char array
                    for (i = 0; i <= index; i++ )
                    {
                        printf("newidx %d, i %d , index %d \n", newidx, i, index);
                        storage[i + newidx] = buffer[i]; //this code will always store buffer[] into the last newidx value of storage[]
                    }
                newidx = i + newidx; //update the newidx value
                index = 0; //clear the index as we want to receive new char array again
                }
            }
    
            else //as long the first input is other than newline, this part will get executed first
            {
                buffer[index] = holder; //every char is put into buffer[] except newline
                index++;
                printf("index %d \n", index);
            }
        }
    
        storage[newidx] = '\0'; //char array is always terminated with NULL to indicate end of string
        printf("%s", storage); //print everything inside storage[]. it is always printed from the first which is [0]
    }
    look at your values and see what your index is doing then look at your code again.
    Code:
    index 119 
    newidx 1717660002, i 0 , index 10 
    Segmentation fault
    Last edited by userxbw; 09-22-2017 at 03:52 PM.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    something to start with:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char buffer;
        char storage[10];
         int keepCount = 0, i = 0;
    
        while( keepCount <  (sizeof(storage)/sizeof(storage[0]) ) )
        {
                buffer = getchar();
                printf(" you did this ");
                storage[keepCount] = buffer;
                putchar(buffer);
                printf("\n");
                keepCount++;
    
        } // end while
    
        for ( i = 0; i < 10; i++)
            printf("%c \n", storage[i]);
    
        return 0 ;
    }
    output
    Code:
    userx@~/bin <> gcc -Wall buffy.c
    userx@~/bin <> ./a.out
    123456789101112131415
     you did this 1
     you did this 2
     you did this 3
     you did this 4
     you did this 5
     you did this 6
     you did this 7
     you did this 8
     you did this 9
     you did this 1
    1 
    2 
    3 
    4 
    5 
    6 
    7 
    8 
    9 
    1 
    userx@~/bin <>
    Last edited by userxbw; 09-22-2017 at 05:06 PM.

  11. #11
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Well then you didn't clearly understand what I was trying to do. Your last example does not meet the spec I stated in my #1 post.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by cyberjupiter View Post
    Well then you didn't clearly understand what I was trying to do. Your last example does not meet the spec I stated in my #1 post.
    if it worked properly I'd not be able to blow it up. you're not checking for over flow on your char[1000], your index++ is not working correctly. look
    your code again adding something to show and tell something else as well.
    what ever it is it is not working properly,

    Code:
    #include <stdio.h>
    
    int main()
    {
        char buffer[10];
        char storage[10];
        int index = 0;
        int holder;
        int i = 0;
        int newidx = 0;
    
    
        while((holder = getchar()) != EOF) //while the input is not = end of file. holder will only hold one char every reading
        {
            if (holder == '\n') //if the input is = newline character
            {
                if (index > 10) //if the index is more than 10, we set the index to a max of 10 only
                {
                    index = 10;
                }
    
                if (index <= 10) //this code is executed because index is now less or equal to 10
                {
                    buffer[index] = holder; //the last index should equal to newline because we want to store the newline per char array
                    for (i = 0; i <= index; i++ )
                    {
                        printf("newidx %d, i %d , index %d \n", newidx, i, index);
                        storage[i + newidx] = buffer[i]; //this code will always store buffer[] into the last newidx value of storage[]
                    }
                newidx = i + newidx; //update the newidx value
                index = 0; //clear the index as we want to receive new char array again
                }
            }
    
            else //as long the first input is other than newline, this part will get executed first
            {
                buffer[index] = holder; //every char is put into buffer[] except newline
                index++;
                printf("index %d \n", index);
            }
            }
       storage[newidx] = '\0'; //char array is always terminated with NULL to indicate end of string
        printf("Storage: %s", storage); //print everything inside storage[]. it is always printed from the first which is [0]
        }
    
        storage[newidx] = '\0'; //char array is always terminated with NULL to indicate end of string
        printf("Not mine storage:  %s", storage); //print everything inside storage[]. it is always printed from the first which is [0]
    
    
        return 0;
    }
    storage never gets printed?
    taking it down to 10 to show you something.
    output look at index?

    if you ever reached that 1000 it'd blow up.

    and your last two lines of code are in the wrong place that is why they never get printed out.


    Code:
    userx@~/bin <> ./a.out
    12345678901234567890
    index 1 
    index 2 
    index 3 
    index 4 
    index 5 
    index 6 
    index 7 
    index 8 
    index 9 
    index 10 
    index 11 
    index 12 
    index 13 
    index 14 
    index 15 
    index 16 
    index 17 
    index 18 
    index 19 
    index 20 
    newidx 0, i 0 , index 10 
    newidx 0, i 1 , index 10 
    newidx 0, i 2 , index 10 
    newidx 0, i 3 , index 10 
    newidx 0, i 4 , index 10 
    newidx 0, i 5 , index 10 
    newidx 0, i 6 , index 10 
    newidx 0, i 7 , index 10 
    newidx 0, i 8 , index 10 
    newidx 0, i 9 , index 10 
    newidx 0, i 10 , index 10 
    ^C
    userx@~/bin <> gcc -Wall buffynotgood.c
    userx@~/bin <> ./a.out
    123456789012345678901234567890
    index 1 
    Storage: index 2 
    Storage: index 3 
    Storage: index 4 
    Storage: index 5 
    Storage: index 6 
    Storage: index 7 
    Storage: index 8 
    Storage: index 9 
    Storage: index 10 
    Storage: index 11 
    Storage: index 12 
    Storage: index 13 
    Storage: index 14 
    Storage: index 15 
    Storage: index 16 
    Storage: index 17 
    Storage: index 18 
    Storage: index 19 
    Storage: index 20 
    Storage: index 21 
    Storage: index 22 
    Segmentation fault
    userx@~/bin <>

  13. #13
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    The sad news for you, storage does get printed out using my code.

    Look, I have stated in my first post this is a "simple array program". Re-read #6. I never intended to check for overflow in the first place.

    And again, your last post is not even doing what I expected but mine did
    My harcdoed Arduino LCD library purely in C,
    https://github.com/cyberjupiter/Project-LCD_Rewrite

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    something more like this but you still got to figure out how to get a array[size] inside another array element

    Code:
    char array1[10];
    char array2 [10];
    
    array2[0] = array1[0];
    arrary2[1] = array1[1];
    
    getchar only gets one char at a time and not a string to fill 
    
    array2[0] = array1[0] = 'bob';
    Code:
    #include <stdio.h>
    /*
     *
    
    Join Date
        Sep 2017
    Posts
        14
    
    Trying to create a simple array program
    
        Hi all.
    
        Okay so first some explanation of the program.
    
        1. When user input some text and press enter, the program saves the array and the newline. The chars entered should be less than 10 including the newline.
    
        2. The input is saved into a storage that contains a maximum of 1000 chars.
    
        3. The program print the storage. The storage should look like this
        * **/
    
    
    int main()
    {
    char c;
    char holdme[5];
    
    int i = 0, count = 0, check = 0, move1atatime = 0;
    
    while ( move1atatime < ( sizeof(holdme)/sizeof(holdme[0]) )  )
    {
             c  = getchar();
    
        if ( check != 4 )
        {
                holdme[move1atatime] = c;
                holdme[move1atatime + 1] = '\0'; // to allow 10th to be \0
                check++;
        }
        else
        {
            check = 0; // fills each element up to 10 --1 for eof
            move1atatime++; // moves  element limit is sizeof(array)
            printf(" count %d move1atatime %d \n",  count, move1atatime);
        }
    }
    
    for ( i = 0; i < sizeof(holdme)/sizeof(holdme[0] ) ;  i++)
        printf("%c \n ", holdme[i]);
    
    
    return 0;
    }

  15. #15
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this one does exactly MORE then what you want because it does not over running the arrays. All that needs be done is ........ , and,it is using only one line with a lot less code then yours.

    Trying to create a simple array program

    Hi all.

    Okay so first some explanation of the program.

    1. When user input some text and press enter, the program saves the array and the newline. The chars entered should be less than 10 including the newline.

    2. The input is saved into a storage that contains a maximum of 1000 chars.

    3. The program print the storage. The storage should look like this
    Code:
    array1
    array2
    array3
    array4
    and so on..
    Code:
    #include <stdio.h>
    #include <string.h>
    int main() {char cheery[10]; char *holdme[5]; int i = 0, move1atatime = 0; while ( move1atatime < ( sizeof(holdme)/sizeof(holdme[0]))) { scanf("%9s" ,cheery);  holdme[move1atatime] =  strdup (cheery); move1atatime++; } for ( i = 0; i < sizeof(holdme)/sizeof(holdme[0] ) ;  i++) printf("\ncount %d :: %s \n ", i, holdme[i] ); return 0; }
    Results

    Code:
     userx@~/bin <> ./a.out 
    abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
    
    count 0 :: abcdefghi 
     
    count 1 :: jklmnopqr 
     
    count 2 :: stuvwxyza 
     
    count 3 :: bcdefghij 
     
    count 4 :: klmnopqrs 
     userx@~/bin <>
    see it does not blow up if you over run it with chars.
    now
    post your output off your program.
    Last edited by userxbw; 09-22-2017 at 10:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 09-03-2013, 12:20 PM
  2. create a simple program using linked list
    By tillu in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2011, 12:53 AM
  3. Simple Array Program
    By Chemeng11 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2011, 08:09 PM
  4. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  5. create a dll for C++ simple program and call from VB
    By motiz in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2008, 04:54 AM

Tags for this Thread