周龙 wrote:
> I write this :
>    
>   #include <stdio.h>
> #include <stdlib.h>
> #include<string.h>
>    
>   int main()
> {
>  char string1[101];
>  char string2[101];
>  int number1[101]={0};
>  int number2[101]={0};
>  char variable1;
>  char variable2;
>  int counter1;
>  int counter2;
>  int length1,length2;
>    
>    printf("enter a number: ");
>  scanf("%s",string1);
>  printf("enter a number: ");
>  scanf("%s",string2);
>    
>    length1 = strlen(string1);
>  length2 = strlen(string2);
>    
>    for (counter1 = 0;counter1 <= length1-1;counter1++){
>   variable1 = string1[counter1];
>   number1[counter1] = atoi(&variable1);
>   printf("%d",number1[counter1]);
>  }
>  printf("\n");
>   
>  for (counter2 = 0;counter2 <= length2-1;counter2++){
>   variable2 = string2[counter2];
>   number2[counter2] = atoi(&variable2);
>   printf("%d",number2[counter2]);
>  }
>  printf("\n");
>    
>    return 0;
>   }
> 
>   ----------------------
>   I entered 111 and 222, then the out put is 111 and 212121. But if I delete 
> one of the two "for", it work on. Only when this two "for" get together, it 
> will be wrong.
>   I don't know why

That would be because Brett forgot his morning coffee yesterday and made
at least two mistakes in his suggestions.

  length1 = strlen(string1);

  for (counter1 = 0; counter1 < length1; counter1++)
  {
    number1[counter1] = string1[counter1] - '0';
    printf("%d", number1[counter1]);
  }
  printf("\n");

Try that instead.  What you had before was a stack overflow.  It was a
miracle the program didn't crash.  This is the sort of thing that makes
it important to use Safe C++ across the board.  That way we won't need
coffee.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to