Create a file named “Circle.txt” and write the following data:(10)
Circle 1, 5
Circle 2,10
Circle 3,4
Each Line has a Circle Name 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 <fstream>
#include <string>
using namespace std;
int main(){
const float pi = 3.14;
string s[3];
fstream circle;
float area;
circle.open("Circle.txt", ios::in);
if(!circle)
cout<<"File not found!";
else
for(int i = 0; i < 3; i++){
getline(circle, s[i]);
area = stoi(s[i].substr(s[i].find(",") + 1)) * stoi(s[i].substr(s[i].find(",") + 1)) * pi;
s[i] += ", " + (to_string(area)).substr(0, (to_string(area)).find(".") + 3);
}
circle.close();
circle.open("Circle.txt", ios::out);
for(int i = 0; i < 3; i++){
circle<<s[i]<<endl;
}
circle.close();
return 0;
}
Comments
Leave a comment