Click here to Skip to main content
15,900,570 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to return area of a circle and i have two radius in txt file but it returns only one radius for area so how can i return radius one by one for area?

What I have tried:

C++
#include <iostream>
using namespace std;
class Circle{
	
	private:
		double radius;
	public:
		Circle(){}
		double getArea();
};
double Circle::getArea()
{
	ifstream fin;
	fin.open("shape-in2.txt");
	
	char check;
	while(!fin.eof())
	{
		fin>>check;
		if(check=='C')
		{
			fin>>xCoordinate;
			fin>>yCoordinate;
			fin>>radius;
		}
	}
	return radius*radius*3.142;
	
}
int main(){
Circle c;
cout<<c.getArea();
}

/*file contains
C 3 6 7
B 1 2 3
C 5 7 8


*/
Posted
Updated 18-Dec-17 4:47am
v2
Comments
CPallini 18-Dec-17 8:33am    
I didn't get you. What do you want to do?

You can use a vector - C++ Reference[^] to store and return multiple values:
C++
#include <vector>

std::vector<double> Circle::getAreas()
{
    std::vector<double> result;
    ifstream fin;
    fin.open("shape-in2.txt");
    while(!fin.eof())
    {
        char check;
        fin>>check;
        // EDIT: Must read all data here; not only when first letter is C
        //if(check=='C')
        //{
            int xCoordinate, yCoordinate, radius;
            fin>>xCoordinate;
            fin>>yCoordinate;
            fin>>radius;
        if(check=='C')
        {
            // Append area value to vector
            result.push_back(radius*radius*3.142);
        }
    }
    return result;
}

int main()
{
    Circle c;
    std::vector<double> areas = c.getAreas();
    // Iterate over areas items here to print each value
    return 0;
}

[EDIT]
Note also that all data must be read from the file even when the first letter is not 'C'.
[/EDIT]
 
Share this answer
 
v2
Comments
CPallini 18-Dec-17 8:54am    
5.
Start by creating a class that deals only with values and calculations. The class constructor should take one parameter, the radius of the circle. You can then call a method to calculate and return the area. You can have another method that accepts the co-ordinates and shows the circle's position. Create a loop in your main function which reads the data file, creates circles for each set of values.
 
Share this answer
 
Comments
CPallini 18-Dec-17 8:54am    
5.
Quote:
How can I return a next value after returning previous value?

First step is correcting errors you get when you compile code.
You are using xCoordinate and yCoordinate, but you don't define them.
Then, you have logical errors: the code don't do what you want because you told it to do otherwise, there is no magic, you need to learn how things works. Use the debugger to see what your code does and how.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900