Hey there!I'm doing this symbolic differentiation program,and i have some problem with the code.The code in the "main" is ok ,but i have problems in the "to_term" function.Can you help me out to find where my mistake is?Please if you have any idea reply me now Thanks.

#include <iostream>
#include <string>
#include <utility>
using namespace std;
void to_term( string substr[],const int sizeofpolynom)
 {  double coefficient ;
    char variable ;
    char exp_symbol ;
    int exponent ;
	double term[sizeofpolynom];
	 for(int j=0;j<sizeofpolynom;j++)
	 {std::istringstream stm(substr[j]) ;
	 stm >> coefficient >> variable >> exp_symbol >> exponent ;
	 pair<double,int> term[j]=make_pair(coefficient,exponent);
	 cout<<term[j].first<<","<<term[j].second<<endl;
	 }



 }
int main ()
{  const int polynomsize=5;
   string sub[polynomsize];
  string str ="2.4x^4 - 1.2x^3 + 6x^2 + 4.1x + 9.2";
  size_t found;

  found=str.find_first_of("+-");
  
   sub[0]=str.substr(0,found);
  cout<<"sub1"<<"  "<<sub[0]<<endl;
  while (found!=string::npos)
  { for (int i=1;i<=polynomsize;i++)

         { int a=found;
           found=str.find_first_of("+-",found+1);
           sub[i]=str.substr(a,found-a);
           cout<<"sub"<<i+1<<"  "<<sub[i]<<endl;}
    
  } 

     to_term(sub,polynomsize);

  return 0;
}

And here are the mistakes that appear in the console:
1>------ Build started: Project: str1, Configuration: Debug Win32 ------
1>Compiling...
1>str.cpp
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(10) : error C2057: expected constant expression
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(10) : error C2466: cannot allocate an array of constant size 0
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(10) : error C2133: 'term' : unknown size
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(12) : error C2079: 'stm' uses undefined class 'std::basic_istringstream<_Elem,_Traits,_Alloc>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(12) : error C2440: 'initializing' : cannot convert from 'std::string' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(13) : error C2297: '>>' : illegal, right operand has type 'double'
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(13) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(14) : error C2057: expected constant expression
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(14) : error C2466: cannot allocate an array of constant size 0
1>c:\users\user\documents\visual studio 2008\projects\str1\str1\str.cpp(14) : error C2075: 'term' : array initialization needs curly braces
1>Build log was saved at "file://c:\Users\User\Documents\Visual Studio 2008\Projects\str1\str1\Debug\BuildLog.htm"
1>str1 - 9 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Recommended Answers

All 2 Replies

>>error C2057: expected constant expression

corresponds withs this double term[sizeofpolynom]; . The reason being is
that sizeofpolynom isn't known at compile time. Because the user can pass it anything. So to solver this there is a couple of things you can do, but the easiest way that I suggest is to use std::vector<double>. You would hardly have to change anything. Another quick but dirty option is to make this const int polynomsize=5; global.

still doesn't work ..uuffff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.