Answer to Question #329830 in C++ for zafar hayat wattoo

Question #329830

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


1
Expert's answer
2022-04-17T16:38:21-0400
#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;
}

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