I'm trying to do a CSC project that involves basic arithmetic problems to calculate the final velocity of a problem. he problem is, I keep getting error messages, and I can't understand them enough to try and fix them. Any help would be immensely appreciated.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ( )
{
float partaccel, dist, veli, velisq, velf, velfm, 2as
// Get the particle acceleration.
cout << "Enter the acceleration of the particle in ft/s^2. ";
cin << partaccel;
// Get the distance covered by the particle.
cout << "Enter the distance covered by the particle in ft. ";
cin >> dist;
// Get the initial velocity.
cout << "Enter the initial velocity of the particle in ft/s. ";
cin >> veli >> endl;
cout << "A particle with a constant acceleration of " << setprecision(2) << partaccel << "ft/s^2 starting with an initial velocity of " << setprecision(5) << veli << "ft/s that
travels a distance of " << setprecision (5) << dist << "ft has a final velocity of " << setprecision(4) << velf << "ft/s which is equivalent to
" << setprecision(4) << velfm << "m/s." << endl;
well posting the error messages might be a good start. Also, please put put your code in code tags.
As far as I can see, the first line in main needs a semicolon at the end, cin >> veli >> endl is not valid, 'fixed' has not been declared yet you try to output it. There are probably more problems. Post the error messages
cout << "A particle with a constant acceleration of " << setprecision(2) << partaccel << "ft/s^2 starting with an initial velocity of " << setprecision(5) << veli << "ft/s that
travels a distance of " << setprecision (5) << dist << "ft has a final velocity of " << setprecision(4) << velf << "ft/s which is equivalent to
" << setprecision(4) << velfm << "m/s." << endl;
Here are the new error codes, and what semi colons am I missing?
Also, I'm sorry, I do have the closing bracket at the end of the code, I forgot to copy that line.
linearmotion.cpp:36:20: error: invalid suffix "as" on integer constant
linearmotion.cpp:40:173: warning: missing terminating " character
linearmotion.cpp:40: error: missing terminating " character
linearmotion.cpp:42:39: warning: missing terminating " character
linearmotion.cpp:42: error: missing terminating " character
linearmotion.cpp: In function âint main()â:
linearmotion.cpp:24: error: no match for âoperator<<â in âstd::cin << partaccelâ
linearmotion.cpp:32: error: no match for âoperator>>â in âstd::cin.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((double&)(& initVelocity))) >> std::endlâ
/usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:127: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:174: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:192: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:218: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:242: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:748: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/bits/istream.tcc:858: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/iomanip:184: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setfill<_CharT>) [with _CharT = char, _Traits = std::char_traits<char>]
linearmotion.cpp:34: error: âveliSquaredâ was not declared in this scope
linearmotion.cpp:36: error: âvelfâ was not declared in this scope
linearmotion.cpp:36: error: âvelisqâ was not declared in this scope
linearmotion.cpp:37: error: âvelfmâ was not declared in this scope
linearmotion.cpp:40: error: âveliâ was not declared in this scope
linearmotion.cpp:41: error: âtravelsâ was not declared in this scope
linearmotion.cpp:41: error: expected `;' before âaâ
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main ()
{
float partaccel, dist, veli, velisq, velf, velfm, 2as
// Missing a ; on previous line
// Cannot make any variable with a number as the first char.
cout << "Enter the acceleration of the particle in ft/s^2. ";
cin << partaccel; // Your other cins are right. This one is left. HINT HINT
cout << "Enter the distance covered by the particle in ft. ";
cin >> dist;
cout << "Enter the initial velocity of the particle in ft/s. ";
cin >> veli >> endl;
// Whats that endl doing there?
// You need to cout a new endl. You cannot cin.
velisq = pow(veli, 2.0);
2as = 2 * partaccel * dist;
velf = sqrt (2as * velisq); // Variable is invalid. Mentioned above.
velfm = velf * 3.281;
// This is fine:
cout << fixed;
cout << "A particle with a constant acceleration of "
<< setprecision(2) << partaccel << " ft/s^2 starting with an initial velocity of "
<< setprecision(5) << veli << " ft/s that travels a distance of "
// Something happened here. You cannot have "text
// newline text" You must surround it on both sides with "". This is valid:
/* cout << "Text"
* "Text"
* "text"
* << endl;
// This is not.
* cout << "Text line goes
* Over two lines
* without quotes on the ends of each line.";
*/
<< setprecision(5) << dist << "ft has a final velocity of "
<< setprecision(4) << velf << "ft/s which is equivalent to "
<< setprecision(4) << velfm << " m/s." << endl;
// RETURN 0;
return 0;
}
Alright, all that text is supposed to be a solid paragraph. The program keeps breaking it up. If I enter it in the
/* cout << "Text"
* "Text"
* "text"
* << endl;
style, will it still be a solid paragraph, but organized so the program will recognize it?
And thanks everyone so much for your help. I've been struggling with this since 10AM and my brain is starting to blank on me. I'm extremely sorry for being annoying.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main ()
{
float partaccel, distance, initVelocity, velocitySquared, finalVelocity, finalVelocitySquared, twoAccelDist, velocityMeters
// Get the particle acceleration.
cout << "Enter the acceleration of the particle in ft/s^2. ";
cin >> partaccel;
// Get the distance covered by the particle.
cout << "Enter the distance covered by the particle in ft. ";
cin >> distance;
// Get the initial velocity.
cout << "Enter the initial velocity of the particle in ft/s. ";
cin >> initVelocity;
cout << endl;
finalVelocitySquared = pow(initVelocity, 2.0);
twoAccelDist = 2 * partaccel * distance;
finalVelocity = sqrt (twoAccelDist * finalVelocitySquared);
velocityMeters = finalVelocity * 3.281;
cout << fixed;
cout <<
"A particle with a constant acceleration of " << setprecision(2) << partaccel << "ft/s^2 starting with"" an initial velocity of " << setprecision(5) << initVelocity << "ft/s that"" travels a distance of " << setprecision(5) << distance << "ft has a ""final velocity of " << setprecision(4) << finalVelocity << "ft/s which"" is equivalent to " << setprecision(4) << velocityMeters << "m/s." << endl;
return 0;
}
And the error message I'm getting is
1 2 3
linearmotion.cpp: In function âint main()â:
linearmotion.cpp:23: error: expected initializer before âcoutâ
linearmotion.cpp:38: error: âvelocityMetersâ was not declared in this scope
Which confuses me, because I declared velocityMeters. As far as the initializer thing, I really don't understand that. I'm really new at C++.