944,141 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 510
  • C RSS
Oct 13th, 2009
0

endless loop

Expand Post »
Please help me find the endless loop and how to fix it:

Quote ...
blade71(555)% showxbits_2
32
in itox, processing 32
Test Value (b div 16): 2
Value of b: 32
Value of b (# mod 16): 0
  1. /*
  2.  * stubs for functions to study
  3.  * integer-hex conversions
  4.  *
  5.  */
  6.  
  7. #include "xbits.h"
  8.  
  9. /* Helpful function to get around not being able to use strchr()
  10.  * Converts hex to decimal value
  11.  */
  12. int hex_Val(int c)
  13. {
  14. char hex_values[] = "aAbBcCdDeEfF";
  15. int i;
  16. int answer = 0;
  17.  
  18. for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
  19. {
  20. if (hex_values[i] == c)
  21. {
  22. answer = 10 + (i/2);
  23. }
  24. }
  25.  
  26. return answer;
  27. }
  28.  
  29. /* function represents the int n as a hexstring which it places in the
  30. hexstring array */
  31.  
  32. void itox( char hexstring[], int n)
  33. {
  34. printf("in itox, processing %d\n",n);
  35. hexstring[0] = '0';
  36. hexstring[1] = 'x';
  37. int b,c=0, i=0, TEST = 0;
  38. b=n;
  39. while (b>=16)
  40. {
  41. TEST = b/16;
  42. hexstring[i+2]=TEST;
  43. i++;
  44. printf("Test Value (b div 16): %d\n", TEST);
  45. printf("Value of b: %d\n", b);
  46. b = b%16;
  47. hexstring[i+2] = b;
  48. printf("Value of b (# mod 16): %d\n", b);
  49. i++;
  50. }
  51. while ( b>=0 && b<15 )
  52. {
  53. int useless = b;
  54. if (hexstring[i+2]==10)
  55. hexstring[i+2] = 'A';
  56. else if (hexstring[i+2]==11)
  57. hexstring[i+2] = 'B';
  58. else if (hexstring[i+2]==12)
  59. hexstring[i+2] = 'C';
  60. else if (hexstring[i+2]==13)
  61. hexstring[i+2] = 'D';
  62. else if (hexstring[i+2]==14)
  63. hexstring[i+2] = 'E';
  64. else if (hexstring[i+2]==15)
  65. hexstring[i+2] = 'F';
  66. else
  67. hexstring[i+2]=hexstring[i+2];
  68. useless--;
  69. b=b-useless;
  70. }
  71. return;
  72. }
  73.  
  74. /* function converts hexstring array to equivalent integer value */
  75. int xtoi(char hexstring[])
  76. {
  77. printf("in xtoi, processing %s\n", hexstring);
  78. int answer = 0;
  79. int i = 0;
  80. int valid = 1;
  81. int hexit;
  82.  
  83. if (hexstring[i] == '0')
  84. {
  85. ++i;
  86. if (hexstring[i] == 'x' || hexstring[i] == 'X')
  87. {
  88. ++i;
  89. }
  90. }
  91. while(valid && hexstring[i] != '\0')
  92. {
  93. answer = answer * 16;
  94. if(hexstring[i] >='0' && hexstring[i] <= '9')
  95. {
  96. answer = answer + (hexstring[i] - '0');
  97. }
  98. else
  99. {
  100. hexit = hex_Val(hexstring[i]);
  101. if (hexit == 0)
  102. {
  103. valid = 0;
  104. }
  105. else
  106. {
  107. answer = answer + hexit;
  108. }
  109. }
  110. ++i;
  111. }
  112. if(!valid)
  113. {
  114. answer = 0;
  115. }
  116.  
  117. return answer;
  118. }
Similar Threads
Reputation Points: 3
Solved Threads: 1
Junior Poster in Training
Ineedhelpplz is offline Offline
57 posts
since Feb 2009
Oct 13th, 2009
1
Re: endless loop
I think first you need to simplify, so as that you can clearly follow and read it. For instance: that whole if(hexstring[i+2]==10 through 15) and then ending it with a redundantly unnecessary else hexstring[i+2]=hexstring[i+2]; , all of that could be summarized in a single line.

So crop off all the excess code and simplify; then, it should become apparent where what is happening to what.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Oct 14th, 2009
1
Re: endless loop
I suppose you've made some mistake at while loop from line 39 to 50. Value of b can't fulfill the condition to end. This line made it
b = b%16;
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Arcaiz is offline Offline
12 posts
since Oct 2009
Oct 14th, 2009
0
Re: endless loop
Please help me find the endless loop and how to fix it:



  1. /*
  2.  * stubs for functions to study
  3.  * integer-hex conversions
  4.  *
  5.  */
  6.  
  7. #include "xbits.h"
  8.  
  9. /* Helpful function to get around not being able to use strchr()
  10.  * Converts hex to decimal value
  11.  */
  12. int hex_Val(int c)
  13. {
  14. char hex_values[] = "aAbBcCdDeEfF";
  15. int i;
  16. int answer = 0;
  17.  
  18. for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
  19. {
  20. if (hex_values[i] == c)
  21. {
  22. answer = 10 + (i/2);
  23. }
  24. }
  25.  
  26. return answer;
  27. }
  28.  
  29. /* function represents the int n as a hexstring which it places in the
  30. hexstring array */
  31.  
  32. void itox( char hexstring[], int n)
  33. {
  34. printf("in itox, processing %d\n",n);
  35. hexstring[0] = '0';
  36. hexstring[1] = 'x';
  37. int b,c=0, i=0, TEST = 0;
  38. b=n;
  39. while (b>=16)
  40. {
  41. TEST = b/16;
  42. hexstring[i+2]=TEST;
  43. i++;
  44. printf("Test Value (b div 16): %d\n", TEST);
  45. printf("Value of b: %d\n", b);
  46. b = b%16;
  47. hexstring[i+2] = b;
  48. printf("Value of b (# mod 16): %d\n", b);
  49. i++;
  50. }
  51. while ( b>=0 && b<15 )
  52. {
  53. int useless = b;
  54. if (hexstring[i+2]==10)
  55. hexstring[i+2] = 'A';
  56. else if (hexstring[i+2]==11)
  57. hexstring[i+2] = 'B';
  58. else if (hexstring[i+2]==12)
  59. hexstring[i+2] = 'C';
  60. else if (hexstring[i+2]==13)
  61. hexstring[i+2] = 'D';
  62. else if (hexstring[i+2]==14)
  63. hexstring[i+2] = 'E';
  64. else if (hexstring[i+2]==15)
  65. hexstring[i+2] = 'F';
  66. else
  67. hexstring[i+2]=hexstring[i+2];
  68. useless--;
  69. b=b-useless;
  70. }
  71. return;
  72. }
  73.  
  74. /* function converts hexstring array to equivalent integer value */
  75. int xtoi(char hexstring[])
  76. {
  77. printf("in xtoi, processing %s\n", hexstring);
  78. int answer = 0;
  79. int i = 0;
  80. int valid = 1;
  81. int hexit;
  82.  
  83. if (hexstring[i] == '0')
  84. {
  85. ++i;
  86. if (hexstring[i] == 'x' || hexstring[i] == 'X')
  87. {
  88. ++i;
  89. }
  90. }
  91. while(valid && hexstring[i] != '\0')
  92. {
  93. answer = answer * 16;
  94. if(hexstring[i] >='0' && hexstring[i] <= '9')
  95. {
  96. answer = answer + (hexstring[i] - '0');
  97. }
  98. else
  99. {
  100. hexit = hex_Val(hexstring[i]);
  101. if (hexit == 0)
  102. {
  103. valid = 0;
  104. }
  105. else
  106. {
  107. answer = answer + hexit;
  108. }
  109. }
  110. ++i;
  111. }
  112. if(!valid)
  113. {
  114. answer = 0;
  115. }
  116.  
  117. return answer;
  118. }
  1. while ( b>=0 && b<15 )

this is where its getting infinite
the value of b is always >0 according to your code

i think it works fine with the following changes
if(b>0 && b <=15)
{
if ( code
replacing 10 with 'A'
:
:
15 with 'F' goes here)
else
copy as it is.
}
i dint find the use of variables "useless" and "b", so i removed.
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
115 posts
since Sep 2009
Oct 14th, 2009
0
Re: endless loop
Corrected:
  1. void itox(char hexstring[], int n)
  2. {
  3. int b = 0, TEST = 0, index =0, r[100], i=0;
  4. printf("in itox, processing %d\n",n);
  5. hexstring[i++] = '0';
  6. hexstring[i++] = 'x';
  7. for (index = 0; n>=16; index++)
  8. {
  9. r[index] = n %16;
  10. n = n/16;
  11. printf("Value of n divided by 16: %d\n",n);
  12. }
  13. r[index] = n;
  14. for (;index>=0;index--)
  15. {
  16. if (r[index] == 0)
  17. {
  18. hexstring[i] = '0';
  19. i++;
  20. }
  21. else if (r[index] == 1)
  22. {
  23. hexstring[i] = '1';
  24. i++;
  25. }
  26. else if (r[index] == 2)
  27. {
  28. hexstring[i] = '2';
  29. i++;
  30. }
  31. else if (r[index] == 3)
  32. {
  33. hexstring[i] = '3';
  34. i++;
  35. }
  36. else if (r[index] == 4)
  37. {
  38. hexstring[i] = '4';
  39. i++;
  40. }
  41. else if (r[index] == 5)
  42. {
  43. hexstring[i] = '5';
  44. i++;
  45. }
  46. else if (r[index] == 6)
  47. {
  48. hexstring[i] = '6';
  49. i++;
  50. }
  51. else if (r[index] == 7)
  52. {
  53. hexstring[i] = '7';
  54. i++;
  55. }
  56. else if (r[index] == 8)
  57. {
  58. hexstring[i] = '8';
  59. i++;
  60. }
  61. else if (r[index] == 9)
  62. {
  63. hexstring[i] = '9';
  64. i++;
  65. }
  66. else if (r[index] == 10)
  67. {
  68. hexstring[i] = 'A';
  69. i++;
  70. }
  71. else if (r[index] == 11)
  72. {
  73. hexstring[i] = 'B';
  74. i++;
  75. }
  76. else if (r[index] == 12)
  77. {
  78. hexstring[i] = 'C';
  79. i++;
  80. }
  81. else if (r[index] == 13)
  82. {
  83. hexstring[i] = 'D';
  84. i++;
  85. }
  86. else if (r[index] == 14)
  87. {
  88. hexstring[i] = 'E';
  89. i++;
  90. }
  91. else if (r[index] == 15)
  92. {
  93. hexstring[i] = 'F';
  94. i++;
  95. }
  96. else
  97. {
  98. printf("Err %d\n", r[index]);
  99. }
  100. }
  101. return;
  102. }
xtoi is now in an endless loop during the execution; because I never end hexstring[i] with '\0'. That is the next thing I will be working on
Reputation Points: 3
Solved Threads: 1
Junior Poster in Training
Ineedhelpplz is offline Offline
57 posts
since Feb 2009
Oct 14th, 2009
0
Re: endless loop
Completed entirely:
  1. blade71(60)% cat xbits.c
  2. /*
  3.  * stubs for functions to study
  4.  * integer-hex conversions
  5.  *
  6.  */
  7.  
  8. #include "xbits.h"
  9.  
  10. /* Helpful function to get around not being able to use strchr()
  11.  * Converts hex to decimal value
  12.  */
  13. int hex_Val(int c)
  14. {
  15. char hex_values[] = "aAbBcCdDeEfF";
  16. int i;
  17. int answer = 0;
  18.  
  19. for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
  20. {
  21. if (hex_values[i] == c)
  22. {
  23. answer = 10 + (i/2);
  24. }
  25. }
  26.  
  27. return answer;
  28. }
  29.  
  30. /* function represents the int n as a hexstring which it places in the
  31. hexstring array */
  32. void itox(char hexstring[], int n)
  33. {
  34. int b = 0, TEST = 0, index =0, r[100], i=0;
  35. printf("in itox, processing %d\n",n);
  36. hexstring[i++] = '0';
  37. hexstring[i++] = 'x';
  38. for (index = 0; n>=16; index++)
  39. {
  40. r[index] = n %16;
  41. n = n/16;
  42. }
  43. r[index] = n;
  44. r[index+1] = '\0';
  45. for (;index>=0;index--)
  46. {
  47. if (r[index] == 0)
  48. {
  49. hexstring[i] = '0';
  50. i++;
  51. }
  52. else if (r[index] == 1)
  53. {
  54. hexstring[i] = '1';
  55. i++;
  56. }
  57. else if (r[index] == 2)
  58. {
  59. hexstring[i] = '2';
  60. i++;
  61. }
  62. else if (r[index] == 3)
  63. {
  64. hexstring[i] = '3';
  65. i++;
  66. }
  67. else if (r[index] == 4)
  68. {
  69. hexstring[i] = '4';
  70. i++;
  71. }
  72. else if (r[index] == 5)
  73. {
  74. hexstring[i] = '5';
  75. i++;
  76. }
  77. else if (r[index] == 6)
  78. {
  79. hexstring[i] = '6';
  80. i++;
  81. }
  82. else if (r[index] == 7)
  83. {
  84. hexstring[i] = '7';
  85. i++;
  86. }
  87. else if (r[index] == 8)
  88. {
  89. hexstring[i] = '8';
  90. i++;
  91. }
  92. else if (r[index] == 9)
  93. {
  94. hexstring[i] = '9';
  95. i++;
  96. }
  97. else if (r[index] == 10)
  98. {
  99. hexstring[i] = 'A';
  100. i++;
  101. }
  102. else if (r[index] == 11)
  103. {
  104. hexstring[i] = 'B';
  105. i++;
  106. }
  107. else if (r[index] == 12)
  108. {
  109. hexstring[i] = 'C';
  110. i++;
  111. }
  112. else if (r[index] == 13)
  113. {
  114. hexstring[i] = 'D';
  115. i++;
  116. }
  117. else if (r[index] == 14)
  118. {
  119. hexstring[i] = 'E';
  120. i++;
  121. }
  122. else if (r[index] == 15)
  123. {
  124. hexstring[i] = 'F';
  125. i++;
  126. }
  127. else
  128. {
  129. printf("Err %d\n", r[index]);
  130. }
  131. }
  132. return;
  133. }
  134.  
  135. /* function converts hexstring array to equivalent integer value */
  136. int xtoi(char hexstring[])
  137. {
  138. printf("in xtoi, processing %s\n", hexstring);
  139. int answer = 0;
  140. int i = 0;
  141. int valid = 1;
  142. int hexit;
  143.  
  144. if (hexstring[i] == '0')
  145. {
  146. ++i;
  147. if (hexstring[i] == 'x' || hexstring[i] == 'X')
  148. {
  149. ++i;
  150. }
  151. }
  152. while(valid && hexstring[i] != '\0')
  153. {
  154. answer = answer * 16;
  155. if(hexstring[i] >='0' && hexstring[i] <= '9')
  156. {
  157. answer = answer + (hexstring[i] - '0');
  158. }
  159. else
  160. {
  161. hexit = hex_Val(hexstring[i]);
  162. if (hexit == 0)
  163. {
  164. valid = 0;
  165. }
  166. else
  167. {
  168. answer = answer + hexit;
  169. }
  170. }
  171. ++i;
  172. }
  173. if(!valid)
  174. {
  175. answer = 0;
  176. }
  177.  
  178. return answer;
  179. }

Desired Output:
Quote ...
blade71(108)% showxbits_2
4
in itox, processing 4
in xtoi, processing 0x4Ì
4 0x4Ì 0

128
in itox, processing 128
in xtoi, processing 0x80
128 0x80 128
32
in itox, processing 32
in xtoi, processing 0x20
32 0x20 32
64
in itox, processing 64
in xtoi, processing 0x40
64 0x40 64
2312321
in itox, processing 2312321
in xtoi, processing 0x234881
2312321 0x234881 2312321
123143536523452352643
in itox, processing -1850210173
Err -1850210173
in xtoi, processing 0x234881
-1850210173 0x234881 2312321
^[[12~^[[13~^[[14~^[[12~
Where I have marked red I was wondering why I get that funny 'I'. If someone could solve this it would be greatly appreciated! Thanks guys for all your help
Reputation Points: 3
Solved Threads: 1
Junior Poster in Training
Ineedhelpplz is offline Offline
57 posts
since Feb 2009
Oct 15th, 2009
0
Re: endless loop
Completed entirely:
  1. blade71(60)% cat xbits.c
  2. /*
  3.  * stubs for functions to study
  4.  * integer-hex conversions
  5.  *
  6.  */
  7.  
  8. #include "xbits.h"
  9.  
  10. /* Helpful function to get around not being able to use strchr()
  11.  * Converts hex to decimal value
  12.  */
  13. int hex_Val(int c)
  14. {
  15. char hex_values[] = "aAbBcCdDeEfF";
  16. int i;
  17. int answer = 0;
  18.  
  19. for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
  20. {
  21. if (hex_values[i] == c)
  22. {
  23. answer = 10 + (i/2);
  24. }
  25. }
  26.  
  27. return answer;
  28. }
  29.  
  30. /* function represents the int n as a hexstring which it places in the
  31. hexstring array */
  32. void itox(char hexstring[], int n)
  33. {
  34. int b = 0, TEST = 0, index =0, r[100], i=0;
  35. printf("in itox, processing %d\n",n);
  36. hexstring[i++] = '0';
  37. hexstring[i++] = 'x';
  38. for (index = 0; n>=16; index++)
  39. {
  40. r[index] = n %16;
  41. n = n/16;
  42. }
  43. r[index] = n;
  44. r[index+1] = '\0';
  45. for (;index>=0;index--)
  46. {
  47. if (r[index] == 0)
  48. {
  49. hexstring[i] = '0';
  50. i++;
  51. }
  52. else if (r[index] == 1)
  53. {
  54. hexstring[i] = '1';
  55. i++;
  56. }
  57. else if (r[index] == 2)
  58. {
  59. hexstring[i] = '2';
  60. i++;
  61. }
  62. else if (r[index] == 3)
  63. {
  64. hexstring[i] = '3';
  65. i++;
  66. }
  67. else if (r[index] == 4)
  68. {
  69. hexstring[i] = '4';
  70. i++;
  71. }
  72. else if (r[index] == 5)
  73. {
  74. hexstring[i] = '5';
  75. i++;
  76. }
  77. else if (r[index] == 6)
  78. {
  79. hexstring[i] = '6';
  80. i++;
  81. }
  82. else if (r[index] == 7)
  83. {
  84. hexstring[i] = '7';
  85. i++;
  86. }
  87. else if (r[index] == 8)
  88. {
  89. hexstring[i] = '8';
  90. i++;
  91. }
  92. else if (r[index] == 9)
  93. {
  94. hexstring[i] = '9';
  95. i++;
  96. }
  97. else if (r[index] == 10)
  98. {
  99. hexstring[i] = 'A';
  100. i++;
  101. }
  102. else if (r[index] == 11)
  103. {
  104. hexstring[i] = 'B';
  105. i++;
  106. }
  107. else if (r[index] == 12)
  108. {
  109. hexstring[i] = 'C';
  110. i++;
  111. }
  112. else if (r[index] == 13)
  113. {
  114. hexstring[i] = 'D';
  115. i++;
  116. }
  117. else if (r[index] == 14)
  118. {
  119. hexstring[i] = 'E';
  120. i++;
  121. }
  122. else if (r[index] == 15)
  123. {
  124. hexstring[i] = 'F';
  125. i++;
  126. }
  127. else
  128. {
  129. printf("Err %d\n", r[index]);
  130. }
  131. }
  132. return;
  133. }
  134.  
  135. /* function converts hexstring array to equivalent integer value */
  136. int xtoi(char hexstring[])
  137. {
  138. printf("in xtoi, processing %s\n", hexstring);
  139. int answer = 0;
  140. int i = 0;
  141. int valid = 1;
  142. int hexit;
  143.  
  144. if (hexstring[i] == '0')
  145. {
  146. ++i;
  147. if (hexstring[i] == 'x' || hexstring[i] == 'X')
  148. {
  149. ++i;
  150. }
  151. }
  152. while(valid && hexstring[i] != '\0')
  153. {
  154. answer = answer * 16;
  155. if(hexstring[i] >='0' && hexstring[i] <= '9')
  156. {
  157. answer = answer + (hexstring[i] - '0');
  158. }
  159. else
  160. {
  161. hexit = hex_Val(hexstring[i]);
  162. if (hexit == 0)
  163. {
  164. valid = 0;
  165. }
  166. else
  167. {
  168. answer = answer + hexit;
  169. }
  170. }
  171. ++i;
  172. }
  173. if(!valid)
  174. {
  175. answer = 0;
  176. }
  177.  
  178. return answer;
  179. }

Desired Output:


Where I have marked red I was wondering why I get that funny 'I'. If someone could solve this it would be greatly appreciated! Thanks guys for all your help

better you supply the code how you are passing the the arguments to functions or the main program
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
115 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Airline Ticket Reservations System
Next Thread in C Forum Timeline: fprintf doesn't add newline to file in cygwin





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC