Circle Calculator Help

Alexander alexander at alexandermohn.com
Tue Jun 26 07:40:18 PDT 2012


Hello everybody!

I am new to D, and I am working on a program that calculates the 
area and circumference of a circle. However, when I compile it 
and run it, it waits for the user to input the radius, but then 
it closes while displaying a bunch of stuff.
I've tried several ways to get it to wait, but none of them work.

Here's my latest code:

//Written in the D programming language! (Which rocks by the way!)
//Import the necessary modules
import std.stdio;
import std.math;
import std.conv;
import std.string;

//Welcome the user
int welcome()
{
	writefln ("Welcome to the Circle Calculator!");
	return 0;
}

//Ask for the radius
float askradius()
{
	char[] number;
	float radius;
	writefln ("What is the radius of your circle?\n");
	stdin.readln(number);
	radius = to!float(number);
	//Put the radius into a variable
	return radius;
}

//Show the user the results
void result()
{
	writefln ("The area is:", area());
	writefln ("The circumference is:", cir());
}

//Wait
void wait()
{
	writefln ("Type A to continue!");
	exittest();
}

//Exit tester
void exittest()
{
	char[] a;
	stdin.readln(a);
	if (a == "A")
	{
		exit();
	}
	else
	{
		wait();
	}
}

//Loop escape
void exit()
{
}

//Define pi
float pi()
{
	float pi = 3.14159265358979323;
	return pi;
}

//Calculate the square of the radius (for the area)
float sqradius()
{
	float sqradius = askradius() * askradius();
	return sqradius;
}

//Calculate area
float area()
{
	float area = pi() * sqradius();
	return area;
}

//Calculate double the radius (for the circumference)
float dbradius()
{
	float dbradius = askradius() * 2;
	return dbradius;
}

//Calculate circumference
float cir()
{
	float cir = pi() * dbradius();
	return cir;
	
}

void main()
{
	welcome();
	askradius();
	dbradius();
	sqradius();
	area();
	cir();
	result();
	wait();
}

How should I fix this so it will wait for the user to press a key 
to exit?

Thanks a ton!

-Alexander
	


More information about the Digitalmars-d-learn mailing list