Lab 4 write your first, albeit, short C++ program. Your program will open a file
ID: 3623346 • Letter: L
Question
Lab 4
write your first, albeit, short C++ program. Your program will open a file named problem.txt and write to a file named solution.txt. The problem file will contain one of three of the trig function and an integer that represents an angle in degrees.
This program is intended to compute a basic trig funciton, either sine, cosine or tangent. Remember these functions are in the header file cmath and they expect their input to be a double that repesents an angle in radians. So you will have to convert the number you read in from degrees to radians.
You can use simple extraction to read these two pieces of input. I will use either, sin, cos, or tan. You should read this into a string variable. To use strings you need to #include and using std::string. You can extract data into a string, e.g. in >> myString; You can also use strings in an if condition.
Explanation / Answer
please rate - thanks#include <iostream>
#include <fstream>
#include <cmath>
#include <string.h>
using namespace std;
using std::string;
int main()
{ifstream input;
ofstream output;
string func;
int degrees;
double radians,value;
input.open("problem.txt"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 0;
}
output.open("solution.txt");
input>>func>>degrees;
radians=degrees*((22./7.)/180.);
if(func.compare("sin")==0)
value=sin(radians);
else if(func.compare("cos")==0)
value=cos(radians);
else
value=tan(radians);
output<<func<<"("<<degrees<<")="<<value<<endl;
input.close();
output.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.