String Help

Hello everyone, I am trying to build a simple derivative 'calculator' that takes a string (sinx, cosx, etc...) and outputs what the derivative of the simple trig function is. When I compile and run it it doesn't output anything other than invalid input no matter the input. Any help would be appreciated!


#include <iostream>
#include <string>
using namespace std;

void calculate(string function);
string sinx, cosx, tanx, secx, cscx, cotx;

int main(){
string function;
cout<<"Please enter a basic trigonometric function (secx, sinx, tanx, etc...): ";
cin>>function;
cout<<"The function is: "<<function<<endl;
calculate(function);
return 0;
}

void calculate(string function){
if(function==sinx){
cout<<"The derivative of "<<function<<"is cosx!";
}
else if(function==cosx){
cout<<"The derivative of "<<function<<"is -sinx!";
}
else if(function==tanx){
cout<<"The derivative of "<<function<<"is sec^2x!";
}
else if(function==secx){
cout<<"The derivative of "<<function<<"is secxtanx!";
}
else if(function==cscx){
cout<<"The derivative of "<<function<<"is -cscxcotx!";
}
else if(function==cotx){
cout<<"The derivative of "<<function<<"is -csc^2x!";
}
else{
cout<<"Invalid input!";
}
}
The name of a variable is meaningless to the program itself.

I would say
1
2
string blahblabhalh = "hello";
cout << blahblabhalh << endl;

This would print hello, not blahblabhalh.

Easiest way to solve this in your case would be to remove your declaration of "string sinx, cosx ... ;" and change your if statements to

1
2
3
4
5
6
if (function=="sinx")
    ..
else if (function == "cosx")
    ...
else if (function == "tanx")
   ...
Last edited on
if(function==sinx)What do you think the value of sinx is?
If you are not sure print it and see.
Thank you guys for the speedy responses. I realize now that the problem I had was that the if statements did not have the quotation marks around the value. There was no need for the trigonometric string variables when you take this into account. Again, thank you guys very much.
Topic archived. No new replies allowed.