Manipulation of character arrays

This is a discussion on Manipulation of character arrays within the C Programming forums, part of the General Programming Boards category; I have been having trouble with using arrays of indefinite size. I havent had a problem I think creating an ...

  1. #1
    Han
    Han is offline
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Question Manipulation of character arrays

    I have been having trouble with using arrays of indefinite size.
    I havent had a problem I think creating an array of unknown size but I have had problems ending the program. I have noticed that others have had similiar problems but I am still unsure as to how I should go about fixing the problem.

    If I were to calculate the sum of an array of size known I would do this and this works:

    for(z=0;x<10;x++){
    scanf("%s",temp_array);
    total=atoi(temp_array);
    sum=sum+total;
    }
    printf("the sum of these numbers is %i\n"sum);
    return 0;
    }


    this is fine for 10 numbers in the array, it will find the sum of ten numbers and terminate.
    However what I want to know is what if you wanted to find 20 30 or even 40 numbers and terminate when you type in a string.
    so if you were to type in 1 2 3 4 5 6 and then end it would find the sum of the 6 numbers you put in.
    What I need is something that terminates the loop when a string of characters or a character is typed in. I have tried lots of things but it just hangs telling me that it is ignoring the code that I have put in. Can anybody help me.
    I need to change the above code to handle this and I think it involves putting in a loop either while or for but I dont know how to implement it

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I'm not very good with scanf but I'll give it a try:

    Code:
    int main()
    {
    	int sum = 0;
    	int number;
    
    	while(scanf("%d*%c", &number))
    	{
    		sum += number;
    	}
    
    	printf("the sum of these numbers is %d\n", sum); 
    	return 0; 
    }

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You can create an array dynamically by using malloc and free the memory with free when you don't need it anymore.

    Code:
    int *array;
    int size_of_array;
    
    size_of_array = calculate_size ();
    
    array = malloc (sizeof (int) * size_of_array);
    
    free (array);

  4. #4
    Han
    Han is offline
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    could you say something like this:

    while(std_array[z]!='\0'){
    for(z=0;sizeof(std_array);z++{
    total=atoi(std_array);
    sum=sum+total;
    }
    if(std_array[z]='\0' /*this represents the end of a string*/
    {
    printf("terminate loop:\n");
    }

    } /*end the while loop*/

    I havent tried this and it probably doesnt work as I am new to C but is there something like this that would work.
    I am trying to work around this null character thing to get this to work.
    basically what I am trying to say is if it isnt null then continue to loop but when it is null then terminate.
    I am thinking though this isnt going to solve my problem of when you enter a character it is going to carry out the sum function that it is supposed to.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Am I correct that you are trying to do the following:

    1. The user enters numbers
    2. You read in the numbers as strings and convert them to int
    3. You calculate the sum of all ints

    ?

    Perhaps you could try something like this:

    Code:
    stop = 0;
    
    while (stop != 1)
    { 
        scanf ("%s",temp_array); 
        
        if (strcmp (temp_array, "stop") == 0)
        {
            stop = 1;
        }
        else
        {
            total = atoi (temp_array); 
            sum = sum+total; 
        }
    } 
    
    printf("the sum of these numbers is %d\n", sum);
    The user enters numbers. If the user enters the word stop, then the loop ends. I've not tested this and probably there are better solutions, but you can give it a try.

    The point where it is all about in my example is that you should introduce a check variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  3. multidimentional character arrays
    By ckeener in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2005, 08:49 PM
  4. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  5. Replies: 5
    Last Post: 09-19-2003, 04:47 AM

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