char releasing weird symbols and strings not working

I have created a simple code to execute as part of my binomial expansion program (please correct me if i'm wrong, i don't speak English nativelly, so i may write something wrong when referring to math). But i'm having a bit of trouble, when i use char, i receive some weird output, such as, JdK, óKj, a smiling face written "ók" right next to it and others... also, i cannot use strings, as they do not work with ^ or *.

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
#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;

char a,b,c,d,e,f,g;
int j=1;

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"Binomial Expansion"<<".\n";
	while(j=1)
	{
		cout<<"Insert the first factor: ";
		cin>>a;
		cout<<"Insert the second factor: ";
		cin>>b;
		cout<<"Insert the expoent: ";
		cin>>c;
		d=(a^c);
		e=(2*a*b);
		f=(b^c);
		cout<<d;
		cout<<e;
		cout<<f;
		cout<<".\n";
	}
	return 0;
}


Please keep in mind that this is a separate code and int main() contents from here will go on another program.
I'm writing this in Visual C++ 2010 Express, and using Windows 7.

What i'm i doing wrong?
Last edited on
why are you using char? This looks like a mathematical calculation which requires type double.

^ is for bitwise operations on integers.
If you want to calculate ac then use the function pow()
 
    d = pow(a,c);

http://www.cplusplus.com/reference/cmath/pow/





Thanks, i had forgot about the use of pow(), but how about the *?
And also, i had some trouble with double previously...
But i must be sure that double can accept letter variables. Is it possible?
But i must be sure that double can accept letter variables. Is it possible?

No, it is not possible. If you want to input a letter, use type char.

It isn't really clear what it is you are trying to do. Why do you want to input letters?
Yes, as int, double also does not support letter variables. I've also tried to use string, without forgetting to #include <string> , but this has proved to be useless, as it conflicts with pow() and *.
I am trying to receive letters as inputs to use binomial expansion in order to obtain a quadratic equation... I think that's how i should put it, in mathematical terms.
Let me guess.
You want to be able to input something like "3x", "2", and "5" and have the program output something like
(3x+2)^5 = 243x^5 + 810x^4 + 1080x^3 + 720x^2 + 240x + 32
?
If so, you'll have to deal with the number and the letter separately, since there's no standard library function out there that will let you calculate (3x)5=243x5.
(So you'll have to do 35 = 243 and then tack on an x5, etc.)

Additionally, if you want it to also be able to expand something more complex such as
(3x^2 - 2/x)^3 = 27x^6 - 54x^3 - 8/x^3 + 36
, then you'll practically have to implement your own computer algebra system (which is not so easy)....
Last edited on
Are you sure? This is bad, i wanted to have a very simple "interface" for the user... This will make it too complicated, most people won't understand (some friends of mine, and other people from my classroom).
Well, thanks anyway, both of you. I appreciate your time.
Last edited on
You probably want to take the input as a string then split it into the letter and number. "5x" doesn't make any sense in programming, it doesn't mean x multiplied by 5, instead it means the number 5 and the letter x. When you do binomial expansion part of it may be (5x)^3, you could tackle this by splitting the input (5x) into 5 as an integer and x as a char then raise 5 to the power of 3 and output the new value of the coefficient as well as some representation of x being raised to the power of 3 (giving 125x^3).

I'm not sure if that made much sense at all so I'll provide an example:
The user inputs 3x, 2 and 3 as the power, giving the equation (3x+2)^3.
The first input consists of both a character and an integer so it should be split into the integer (or float/double) coefficient 3 as well as the character portion being x (you may want to use a class/struct for this). 2 is then an integer on its own (could also be a float/double) and should be treated as an individual piece of data.
You will then need to calculate the values for each term (in this case I believe we get (2^3) + (2^2 * (3x) * 3) + (2 * (3x)^2 * 3) + ((3x)^3) ) But this cannot be done automatically, it should instead be handled by calculating the value of the second term to the power of n, then the first terms coefficient to the power of n, then the value of 3Cn and all should be multiplied, finally the value of x can be stuck on the end and represented as being to the power of n (e.g. x^n or x*x*...), this must then be repeated n times so that all values are calculated. Keep in mind that this will only work if the input uses x as the first term, other wise it will need to be switched and if both have a letter as a term you'll need to just shove the other letter on the end in the same way x is.

I hope this helps, I'm a little tired at the moment so this may just sound like some crazy, nonsensical ramblings so please do ask me to clear up anything (or everything) that doesn't make sense.

EDIT: It should work exactly as you wanted it, it'll require a little more work on your end but your friends should be able to use it quite easily. Check out things like the wolfram binomial expansion tool and look at how easy that is to use, there's no reason why you couldn't make something just as simple to operate :)
http://www.wolframalpha.com/widgets/view.jsp?id=760505598bcd4e4ff57b77bbff4f7f9d
Last edited on
If you're willing to restrict the input to just expressions in the form (ax+b)n, then it's a lot easier since you'll just have to calculate the coefficients (which you can do with std::pow and your ordinary multiplication operator (*) and such) and then you can tack on the "x" part without too much effort.
You probably want to take the input as a string then split it into the letter and number.


I think i understand what you mean, but i don't have any idea of how to do this... How i'm i able to split a string variable?
This will hopefully help:
https://ideone.com/2kcDAL

There are some issues with this you may want to fix such as entering "3x6y" will convert to "36xy" instead of "18xy" but without complicating things too much, the linked code will work for any input with the coefficient as a single term entered before the variables.

Here is an ASCII table for reference:
http://www.asciitable.com/
I have tried your code, but i'm not able to get pow() working... Here's the code (please point me to any mistakes):

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "stdafx.h"
#include <iostream>
#include <string>
#include "math.h"
using namespace std;

string a1,b1;
int c,d,e,f;
int j=1;

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"Binomial Expansion"<<".\n";
	while(j=1)
	{
		cout<<"Insert the first factor: ";
		cin>>a1;
		string a2, coefficientString; //Variable declerations
		for(unsigned int i = 0; i < a1.length(); ++i)
		{
			unsigned int currentVal = (int)a1.c_str()[i]; //Get the integer value of the current character
			if(currentVal > 47 && currentVal < 58) // Onan ASCII table all integers are between 47 and 57, in this case a number has been entered
			{
				coefficientString += a1.c_str()[i]; //All numbers are added to a string for the coefficient
			}
			else
			{
				a2 += a1.c_str()[i]; //All non-numerical values are being considered variables, you may want to limit this to alphabetical only
			}
		}
		int a = atoi(coefficientString.c_str()); //The string is then converted to an integer


		cout<<"Insert the second factor: ";
		cin>>b1;
				string b2, coefficientString2; //Variable declerations
		for(unsigned int i = 0; i < b1.length(); ++i)
		{
			unsigned int currentVal = (int)b1.c_str()[i]; //Get the integer value of the current character
			if(currentVal > 47 && currentVal < 58) // Onan ASCII table all integers are between 47 and 57, in this case a number has been entered
			{
				coefficientString2 += b1.c_str()[i]; //All numbers are added to a string for the coefficient
			}
			else
			{
				b2 += b1.c_str()[i]; //All non-numerical values are being considered variables, you may want to limit this to alphabetical only
			}
		}
		int b = atoi(coefficientString2.c_str()); //The string is then converted to an integer
		cout<<"Insert the expoent: ";
		cin>>c;
		while(cin.fail())
		{
		cin.clear();
			cin.ignore(numeric_limits<std::streamsize>::max(),'\n');
		cout<<"The informed value is not a number."<<".\n";
		cout<<"Insert the expoent: ";
		cin >> a;
		}
		d=(pow(a,c));
		e=(2*a*b);
		f=(pow(b,c));
		cout<<d<<a2;
		cout<<e<<a2<<b2;
		cout<<f<<b2;
		cout<<".\n";
	}
	return 0;
}
I think you should review what it is your code is doing.
d is the coefficient of the first term to the power of c, e is the coefficient of the first term multiplied by the coefficient of the second term multiplied by 2 and f is the coefficient of the second term to the power of c. I don't really see where this is going, for something like (2x+3)^3 you can expect to have the following if all works correctly:
d = 8
e = 12
f = 27
The output will be 8x12x27. This really makes no sense at all, I'd suggest having a little rethink to see what should be done.

Each term that is in the output should be in the form "coefficient * term + ". I'd suggest using a for loop between 0 and the value of the exponent (lets use i for the variable), for each iteration calculate the value of the first term to the power of i, multiply by i C exponent then multiply this by the second term to the power of the exponent - i. I think this should give the desired result, it probably sounds complicated and I may not be making much sense so please, once again, do ask if you need anything to be cleared up or any further advice.

Also, at line 58 the cin should be inputting to c and at line 14 it should be "j==1", "j=1" will make j have the value of 1 so j=1 is always true regardless of the value of j.
The current display of the answer is not a problem for me, as this is merely a test to be implemented into another program, a little bit more extensive.

The j variable is only creating the loop for now, i'll rewrite it later.

The big problem is that i can't compile because pow() isn't working, but it should because a should only be an int at this point.
Last edited on
It compiled fine for me, what error is your compiler giving? Are you sure it's a problem with pow and not something else?
The error before compiling is:
Error: more than one instance of overloaded function 'pow' matches the argument list:


The error after trying to compile is:
1>------ Build started: Project: BasicCalc2, Configuration: Debug Win32 ------
1> BasicCalc2.cpp
1>c:\users\arthur\documents\visual studio 2010\projects\basiccalc2\basiccalc2\basiccalc2.cpp(65): error C2668: 'pow' : ambiguous call to overloaded function
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)'
1> while trying to match the argument list '(int, int)'
1>c:\users\arthur\documents\visual studio 2010\projects\basiccalc2\basiccalc2\basiccalc2.cpp(67): error C2668: 'pow' : ambiguous call to overloaded function
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)'
1> while trying to match the argument list '(int, int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



And, of course, i haven't altered the math header.
Last edited on
Ah, I've encountered this one before, basically pow doesn't actually have an overload with 2 integers as parameters so it needs to convert them to something and it could convert it to anything. Just change it to this:
d=(pow((float)a,c));
This will just use the (float, int) version of pow.
Last edited on
So the code has *worked*. Now i have to think of the possibilities so the program won't output anything wrong... So far, so good...
Btw, what would you think about a math.h extension? I'm writing some new functions...
Topic archived. No new replies allowed.