| | |
endless loop
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 57
Reputation:
Solved Threads: 1
Please help me find the endless loop and how to fix it:
•
•
•
•
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
C Syntax (Toggle Plain Text)
/* * stubs for functions to study * integer-hex conversions * */ #include "xbits.h" /* Helpful function to get around not being able to use strchr() * Converts hex to decimal value */ int hex_Val(int c) { char hex_values[] = "aAbBcCdDeEfF"; int i; int answer = 0; for (i=0; answer == 0 && hex_values[i] != '\0'; i++) { if (hex_values[i] == c) { answer = 10 + (i/2); } } return answer; } /* function represents the int n as a hexstring which it places in the hexstring array */ void itox( char hexstring[], int n) { printf("in itox, processing %d\n",n); hexstring[0] = '0'; hexstring[1] = 'x'; int b,c=0, i=0, TEST = 0; b=n; while (b>=16) { TEST = b/16; hexstring[i+2]=TEST; i++; printf("Test Value (b div 16): %d\n", TEST); printf("Value of b: %d\n", b); b = b%16; hexstring[i+2] = b; printf("Value of b (# mod 16): %d\n", b); i++; } while ( b>=0 && b<15 ) { int useless = b; if (hexstring[i+2]==10) hexstring[i+2] = 'A'; else if (hexstring[i+2]==11) hexstring[i+2] = 'B'; else if (hexstring[i+2]==12) hexstring[i+2] = 'C'; else if (hexstring[i+2]==13) hexstring[i+2] = 'D'; else if (hexstring[i+2]==14) hexstring[i+2] = 'E'; else if (hexstring[i+2]==15) hexstring[i+2] = 'F'; else hexstring[i+2]=hexstring[i+2]; useless--; b=b-useless; } return; } /* function converts hexstring array to equivalent integer value */ int xtoi(char hexstring[]) { printf("in xtoi, processing %s\n", hexstring); int answer = 0; int i = 0; int valid = 1; int hexit; if (hexstring[i] == '0') { ++i; if (hexstring[i] == 'x' || hexstring[i] == 'X') { ++i; } } while(valid && hexstring[i] != '\0') { answer = answer * 16; if(hexstring[i] >='0' && hexstring[i] <= '9') { answer = answer + (hexstring[i] - '0'); } else { hexit = hex_Val(hexstring[i]); if (hexit == 0) { valid = 0; } else { answer = answer + hexit; } } ++i; } if(!valid) { answer = 0; } return answer; }
1
#2 Oct 13th, 2009
I think first you need to simplify, so as that you can clearly follow and read it. For instance: that whole
So crop off all the excess code and simplify; then, it should become apparent where what is happening to what.
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
0
#4 Oct 14th, 2009
•
•
•
•
Please help me find the endless loop and how to fix it:
C Syntax (Toggle Plain Text)
/* * stubs for functions to study * integer-hex conversions * */ #include "xbits.h" /* Helpful function to get around not being able to use strchr() * Converts hex to decimal value */ int hex_Val(int c) { char hex_values[] = "aAbBcCdDeEfF"; int i; int answer = 0; for (i=0; answer == 0 && hex_values[i] != '\0'; i++) { if (hex_values[i] == c) { answer = 10 + (i/2); } } return answer; } /* function represents the int n as a hexstring which it places in the hexstring array */ void itox( char hexstring[], int n) { printf("in itox, processing %d\n",n); hexstring[0] = '0'; hexstring[1] = 'x'; int b,c=0, i=0, TEST = 0; b=n; while (b>=16) { TEST = b/16; hexstring[i+2]=TEST; i++; printf("Test Value (b div 16): %d\n", TEST); printf("Value of b: %d\n", b); b = b%16; hexstring[i+2] = b; printf("Value of b (# mod 16): %d\n", b); i++; } while ( b>=0 && b<15 ) { int useless = b; if (hexstring[i+2]==10) hexstring[i+2] = 'A'; else if (hexstring[i+2]==11) hexstring[i+2] = 'B'; else if (hexstring[i+2]==12) hexstring[i+2] = 'C'; else if (hexstring[i+2]==13) hexstring[i+2] = 'D'; else if (hexstring[i+2]==14) hexstring[i+2] = 'E'; else if (hexstring[i+2]==15) hexstring[i+2] = 'F'; else hexstring[i+2]=hexstring[i+2]; useless--; b=b-useless; } return; } /* function converts hexstring array to equivalent integer value */ int xtoi(char hexstring[]) { printf("in xtoi, processing %s\n", hexstring); int answer = 0; int i = 0; int valid = 1; int hexit; if (hexstring[i] == '0') { ++i; if (hexstring[i] == 'x' || hexstring[i] == 'X') { ++i; } } while(valid && hexstring[i] != '\0') { answer = answer * 16; if(hexstring[i] >='0' && hexstring[i] <= '9') { answer = answer + (hexstring[i] - '0'); } else { hexit = hex_Val(hexstring[i]); if (hexit == 0) { valid = 0; } else { answer = answer + hexit; } } ++i; } if(!valid) { answer = 0; } return answer; }
C Syntax (Toggle Plain Text)
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.
Minds are like parachutes - they only work when they are open
Gaiety
Gaiety
•
•
Join Date: Feb 2009
Posts: 57
Reputation:
Solved Threads: 1
0
#5 Oct 14th, 2009
Corrected:
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
C Syntax (Toggle Plain Text)
void itox(char hexstring[], int n) { int b = 0, TEST = 0, index =0, r[100], i=0; printf("in itox, processing %d\n",n); hexstring[i++] = '0'; hexstring[i++] = 'x'; for (index = 0; n>=16; index++) { r[index] = n %16; n = n/16; printf("Value of n divided by 16: %d\n",n); } r[index] = n; for (;index>=0;index--) { if (r[index] == 0) { hexstring[i] = '0'; i++; } else if (r[index] == 1) { hexstring[i] = '1'; i++; } else if (r[index] == 2) { hexstring[i] = '2'; i++; } else if (r[index] == 3) { hexstring[i] = '3'; i++; } else if (r[index] == 4) { hexstring[i] = '4'; i++; } else if (r[index] == 5) { hexstring[i] = '5'; i++; } else if (r[index] == 6) { hexstring[i] = '6'; i++; } else if (r[index] == 7) { hexstring[i] = '7'; i++; } else if (r[index] == 8) { hexstring[i] = '8'; i++; } else if (r[index] == 9) { hexstring[i] = '9'; i++; } else if (r[index] == 10) { hexstring[i] = 'A'; i++; } else if (r[index] == 11) { hexstring[i] = 'B'; i++; } else if (r[index] == 12) { hexstring[i] = 'C'; i++; } else if (r[index] == 13) { hexstring[i] = 'D'; i++; } else if (r[index] == 14) { hexstring[i] = 'E'; i++; } else if (r[index] == 15) { hexstring[i] = 'F'; i++; } else { printf("Err %d\n", r[index]); } } return; }
•
•
Join Date: Feb 2009
Posts: 57
Reputation:
Solved Threads: 1
0
#6 Oct 14th, 2009
Completed entirely:
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
C Syntax (Toggle Plain Text)
blade71(60)% cat xbits.c /* * stubs for functions to study * integer-hex conversions * */ #include "xbits.h" /* Helpful function to get around not being able to use strchr() * Converts hex to decimal value */ int hex_Val(int c) { char hex_values[] = "aAbBcCdDeEfF"; int i; int answer = 0; for (i=0; answer == 0 && hex_values[i] != '\0'; i++) { if (hex_values[i] == c) { answer = 10 + (i/2); } } return answer; } /* function represents the int n as a hexstring which it places in the hexstring array */ void itox(char hexstring[], int n) { int b = 0, TEST = 0, index =0, r[100], i=0; printf("in itox, processing %d\n",n); hexstring[i++] = '0'; hexstring[i++] = 'x'; for (index = 0; n>=16; index++) { r[index] = n %16; n = n/16; } r[index] = n; r[index+1] = '\0'; for (;index>=0;index--) { if (r[index] == 0) { hexstring[i] = '0'; i++; } else if (r[index] == 1) { hexstring[i] = '1'; i++; } else if (r[index] == 2) { hexstring[i] = '2'; i++; } else if (r[index] == 3) { hexstring[i] = '3'; i++; } else if (r[index] == 4) { hexstring[i] = '4'; i++; } else if (r[index] == 5) { hexstring[i] = '5'; i++; } else if (r[index] == 6) { hexstring[i] = '6'; i++; } else if (r[index] == 7) { hexstring[i] = '7'; i++; } else if (r[index] == 8) { hexstring[i] = '8'; i++; } else if (r[index] == 9) { hexstring[i] = '9'; i++; } else if (r[index] == 10) { hexstring[i] = 'A'; i++; } else if (r[index] == 11) { hexstring[i] = 'B'; i++; } else if (r[index] == 12) { hexstring[i] = 'C'; i++; } else if (r[index] == 13) { hexstring[i] = 'D'; i++; } else if (r[index] == 14) { hexstring[i] = 'E'; i++; } else if (r[index] == 15) { hexstring[i] = 'F'; i++; } else { printf("Err %d\n", r[index]); } } return; } /* function converts hexstring array to equivalent integer value */ int xtoi(char hexstring[]) { printf("in xtoi, processing %s\n", hexstring); int answer = 0; int i = 0; int valid = 1; int hexit; if (hexstring[i] == '0') { ++i; if (hexstring[i] == 'x' || hexstring[i] == 'X') { ++i; } } while(valid && hexstring[i] != '\0') { answer = answer * 16; if(hexstring[i] >='0' && hexstring[i] <= '9') { answer = answer + (hexstring[i] - '0'); } else { hexit = hex_Val(hexstring[i]); if (hexit == 0) { valid = 0; } else { answer = answer + hexit; } } ++i; } if(!valid) { answer = 0; } return answer; }
Desired Output:
•
•
•
•
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~
0
#7 Oct 15th, 2009
•
•
•
•
Completed entirely:
C Syntax (Toggle Plain Text)
blade71(60)% cat xbits.c /* * stubs for functions to study * integer-hex conversions * */ #include "xbits.h" /* Helpful function to get around not being able to use strchr() * Converts hex to decimal value */ int hex_Val(int c) { char hex_values[] = "aAbBcCdDeEfF"; int i; int answer = 0; for (i=0; answer == 0 && hex_values[i] != '\0'; i++) { if (hex_values[i] == c) { answer = 10 + (i/2); } } return answer; } /* function represents the int n as a hexstring which it places in the hexstring array */ void itox(char hexstring[], int n) { int b = 0, TEST = 0, index =0, r[100], i=0; printf("in itox, processing %d\n",n); hexstring[i++] = '0'; hexstring[i++] = 'x'; for (index = 0; n>=16; index++) { r[index] = n %16; n = n/16; } r[index] = n; r[index+1] = '\0'; for (;index>=0;index--) { if (r[index] == 0) { hexstring[i] = '0'; i++; } else if (r[index] == 1) { hexstring[i] = '1'; i++; } else if (r[index] == 2) { hexstring[i] = '2'; i++; } else if (r[index] == 3) { hexstring[i] = '3'; i++; } else if (r[index] == 4) { hexstring[i] = '4'; i++; } else if (r[index] == 5) { hexstring[i] = '5'; i++; } else if (r[index] == 6) { hexstring[i] = '6'; i++; } else if (r[index] == 7) { hexstring[i] = '7'; i++; } else if (r[index] == 8) { hexstring[i] = '8'; i++; } else if (r[index] == 9) { hexstring[i] = '9'; i++; } else if (r[index] == 10) { hexstring[i] = 'A'; i++; } else if (r[index] == 11) { hexstring[i] = 'B'; i++; } else if (r[index] == 12) { hexstring[i] = 'C'; i++; } else if (r[index] == 13) { hexstring[i] = 'D'; i++; } else if (r[index] == 14) { hexstring[i] = 'E'; i++; } else if (r[index] == 15) { hexstring[i] = 'F'; i++; } else { printf("Err %d\n", r[index]); } } return; } /* function converts hexstring array to equivalent integer value */ int xtoi(char hexstring[]) { printf("in xtoi, processing %s\n", hexstring); int answer = 0; int i = 0; int valid = 1; int hexit; if (hexstring[i] == '0') { ++i; if (hexstring[i] == 'x' || hexstring[i] == 'X') { ++i; } } while(valid && hexstring[i] != '\0') { answer = answer * 16; if(hexstring[i] >='0' && hexstring[i] <= '9') { answer = answer + (hexstring[i] - '0'); } else { hexit = hex_Val(hexstring[i]); if (hexit == 0) { valid = 0; } else { answer = answer + hexit; } } ++i; } if(!valid) { answer = 0; } return answer; }
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
Minds are like parachutes - they only work when they are open
Gaiety
Gaiety
![]() |
Similar Threads
- Python loop help (Python)
- Endless loop while losing focus (C#)
- Endless Loop (C++)
- Endless Loops (Python)
- Keyboard input or "stuck in a loop" (C++)
- norton disk doctor reboot for repair in endless loop (Windows NT / 2000 / XP)
- endless loop? not sure why, any suggestions? (C++)
Other Threads in the C Forum
- Previous Thread: Airline Ticket Reservations System
- Next Thread: fprintf doesn't add newline to file in cygwin
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h





