Problem with a basic code

My code i as follows:

float radius;
String msg="What is the radius of your circle? ";
float area;
String msg2="Your circle has an area of: ";
float pi=3.14;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(msg);
while (Serial.available()==0);

radius=Serial.parseFloat();
area=piradiusradius;
Serial.print(msg2);
Serial.println(area);

}

//the result i get in my serial monitor is as follows:

What is the radius of your circle?
Your circle has an area of: 12.56
What is the radius of your circle?
Your circle has an area of: 0.00
What is the radius of your circle?

// I entered 2 as my variable and get the proper result. then I get the second entry of "Your circle has an area of: 0.00.

have not figured out why.

Change the line ending setting on the serial monitor to "no line ending" or "none" or whatever gives no line-ending character like CR or LF, or both..

Please remember to use code tags when posting code

Coding tags? I am very new to this. Thank you so very much for your assistance.

No, code tags.

Code tags?

Yes, that's right.

The things that stop your posted code making no sense.
(Hint: read it back and compare it to your actual code)

can you show me which line you are referring to?

I will go back and look at the posting requirements for code.
Thank you so very much. I started this just a few days ago and am now addicted.

The line in your original post that doesn't look like the same line in your original code.

consider (Arduino sprintf lacks floating pt formating)

String prompt ="What is the radius of your circle? ";

char s0 [80];
char s1 [20];
char s2 [20];

void setup() {
    Serial.begin(9600);
    Serial.println(prompt);
}

void loop() {
    if (! Serial.available())
        return;                     // nothing to do

    int n = Serial.readBytesUntil ('\n', s0, sizeof(s0));
    s0 [n] = 0;                     // replace '\n' with null

    float radius = atof (s0);
    float area   = PI * radius * radius;

    dtostrf (radius, 8, 3, s1);
    dtostrf (area,   8, 3, s2);
    sprintf (s0, " radius %s, area %s", s1, s2);    // reuse s0
    Serial.println (s0);

    Serial.println(prompt);
}

There is no need to re-invent PI, it's already defined for you in the core. Here is the source code:

#define PI 3.1415926535897932384626433832795

Hi, @krebad
Welcome to the forum.
Please read the post at the start of any forum , entitled "How to use this Forum".

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

There's more than one way to do it, by the way. You can click on the button marked </> while you are editing, you can use traditional [code] and [/code] tags, or (my favourite and the most versatile) you can post your code between 2 instances of 3 consecutive backticks, with the optional indication of the language (usually cpp), like so:

```cpp
// Your code goes here
```

At least, OP truncated it to 2 decimal places. Once upon a time, the State of Indiana did much worse.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.