-8

I have done a code which has to be written a mathemathical function and then give the images. First, introduces the function as a string variable, but I don't know how to convert it in another type of variable to return the images of the function.

The compiler error I get is:

cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int printf(const char*, ...)'

#include<iostream> 
#include<cmath> 
#include<math.h> 
#include<vector> 
#include<stdio.h> 
#include<iomanip> //setprecision// 
#include<sstream> 
#include<string> 
#define precisio 4 
#define K 100   
using namespace std;  
double valors(double a, double b);  
double g(double x);  
double x; 
double a = x; 
double y(x); 
int i;    
int main () {       
  cout << setprecision(precisio);   
  cout << "Escriu l'interval de la funció" << endl;         
  double a, b;      
  cout << "\n a =";     cin >> a;       
  cout << "\n b =";     cin >> b;       
  cout << "Escriu la funcio" << endl;   string s;   
  cin >> s;     cout << s;      
  double y = printf(s.c_str());         
  valors(a,b); 
}  

double valors(double a, double b){          
  int punts = K*(b-a) + 1;      
  double amplada = (b-a)/K;         
  cout << "\n\tx\tf(x)" << endl;        

  for (int i = 0; i < punts; i++) {                 
    cout << "\t" << a << "\t" << g(a) << endl;      
    a = a + amplada;    
  }     
}

double g(double x){
   return y; 
}
6
  • Why are you performing this: double y = printf(s.c_str()); when you could use cout << s; like you did in the lines above? Nov 10, 2015 at 19:10
  • 3
    Using a debugger will help clarify issues. This is a good example for learning to use a debugger. Nov 10, 2015 at 19:12
  • This code compiles and runs without any editing. Whatever the problem is, it's either not in this code, or it's not what was asked. Nov 10, 2015 at 19:13
  • No one else saw the missing return on valors()? O.o Nov 10, 2015 at 19:17
  • 1
    Please edit your code to reduce it to a minimal reproducible example of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. Jul 20, 2018 at 12:35

3 Answers 3

1
double valors(double a, double b)

Your function does not return any value, while it should return "double".

1
  • This is the only compilation error I've had, and only VS seems to treat it as an error. Nov 10, 2015 at 19:18
0

I assume the purpose of :

cin >> s;   cout << s;
double y = printf(s.c_str());

was to put the value entered by the user on standard input into y. If so, you should just do:

cin >> y;

0

No problem with this online compiler utilizing C++14: here

#include<iostream>
#include<stdio.h>
#include<iomanip> //setprecision//
#include<sstream>
#include<string>
#define precisio 4
#define K 100
using namespace std;
double valors(double a, double b);
double g(double x);
double x;
double a = x;
double y(x);
int i;
int main()
{
    cout << setprecision(precisio);
    cout << "Escriu l'interval de la funció" << endl;
    double a, b;
    cout << "\n a =";
    cin >> a;
    cout << "\n b =";
    cin >> b;
    cout << "Escriu la funcio" << endl;
    string s;
    cin >> s;
    cout << s;
    double y = printf(s.c_str());
    valors(a, b);
}

double valors(double a, double b)
{
    int punts = K * (b - a) + 1;
    double amplada = (b - a) / K;
    cout << "\n\tx\tf(x)" << endl;

    for (int i = 0; i < punts; i++)
    {
        cout << "\t" << a << "\t" << g(a) << endl;
        a = a + amplada;
    }
}
double g(double x)
{
    return y;
}
4
  • LOL Youd didn't include <iostream> ;-; Nov 10, 2015 at 19:15
  • @Kostas Lifeboy, it was there at IdeOne. It wasn't just in the code text here. Nov 10, 2015 at 19:20
  • LOL I just tried it and it didn't throw any error O.o Awesome! :D Nov 10, 2015 at 19:43
  • Crank up the warning level. I use -pedantic -pedantic-errors -Wall -Wextra -Werror Nov 10, 2015 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.