c - Variable isn't storing return value -
i trying make program converts hex string decimal. having issue assigning returned integer value findlength function. going printf statements can tell findlength(thestring) yield correct value length showing value of 0 despite fact have length = findlength(thestring).
this isn't homework problem, i'm absolutely stumped why simple assignment isn't working. i've declared length know that's not issue. i'm getting no compiler messages. appreciated.
edit: know convert doesn't useful , loop needs fixed shouldn't effecting findlength return right? second edit: i've submitted string of '324' tested.
#include <stdio.h> int convert(char s[], int thelength); int findlength(char s[]); int main(){ char thestring[100]; int result; int i; int length; printf("%s","hello, please enter string below. press enter when finished."); scanf("%s",thestring); //apparently scanf bad we'll learn better input methods later. //for tests submitted string of '324'. length = (findlength(thestring)); //length = findlength('324') printf("%d",findlength(thestring)); //yields 3 printf("%d",length); //yields value of 0 always. result = convert(thestring, length); printf("%d\n result is",result); return 0; } //end of main int convert(char s[], int thelength){ //this function converts string of hex ints. of nothing useful. int i; int sum; for(i = thelength; i=0; i--){ sum = sum + s[i]; printf("%d\n",sum); } return sum; } //end of convert int findlength(char s[]){ int i; for(i = 0; s[i]!='\0'; ++i){ } return(i); } //end of findlength
the variable length
storing correct value. think has confused how you've laid out printf
statements. if try below easier see code works.
#include <stdio.h> int findlength(char s[]); int main(){ char thestring[100]; int result; int i; int length; printf("hello, please enter string below. press enter when finished.\n"); scanf("%s",thestring); length = (findlength(thestring)); printf("findlength(thestring) = %d\n",findlength(thestring)); printf("length = %d\n",length); return 0; } int findlength(char s[]){ int i; for(i = 0; s[i]!='\0'; ++i){ } return(i); }
just clarify in post have
printf("%d",findlength(thestring)); printf("%d",length); printf("%d\n result is",result);
note \n
before %d
in last printf
statement. 0 because convert
function needs fixed , value of result
not length
.
Comments
Post a Comment