Author Topic: Codeblocks showing unexpected output  (Read 3123 times)

Offline anmol

  • Single posting newcomer
  • *
  • Posts: 2
Codeblocks showing unexpected output
« on: January 18, 2019, 07:14:18 pm »
I was solving this question on spoj- https://www.spoj.com/problems/ABSYS/

I compiled the code on geeks for geeks online compiler and it gave correct output  ( https://ide.geeksforgeeks.org/P0gWlUy4Yg ), but codeblocks gave wrong output for last case.
I also submitted the solution on spoj, and the solution got accepted.

The expected output is:
23 + 47 = 70
3247 + 502 = 3749
1613 + 75425 = 77038

I compiled the code on codeblocks and it is showing output as:
23 + 47 = 70
3247 + 502 = 3749
1613 + 75424 = 77037

As you can see, the third test case gives wrong output, which i have highlighted.

I am using version 17.12 of codeblocks on windows 10, and this is the first time something like this occurred. can anyone clarify, where the problem is ? is it codeblocks error/bug or some compiler error ?

I have attached the screenshot of my codeblocks output.

Following is my code:
Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,s1,s2,s3;
    long long int t,i,j,k,f,a,b,T;
    cin>>T;
    cin.ignore();
    while(T--)
    {
        cin.ignore();
        getline(cin,s);

        for(i=0; ; i++)
        {
            if(s[i]==' ')
            {
                s1=s.substr(0,i);
                //cout<<s1<<" ";
                break;
            }
            if(s[i]=='m')
                f=1;
        }
        for(j=i+3; ; j++)
        {
            if(s[j]==' ')
            {
                s2=s.substr(i+3,j-i-3);
                //cout<<s2<<" ";
                break;
            }
            if(s[j]=='m')
                f=2;
        }

        for(k=j+3; ; k++)
        {
            if(s[k]=='\0')
            {
                s3=s.substr(j+3,k-j-3);
                //cout<<s3<<" ";
                break;
            }
            if(s[k]=='m')
                f=3;
        }

        if(f==1)
        {
            t=s2.size();
            a=0;
            b=0;
            for(i=0; i<s2.size(); i++)
            {
                a=a+((s2[i]-'0')*pow(10,--t));
            }
            t=s3.size();
            for(i=0; i<s3.size(); i++)
            {
                b=b+(s3[i]-'0')*pow(10,--t);
            }
            cout<<b-a<<" + "<<a<<" = "<<b<<"\n";
        }
        if(f==2)
        {
            t=s1.size();
            a=0;
            b=0;
            for(i=0; i<s1.size(); i++)
            {
                a=a+(s1[i]-'0')*pow(10,--t);
            }
            t=s3.size();
            for(i=0; i<s3.size(); i++)
            {
                b=b+(s3[i]-'0')*pow(10,--t);
            }
            cout<<a<<" + "<<b-a<<" = "<<b<<"\n";
        }
        if(f==3)
        {
            t=s1.size();
            a=0;
            b=0;
            for(i=0; i<s1.size(); i++)
            {
                a=a+(s1[i]-'0')*pow(10,--t);
            }
            t=s2.size();
            for(i=0; i<s2.size(); i++)
            {
                b=b+(s2[i]-'0')*pow(10,--t);
            }
            cout<<a<<" + "<<b<<" = "<<a+b<<"\n";
        }
    }
}

« Last Edit: January 18, 2019, 07:16:54 pm by anmol »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1557
Re: Codeblocks showing unexpected output
« Reply #1 on: January 18, 2019, 07:37:12 pm »
When you use doubles you can suffer this kind of issues due to their limited precision.

Anyway, Codeblocks is an IDE and not a compiler. You should ask in your compiler's support site.

Offline anmol

  • Single posting newcomer
  • *
  • Posts: 2
Re: Codeblocks showing unexpected output
« Reply #2 on: January 19, 2019, 08:33:58 am »
but i have used long long int only. nowhere i have used doubles. can you specify more ?

and i am using codeblocks ide, that includes a compiler right? how can i know that which compiler my ide is using ?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Codeblocks showing unexpected output
« Reply #3 on: January 19, 2019, 08:39:01 am »
https://en.cppreference.com/w/cpp/numeric/math/pow

For the compiler: by starting in a console window and seeing what it returns...

p.s. including bits/stdc++.h is probably not a good idea :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]