c++ - error in solution of ANTI-BLOT SYSTEM(spoj) -


hello have convert expression "43 + machula0 = 163" 43 + 120 = 163.means have scan expression , find missing part.my program runs without putting while loop shows error when run loop.

#include<iostream>   #include<cstring>   #include<cctype>   #include<cstdlib>    using namespace std;    void convert(char *a,int size)      {       int i=0;       char c =a[i];       int f1,f2,f3,n1,n2,s=0; //f1,f2,f3 flags check machula      f1=0;f2=0;f3=0;n1=0;n2=0;         while(c!='+')//to store no. before plus in n1     {         c=a[i];        if(isalpha(c))//to check whether character alphabet or not         {             f1=1;         }         else if(isdigit(c))//to check whether character digit         {             int = c-'0';             n1=n1*10 + a;         }         i++;     }     while(c!='=')//to store no. before plus in n2     {         c=a[i];         if(isalpha(c))         {              f2=1;         }         else if(isdigit(c))         {             int k = c-'0';             n2=n2*10 + k;         }         i++;      }     while(i!=size)//to store no. before plus in s     {         c=a[i];         if(isalpha(c))        {              f3=1;         }         else if(isdigit(c))         {             int h = c-'0';             s=s*10 + h;         }         i++;     }      if(f3==1)    {         s=n1+n2;         cout<<n1<<" + "<<n2<<" = "<<s<<endl;      }     else      {         if(f1==1)         {             n1=s-n2;             cout<<n1<<" + "<<n2<<" = "<<s<<endl;         }         else if(f2==1)         {             n2=s-n1;             cout<<n1<<" + "<<n2<<" = "<<s<<endl;         }         else         {             cout<<n1<<" + "<<n2<<" = "<<s<<endl;         }     }} int main() {     int t;     cin>>t;     while(t--)     {      char *a;     a= new char[10000];     cin.getline(a,10000);     int size = strlen(a);     convert(a,size);     delete []a;     }     return 0; } 

on second thought have useful input. if typing in hand, there attendance type in input this:

1  43 + machula0 = 163" 43 + 120 = 163 

and leads problems

int main() {     int t;     cin>>t; // gets end of number typed.             // not carriage return hitting enter trigger input.     while(t--)     {          char *a; // not relevant problem, awesomely bad idea.                   // use string         a= new char[10000];         cin.getline(a,10000); // returns instantly empty string because of                                // left-over enter used above number.         int size = strlen(a);         convert(a,size); // of convert not check size, instantly out of                           // array bounds , crash         delete []a;     }     return 0; } 

it valid input this:

1 43 + machula0 = 163" 43 + 120 = 163 

better solution:

void convert(string &a) {     (char c: a)     {         //parse logic goes here     }     // output logic goes here }  int main() {     int t;     cin >> t;     cin.get();     while (t--)     {          string line;         if (getline(cin, line))         {             convert(line);         }     }     return 0; } 

unfortunately convert function needs lot more work. complete lack of testing end of string.


Comments