Help with strings (should be a quick fix)

I'm working on a project right now that will solve various simple physics problems for me. It should be pretty easy to see the way I set it up. Don't worry about how I am getting these variables or anything like that, this is just a piece of code for a button.

All I'm wondering is why it is giving me string errors...

Sooo this is my code:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
  Name: "Go Button"
  Author: Matt Cromer
  Date: 08/11/11 17:09
  Description: Used to solve various formulas for my physics widget. 

 I NEED TO:
	-add a way to display my answers on the form. 

*/

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

	int formula = 1; // 1-7, BASED ON THE FORM
	double d = 0, d_0 = 0, V = 0, V_0 = 5, t = 5, a = 10, f = 0, m = 0;
	string missing;

void main ()
{
    
    
//FORMULA NUMBERS ARE BASED ON THE FORM
if (formula == 1)
{
		//MANY IF STATEMENTS TO FIND OUT WHICH FORMULA TO USE
		if (missing == "d")
		{
		d = d_0+V_0*t+1/2*a*pow(t,2);
		//DISPLAY ANSWER
		cout << d;
		cin >> d_0;
		}

		else if (missing == "d_0")
		{
		d_0 = 0;
		//DISPLAY ANSWER
		}

		else if (missing = "V_0")
		{
		V_0 = 0;
		//DISPLAY ANSWER
		}

		else if (missing = "t")
		{
		t = 0;
		//DISPLAY ANSWER
		}

		else if (missing = "a")
		{
		a = 0;
		//DISPLAY ANSWER
		}
}



else if (formula == 2)
{
    system("cls");
	cout << "FORMULA 1" << endl;
}



else if (formula == 3)
{
    system("cls");
	cout << "FORMULA 1" << endl;
}



else if (formula == 4)
{
    system("cls");
	cout << "FORMULA 1" << endl;
}



else if (formula == 5)
{
    system("cls");
	cout << "FORMULA 1" << endl;
}



else if (formula == 6)
{
    system("cls");
	cout << "FORMULA 1" << endl;
}



else if (formula == 7)
{
    system("cls");
	cout << "FORMULA 1" << endl;
}




    
    //end
    cin >> d;

}


When I run it, debug gives me this crap:


1>------ Build started: Project: weiner, Configuration: Debug Win32 ------
1>  wedas.cpp
1>c:\users\matt cromer\documents\visual studio 2010\projects\weiner\weiner\wedas.cpp(44): error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\matt cromer\documents\visual studio 2010\projects\weiner\weiner\wedas.cpp(50): error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\matt cromer\documents\visual studio 2010\projects\weiner\weiner\wedas.cpp(56): error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Help is much appreciated.

Oh and if it helps at all, I am using Microsoft Visual 2010 Express
On those three lines, you should be using a double equals sign for equality.
which three lines? and why is that?

[edit] oohhhh okay i got it!

Thanks alot!
Last edited on
Alright new question, why does this:


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 <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

	int formula = 1; // 1-7, BASED ON THE FORM
	double d = 0, d_0 = 0, V = 0, V_0 = 5, t = 5, a = 10, f = 0, m = 0;
	string missing = "d";

int main ()
{
    
    
//FORMULA NUMBERS ARE BASED ON THE FORM
if (formula == 1)
{
		//MANY IF STATEMENTS TO FIND OUT WHICH FORMULA TO USE
		if (missing == "d")
		{
		d = d_0+V_0*t+(1/2)*a*pow(t,2);
		//DISPLAY ANSWER
		cout << "the answer is" << d;
		cin >> d_0;
		}
}
}

return (0)


spit out 25 instead of 150 (which is the actual answer)?
Last edited on
Line 44. Change else if (missing = "V_0") to else if (missing == "V_0").

The single = sign is performing an assignment. The compiler is complaining because after assigning "V_0" to missing it can't convert the string missing to a boolean as required by the if statement. Use == for the comparison as you have done elsewhere.

Same thing happens on lines 56 and 60.
Last edited on
The problem is the 1/2

As 1 and 2 are both integers, the compiler uses integer division for this calculation: 1/2 = 0 (actually, 0 remainder 1 -- use 1%2 = 1 for the remainder)

d = d_0+V_0*t+(1/2)*a*pow(t,2); => 25

(= 0 + (5 * 5) + (0 * 10 * 25) = 25)

Adding .0 to the numeric constants, so the compiler knows they're doubles

d = d_0+V_0*t+(1.0/2.0)*a*pow(t,2.0); => 150

(= 0 + (5 * 5) + (0.5 * 10 * 25) = 150)

Andy

P.S. You could have changed just one constant to double format:
d = d_0+V_0*t+(1.0/2)*a*pow(t,2);
but I think it's good to be consistent!

Or even used one or more casts
d = d_0+V_0*t+((double)1/(double)2)*a*pow(t,2);

The type promotions rules mean that:
int/int -> int
double/double -> double
int/double -> double
double/int -> double
Last edited on
Wow, thanks andy. That was an awesome answer. I really appreciate it!
Topic archived. No new replies allowed.