Answer to Question #178135 in C++ for Talha

Question #178135

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



1
Expert's answer
2021-04-04T23:14:47-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS