error: assigning to 'char' from incompatible type 'const char [2]'

I'm writing a proportions calculator. Don't worry about the first part :)

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

char numerator1, denominator1, numerator2, denominator2, answer;



int main (int argc, char *argv[]) {
	
	//Start to read picture
	string line;
	ifstream myfile ("picture.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof())
		{
			getline (myfile,line);
			cout << line << endl;
		}
		myfile.close();
	}
	else cout << "Unable to open file";
	//Finish reading picture
	
	//Output
	cout << "Enter your first ratio: ";
	cin >> numerator1;
	cin >> denominator1;
	cout << "Now your second ratio: ";
	cin >> numerator2;
	cin >> denominator2;
	
	
	if(numerator2="x"){
		answer = numerator1 * denominator2 / denominator1;
		cout << answer;
	}
	
}


I get the following error:
"Proportions.cpp:37:15: error: assigning to 'char' from incompatible type 'const char [2]'
if(numerator2="x"){
^~~~
1 error generated."

How do I solve this?

Thanks
Hi there,
Since you are using char type, you must use its correspondind literals, the single quote not double so, 'x' not "x"

HTH,
Aceix.
Okay. I am having an issue however. Here is my current script.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

char numerator1, denominator1, numerator2, denominator2, answer;


int main (int argc, char *argv[]) {
	//Start to read picture
	string line;
	ifstream myfile ("picture.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof())
		{
			getline (myfile,line);
			cout << line << endl;
		}
		myfile.close();
	}
	else cout << "Unable to open file";
	//Finish reading picture
	
	//Output
	
	cout << "Hello! Use 0 as the unknown variable." << endl;
	//Start loop
		char loop;
			do{
	cout << "Enter your first ratio:" << endl;
	cin >> numerator1;
	cin >> denominator1;
	cout << "Now your second ratio:" << endl;
	cin >> numerator2;
	cin >> denominator2;
	cout << "Your unknown variable is ";
	
	//Formulas
	if(numerator1='x'){
		answer = denominator1 * numerator2 / denominator2;
	}
	if(numerator2='x'){
		answer = numerator1 * denominator2 / denominator1;
	}
	if(denominator1='x'){
		answer = numerator1 * denominator2 / numerator2;
	}
	if(denominator2='x'){
		answer = denominator1 * numerator2 / numerator1;
	}

	//Output answer
	cout << answer << "!";

	//End Loop
		cout<< endl <<"Do you want to continue (Y/N)?" << endl;
		cin>>loop;
		}while(loop=='y'||loop=='Y');
		if(loop=='n'){
			cout << "Bye!" << endl;
	}
}


When I solve a proportion, it says my answer is x. It doesn't actually find the value of x.
Why don't you use int instead of char?

Aceix.
I am confused, tell me what input you're putting in.
Topic archived. No new replies allowed.