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...
/*
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>
usingnamespace 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;
}
elseif (missing == "d_0")
{
d_0 = 0;
//DISPLAY ANSWER
}
elseif (missing = "V_0")
{
V_0 = 0;
//DISPLAY ANSWER
}
elseif (missing = "t")
{
t = 0;
//DISPLAY ANSWER
}
elseif (missing = "a")
{
a = 0;
//DISPLAY ANSWER
}
}
elseif (formula == 2)
{
system("cls");
cout << "FORMULA 1" << endl;
}
elseif (formula == 3)
{
system("cls");
cout << "FORMULA 1" << endl;
}
elseif (formula == 4)
{
system("cls");
cout << "FORMULA 1" << endl;
}
elseif (formula == 5)
{
system("cls");
cout << "FORMULA 1" << endl;
}
elseif (formula == 6)
{
system("cls");
cout << "FORMULA 1" << endl;
}
elseif (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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
usingnamespace 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)?
Line 44. Change elseif (missing = "V_0") to elseif (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.
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