Create a file named “Circle.txt” and write the following data:
Circle 1, 5
Circle 2,10
Circle 3,4
Each Line has a Circle Name(Circle 1 as in example in 1st line above) and its radius separated by a comma. You have to calculate the Area of
each circle using (pi*r*r) and write it at the end of every line. (5+ 10)
e.g.
Circle 1 , 5, 78.5
Circle 2, 10, 314
Circle 3, 4, 50.24
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
const float pi = 3.14159;
int main() {
std::ifstream is("Circle.txt");
std::vector<float> data;
std::string str;
while (std::getline(is, str)) {
int pos = str.find(",");
float radius = atof(str.substr(pos + 1).c_str());
data.push_back(radius);
}
std::ofstream os("Circle.txt");
for (int i = 0; i < data.size(); ++i) {
float r = data[i];
os << "Circle " << i + 1 << ", " << r << ", " << pi * r * r << '\n';
}
return 0;
}
Comments
Leave a comment