Simple Deriviation Program

I'm a beginner in C++. In this program, I'm attempting to derive a simple mathematical problem (i.e. 3x^8 = 24x^7). I know where my problem is, but I don't know how to fix it. Near the middle of the program I attempt to display what the user has entered to no avail. How can I make the system recognize the difference between the variable that is entered, the constant, and the power which everything is raised to? (I'm using the basic idea that c*x^n = c*n*x^(n-1), where c is the constant, x is the variable, and n is the power).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  //C++ Program
//Program is for finding simple derivatives (i.e. 5x^7)

#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main()
{
	int constant, power, ans;
	char var;

	cout << "Enter a constant of variable to be derived (if there is no constant enter 1): ";
	cin >> constant;
    cout << "Now enter the variable to be derived: ";
	cin >> var;
	cout << "Finally, enter the power which everything is raised to: ";
    cin >> power;
    
	cout << "\n\nYou have entered: \n" << constant*power << "\n";
    
	//ans = constant * var ^(power - 1), pow(var, power);

	//cout << "\nThe deriviation is: \n" << ans << "\n\n";

	system("pause");
	return 0;

}
 
	cout << "\n\nYou have entered: \n" << constant*power << "\n";


Should look like this:

 
	cout << "\n\nYou have entered: \n" << constant << " ^ " << power << "\n";


If I am understanding correctly what you mean, which I'm not sure I am.
Yeah, that would work.

But after that, am I properly getting the answer? (I've put it as a comment because I wasn't finished with the lines above.

Thanks for your input.
I've got it displaying properly now. It actually should be:

cout << "\n\nYou have entered: \n" << constant << "*" << var << "^" << power << "\n";

This way, it also displays the x variable :)
So here's the real problem: when I enter something like x^2 (a.k.a 1x^2), it gives me the answer as: 121, when it should be 2x.

Anyone got an idea on how I should proceed?
Actually, it's a lot simpler than I thought. Thank you for your input Mats, you kind of helped to spark some ideas :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//C++ Program
//Program is for finding simple derivatives (i.e. 5x^7)

#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main()
{
	int constant, power, ans;
	char var;

	cout << "Enter a constant of variable to be derived (if there is no constant enter 1): ";
	cin >> constant;
               cout << "Now enter the variable to be derived: ";
	cin >> var;
	cout << "Finally, enter the power which everything is raised to: ";
               cin >> power;
    
	cout << "\n\nYou have entered: \n" << constant << "*" << var << "^" << power << "\n";
    
	ans = constant * power;
	power = power - 1;

	cout << "\nThe deriviation is: \n" << ans << "" << var << "^" << power << "\n\n";

	system("pause");
	return 0;

}
Topic archived. No new replies allowed.