[try Beta version]
Not logged in

 
String error?

Mar 8, 2013 at 5:26pm
Hi,

I have another issue with a sample excercise. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string s;
	double op1, op2, sum, diff, product, div;
	sum = op1 + op2;
	diff = op1 - op2;
	product = op1 * op2;
	div = op1 / op2;
	cout << "Enter an operation and two operands: ";
	while (cin >> s >> op1 >> op2){
		if (s=='+')
			cout << "Sum is equal to " << sum << "\n";
		if (s=='-')
			cout << "Diff is equal to " << diff << "\n";
		if (s=='*')
			cout << "Product is equal to " << product << "\n";
		if (s=='/')
			cout << "Div is equal to " << div << "\n; 


Here is the error I'm getting : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'std::string'
Mar 8, 2013 at 5:29pm
Please paste the entire program; the code you have shown here does not relate to the error you have given.
Mar 9, 2013 at 1:35am
I would change your string variable to a char. I believe that a statement like string == char is illegal. It looks like you are doing your calculations, before you input the data you are doing your calculation on. You need to reverse that. Also you are missing a double quotation mark at the end of line 16. I would change the \n to an endl;

So here is what i came up with
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
char anser = 'Y';
char sign;
	double op1 = 1.0, op2 = 1.0, sum = 1.0, diff = 1.0, product = 1.0, div = 1.0;
	
	cout << "Enter an operation and two operands: ";
	cin >> sign >> op1 >> op2;
	cin.ignore();

	while (anser == 'Y')
	{
		
		if (sign == '+')
		{
			sum = op1 + op2;
			cout << "Sum is equal to " << sum << "\n";
		}
		else if (sign == '-')
		{
			diff = op1 - op2;
			cout << "Diff is equal to " << diff << "\n";
		}
		else if (sign == '*')
		{
			product = op1 * op2;
			cout << "Product is equal to " << product << "\n";
		}
		else if (sign == '/')
		{
			div = op1 / op2;
			cout << "Div is equal to " << div << "\n"; 
		}
		else
			cout << "no matching sign" << endl;
		
		cout << "Would you like to continue [y/n] ";
		cin >> anser;
		cin.ignore();
		anser = toupper(anser); //converts anser to uppercase
		cout << endl;
		
		if (anser == 'Y')
		{
			cout << "Enter an operation and two operands: ";
			cin >> sign >> op1 >> op2;
			cin.ignore();
		}
		else
		{
			cout << "Goodby"<< endl;
			break;
		}
	}


i hope this helps
Topic archived. No new replies allowed.