I am trying to write a program that asks users for inputs mass and acceleration to calculate the force and then print it out. I don't know why it isn't compiling, however.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
double mass;
double acceleration;
double force;
std::cout <<"Please enter a number for mass (in kg)."<<;
std::cin >> mass<<;
std::cout <<"Please enter a number for acceleration (in m/s^2)"<<;
std::cin>>acceleration<<;
force=mass*acceleration;
std::cout<<"Here is the force for the numbers you provided."<< force <<;
You need to place your code in between the code tags.
It looks as if you placed the characters << after your cin statement before the semi-colon try removing them and remove the ones after your output statements as well
Thanks guys! I have it working now. Also, I was wondering if you guys knew a way to give the user an option to re-run the program?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
double mass;
double acceleration;
double force;
std::cout <<"Please enter a number for mass (in kg). \n";
std::cin >> mass;
std::cout <<"Please enter a number for acceleration (in m/s^2) \n";
std::cin>>acceleration;
force=mass*acceleration;
std::cout<<"Here is the force for the numbers you provided. \n"<< force;
I would place the executable code for gathering the data from the user into it own method that you call from within main(). Then you could place the method call for that newly created method within a while loop that asks the user to enter a value that you test to determine whether you would like to call that method again or exit the while loop and end the program.
void yourMethod()
{
//get data from user
//output results
}
int main()
{
bool answer = true;
while(answer)
{
yourMethod();
//ask and get answer to repeat then set variable answer
return 0;
}
}