0

I want to take two string input from user in c#, and than convert them to integer by their position in array and then add them. My code is the one below.

        string a = Console.ReadLine();
        string b = Console.ReadLine();
        int i,c,d,j;
        for (i=a.Length-1;i>=0;i--)
        {
            c = Convert.ToInt32(a[i]);
            d = Convert.ToInt32(b[i]);
            j = c + d;
            Console.WriteLine("{0} ",j);
            Console.ReadKey();
        }

This code is showing wrong output such as "99 99" for input "12 21". I wanted to add c+d then put the sum in j. Ultimately I want to write a code for big sum problem. Where I am making mistakes?

Expected: for strings "12" and "34" output to be "46" (1+3 and 2+4).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • what bis expected output for string "12 21" as mentioned in your question ? – Mudassir Hasan Nov 02 '16 at 05:39
  • suppose 12 another one 34, then output should be (1+3) 4 and (2+4) 6. JohnG's solution worked. I need to use Convert.Toint32(a[i].ToString()), to convert to integer, or it takes the ASCII value. –  Nov 02 '16 at 06:07

4 Answers4

1

Your looping logic does not make any sense.

You are looping for the length of the string entered into a? If there will always only be 2 numbers why are you looping in the first place?

Why not just try:

string a = Console.ReadLine();
string b = Console.ReadLine();

int c = Int.Parse(a);
int d = Int.Parse(b);

int j = c + d
ThatChris
  • 752
  • 1
  • 4
  • 18
  • It wouldn't be 2 numbers always. I am in the very beginning. Just trying to see if it works, then will adjust the loop. –  Nov 02 '16 at 05:59
0

Loop doesn't make any sense,because you need to parse into integer.In your code,the variable c and d is getting the ASCII value of alphabets, instead try

int c=Int.Parse(a);
int d=Int.Parse(b);
Mahek
  • 552
  • 2
  • 7
  • 28
0

When you use the string (array) like a[i] you are not getting what you want...

The way you are referencing the string is the problem. Even though “a” is a string... when you reference it like c = Convert.ToInt32(a[i]); a[i] is going to return something else. You want to get a[i]’s value.

c = Convert.ToInt32(a[i].ToString());
d = Convert.ToInt32(b[i].ToString());
JohnG
  • 9,259
  • 2
  • 20
  • 29
  • Thnx mate. It worked after adding .ToString() . Can you explain me about ".ToString()" . –  Nov 02 '16 at 05:58
  • @Himura Da Battosuai ToString() is a method that comes with most Classes. If you don't know what a class is... google is your friend here... and there is plenty of help on that subject. But basically ToString() returns a string representation of the Object (Class). Glad to see you got it working. – JohnG Nov 02 '16 at 06:05
0

In addition to converting a[i] and b[i] to string first (ToString method), you should rather use Int32.TryParse . It can stop unexpected exceptions from popping up later.

A basic implementation would be:

int num;

bool result = Int32.TryParse(a[i].ToString(), out num);

if (result) // some action

else // some other action

See: https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

Elmar
  • 55
  • 3
  • 15