Jump to page: 1 2
Thread overview
Circle Calculator Help
Jun 26, 2012
Alexander
Jun 26, 2012
Alexander
Jun 26, 2012
Adam D. Ruppe
Jun 26, 2012
Alexander
Jun 26, 2012
Timon Gehr
Jun 26, 2012
Alexander
Jun 26, 2012
Alexander
Jun 26, 2012
bearophile
Jun 26, 2012
Alexander
Jun 26, 2012
bearophile
Jun 26, 2012
Sean Kelly
Jun 27, 2012
Timon Gehr
June 26, 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
	
June 26, 2012
By the way, this is just my 2nd program in D.
It is just for fun, and I'm doing it to practice using this language.

This is normally how I teach myself languages.
I write a simple one then a more complex one on and on until I'm fluent in the language.
I don't have the book (but I'll get it soon), and I've just tried to use online references for this so far.

Thanks again!


June 26, 2012
You should be able to just do a readln() before exiting
to give the user a chance to read everything, and hit
enter to exit.
June 26, 2012
On Tuesday, 26 June 2012 at 14:53:33 UTC, Adam D. Ruppe wrote:
> You should be able to just do a readln() before exiting
> to give the user a chance to read everything, and hit
> enter to exit.

I just tried that, and it still closed right after I typed the radius.
It looks almost like error code that pops up, but it closes so fast there's no way I could possibly read it.
June 26, 2012
On Tue, 26 Jun 2012 10:40:18 -0400, Alexander <alexander@alexandermohn.com> wrote:

> 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'm assuming you are running on Windows.  Windows with console output will allocate a console, and destroy the console after the program is finished.

If you want it to keep the console up, start the command line interpreter (cmd.exe) before-hand and then run the program from within the console. Then all the output stays put.


> //Wait
> void wait()
> {
> 	writefln ("Type A to continue!");
> 	exittest();
> }
>
> //Exit tester
> void exittest()
> {
> 	char[] a;
> 	stdin.readln(a);
> 	if (a == "A")
> 	{
> 		exit();
> 	}
> 	else
> 	{
> 		wait();
> 	}
> }

Hm... interesting code here :)

This is not horrible, just... weird.

You probably want to avoid recursion in this case:

void wait()
{
   char[] a;
   while(a != "A")
   {
       writeln("Type A to continue!");
       stdin.readln(a);
   }
}

-Steve
June 26, 2012
On 06/26/2012 09:43 PM, Steven Schveighoffer wrote:
> On Tue, 26 Jun 2012 10:40:18 -0400, Alexander
> <alexander@alexandermohn.com> wrote:
> ...
>> //Wait
>> void wait()
>> {
>>     writefln ("Type A to continue!");
>>     exittest();
>> }
>>
>> //Exit tester
>> void exittest()
>> {
>>     char[] a;
>>     stdin.readln(a);
>>     if (a == "A")
>>     {
>>         exit();
>>     }
>>     else
>>     {
>>         wait();
>>     }
>> }
>
> Hm... interesting code here :)
>
> This is not horrible, just... weird.
>

It is functional style. ;)

> You probably want to avoid recursion in this case:
>
> void wait()
> {
>     char[] a;
>     while(a != "A")
>     {
>         writeln("Type A to continue!");
>         stdin.readln(a);
>     }
> }
>
> -Steve

I want the two examples to generate comparable code.
June 26, 2012
So, I've taken out the loop part that is "interesting" and replaced it with the readln() alternative.
However, I'm still getting what looks like an error.
I managed to take a screenshot of what pops up, and here is what it says:

std.conv.ConvException@C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1597):
Unexpected '
' when converting from type char[] to type float
-----------------
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1597): float std.conv.toImpl!(float, char[]).toImpl(char[])
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(245): float std.conv.to!(float).to!(char[]).to(char[])
C:\Users\Alexander\Documents\D\circlecalc.d(23): float circlecalc.askradius()
C:\Users\Alexander\Documents\D\circlecalc.d(80): _Dmain
-----------------

Does anyone understand this error code?

I believe it means that it isn't letting me convert the user input into a floating number in function askradius().

How do I fix this?

(Just a note, the above part is popping up INSTEAD of what is supposed to pop up.)

Thanks!

June 26, 2012
I'll probably put in this alternative:

> void wait()
> {
>     char[] a;
>     while(a != "A")
>     {
>         writeln("Type A to continue!");
>         stdin.readln(a);
>     }

Thanks!
June 26, 2012
On Tue, 26 Jun 2012 16:39:07 -0400, Alexander <alexander@alexandermohn.com> wrote:

> So, I've taken out the loop part that is "interesting" and replaced it with the readln() alternative.
> However, I'm still getting what looks like an error.
> I managed to take a screenshot of what pops up, and here is what it says:
>
> std.conv.ConvException@C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1597):
> Unexpected '
> ' when converting from type char[] to type float
> -----------------
> C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1597): float std.conv.toImpl!(float, char[]).toImpl(char[])
> C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(245): float std.conv.to!(float).to!(char[]).to(char[])
> C:\Users\Alexander\Documents\D\circlecalc.d(23): float circlecalc.askradius()
> C:\Users\Alexander\Documents\D\circlecalc.d(80): _Dmain
> -----------------
>
> Does anyone understand this error code?

Oh, readln includes the newline by default, so to!float is choking on that.

Just remove the newline character:

radius = to!float(strip(number));

(must import std.string)

-Steve
June 26, 2012
Steven Schveighoffer:

> Oh, readln includes the newline by default, so to!float is choking on that.

Similar things happen often. But Andrei says this is good, because it's more orthogonal. As Sting, I don't subscribe to this point of view. Orthogonality isn't more important than practicality.

Bye,
bearophile
« First   ‹ Prev
1 2