-1

I wrote a code for spoj.com

Adding reverse number

We have to input two numbers reverse both and add, and finally print the reverse of the sum.

I have scanned both numbers in form of string and used atoi and itoa functions several times.

This code works fine on Codeblocks 13.12 but shows compilation error on ideone or spoj(c++ 5.3.2).

prog.cpp: In function 'int main()':

prog.cpp:38:25: error: 'itoa' was not declared in this scope
         itoa(sum,ssum,10);

Also am not able to find which g++ compiler Codeblocks is using. I also tried adding stdlib.h but it also don't work.

This is my code in c++

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,num1,num2,sum,ln1,ln2,lns,i,j;
    char snum1[100],snum2[100],ssum[100];
    char stemp;
    cin>>t; // scaning number of test cases
    while(t--)
    {
        cin>>snum1>>snum2; // scanning number 1 and number 2 as string
        ln1=strlen(snum1); //finding length of string of number 1
        ln2=strlen(snum2); // finding length of string of number 1
        for(i=0,j=ln1-1;i<ln1/2;i++,j--) // reversing the string of number 1
        {
           stemp=snum1[i];
            snum1[i]=snum1[j];
            snum1[j]=stemp;
        }
        for(i=0,j=ln2-1;i<ln2/2;i++,j--) // reversing string of number 2
        {
           stemp=snum2[i];
            snum2[i]=snum2[j];
            snum2[j]=stemp;
        }

        num1=atoi(snum1); //converting reversed string of number 1 to integer as num1
        num2=atoi(snum2); // converting reversed string of number 2 to integer as num 2

        sum=num1+num2; // finding sum

        itoa(sum,ssum,10); // converting sum(integer) t0 string
        lns=strlen(ssum); // finding length of string of sum
        for(i=0,j=lns-1;i<lns/2;i++,j--) // reversing the sum string
        {
           stemp=ssum[i];
            ssum[i]=ssum[j];
            ssum[j]=stemp;
        }
        sum=atoi(ssum); // finally converting the reversed sum string to integer
        cout<<sum<<endl; // final printing of result

    }
    return 0;

}
Rajnish
  • 419
  • 6
  • 21

1 Answers1

0

Your basic problem is that itoa is not a function defined by the standard.
You need to start the file with

#include <iostream>   // Define cin and cout
#include <stdlib.h>   // Define atoi
#include <string>     // Define to_string and std::string

Next, remember the great commandment: Don't Repeat Yourself. You reverse a string three times. You read a string from cin, reverse it, and convert to integer twice.

#include <iostream>   // Define cin and cout
#include <string>     // Define to_string and std::string, and stoi.

using namespace std;

static int reverse(string snum) // Reverse and convert to integer
{                               // NB.  Takes argument by VALUE.
    const auto len = snum.length();
    for(size_t i=0,j=len-1; i<j; i++,j--) // or use std::reverse.
    {
        const char stemp = snum[i];
        snum[i] = snum[j];
        snum[j] = stemp;
    }
    return stoi(snum);
}

static int read_reversed()
{
    std::string snum;
    cin>>snum;
    return reverse(snum);
}

int main()
{
    int t;
    cin>>t;// scaning number of test cases
    while(t--)
    {
        const int num1 = readreversed();
        const int num2 = readreversed();
        const int sum=num1+num2;

        string ssum = to_string(sum);

        cout << reverse(ssum) << endl; // Reverse string and print.
    }
    return 0;
}