String to int | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

String to int

I wrote this code to convert number string to int and compare:- #include<stdio.h> int str(char S1[],char S2[]) { int x=0,y=0,i; for(i=0;S1[i]!='\0';i++) { x*=10; int m=S1[i]; x+=m; } for(i=0;S2[i]!='\0';i++) { y*=10; int m=S2[i]; y+=m; } if(x>y) return 1; if(y<x) return -1; if(x==y) return 0; } int main() { char S1[100],S2[100]; gets(S1); gets(S2); int a = str(S1,S2); if(a==1) printf("S1 is greater than S2"); else if(a==-1) printf("S2 is greater than S1"); else printf("Both are equal"); return 0; } but x and y are storing garbage values. why is it so? please help.

10th Oct 2017, 12:26 PM
Shantanu Shinde
Shantanu Shinde - avatar
16 Answers
+ 17
If you compare two codes together, you most likely find the answer. In fact, when you are trying to invent the wheel again, weird things might happen!
11th Oct 2017, 9:52 AM
Babak
Babak - avatar
+ 16
Here is a workable version of your problem. Notice that GCC compiler no longer supports gets function. [http://cpp.sh/2bbwp] // You can't run it on SL [https://code.sololearn.com/cLnhWQ6YL7x6]
11th Oct 2017, 9:41 AM
Babak
Babak - avatar
+ 16
I appreciate that, sir. From now on I'll try my best to reduce those kind of mistakes.
11th Oct 2017, 12:03 PM
Babak
Babak - avatar
+ 15
Another way might be using stringstream facilities to do such thing. Here is an ad-hoc solution for an average calculator program . https://code.sololearn.com/cn62B9g73skE/?ref=app
10th Oct 2017, 1:21 PM
Babak
Babak - avatar
+ 15
Of course sir. It's probably the worse solution in terms of considering the limits, but at least gives some clue to our friend of how he can reach to a convincing output.
11th Oct 2017, 11:35 AM
Babak
Babak - avatar
+ 14
As a clear example, see this fragment. if(x>y) return 1; if(y<x) // is the same sa (x > y) return -1; if(x==y) return 0;
11th Oct 2017, 10:59 AM
Babak
Babak - avatar
+ 14
Bookmarked. Thanks again. :)
11th Oct 2017, 12:22 PM
Babak
Babak - avatar
+ 2
Well There is a function called atoi and itoa which converts string to integer and integer to string respectively Try that out! Not sure but I think it's in the stdlib header file
11th Oct 2017, 6:59 AM
Utkarsh Gupta
Utkarsh Gupta - avatar
+ 1
so can you pls tell me what wrong with my code?
11th Oct 2017, 12:03 PM
Shantanu Shinde
Shantanu Shinde - avatar
0
I did that minus of '0' still same thing
11th Oct 2017, 12:55 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
I don't understand and how is garbage stored in a as well. a takes value from the function and the function returns only 3 values
11th Oct 2017, 12:56 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
I don't have to use and library func
11th Oct 2017, 7:07 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
why isn't mine working but?
11th Oct 2017, 9:48 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
I have done everything same. just I used gets.
11th Oct 2017, 10:38 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
so what's difference
11th Oct 2017, 11:02 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
but I have done the same thing. just x>y instead of y<x. so why isn't mine working?
11th Oct 2017, 11:47 AM
Shantanu Shinde
Shantanu Shinde - avatar