-3

Hey guys was wondering if you could help me out with this code below. My program is outputting some weird calculations.

#include <iostream>

using namespace std;

int main() 

{    

    int radius;
    const double PI = 3.14159265;
    float area;
    float circumference;


    cout << "Program to find the area and circumference of a circle\n\n\n";
    cout << "Please enter the radius: ";
    radius = cin.get();

    area = PI * (radius * radius);
    circumference = (2 * radius) * PI;

    cout << "The area of your circle is " << area << ", the circumference of your circle is "   << circumference <<"\n\n";
    system("PAUSE");

}
jrd1
  • 10,358
  • 4
  • 34
  • 51
Daniel M
  • 15
  • 1
  • 4
    For some example input, what do you expect it to output, and what does it actually output? – BoBTFish Jul 25 '13 at 08:38
  • What is `cin.get()`? Why not use the stream conversion `radius << cin;`? – wallyk Jul 25 '13 at 08:39
  • Aside from the misuse of `cin.get()`, be aware that `(radius * radius)` could easily overflow prior to being cast to `float`. – Brett Hale Jul 25 '13 at 08:43
  • 1
    @BrettHale It will actually be cast to `double`, as it is being multiplied by `PI`. Then the result will be cast to `float`, losing precision. But given that the asker is clearly a beginner, I decided not to go there in my answer. And to be honest, people do `int` multiplications all the time, it's no more of a problem here than anywhere else. Although you do of course have to be aware of the problem, but again, beginners, one thing at a time. – BoBTFish Jul 25 '13 at 08:45
  • @BoBTFish - You're right. I missed that. – Brett Hale Jul 25 '13 at 08:46
  • 1
    Hi Daniel. Welcome to S.O. As BoBTFish already said, you should really include the output of your program - and what you expect - when you ask a question where you think your program outputs something wrong. Also: When posting source code, do format it accordingly. BoBTFish was so nice to edit it for you, but its only one button away for yourself. See the help: http://stackoverflow.com/help – Martin Ba Jul 25 '13 at 08:59

5 Answers5

5

That's probably because you're using: cin.get().

cin.get() only obtains one character from the input stream.

What this means in practice is that if you had entered 1 at the prompt, that value would be stored as a character and not as a double value (which is what you really want).

And, given the ASCII representation of the character 1 being equal to 49 in decimal notation, that means that your input will be interpreted as 49 - which is syntactically correct, but conceptually wrong - hence your results.

More than likely you'll want more than that - you'll want all of your input to be stored correctly. Hence, in this case, it's best to use cin as follows:

cin >> radius;

Plus, a possible caveat of usage of cin like this is that it automatically skips whitespace - which is arguably good depending on its usage. Relative to your previous usage of cin (cin.get()) could cause more problems than its worth with the intentional or accidental usage of whitespace. So, in this case, it is arguably better to use streams as suggested above, to parse input.

Another option is to use the standard C library input to get input (if you don't like dealing with streams - and there is debate regarding streams usage in C++ vs C-style IO):

http://www.cplusplus.com/reference/cstdio/scanf/

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
0

cin.get() returns a character, when that gets converted to double, radius will get the wrong value. You should use stream conversion such as

cin >> radius;
Kristian Duske
  • 1,769
  • 9
  • 14
0

The istream::get function extracts a character and not a number. For most platforms that means that if you enter 5 you will assign 53 to radius as that is the [ASCII](http://www.asciitable.com/) value of'5'`.

Use the normal input operator >> to read the number, it will do the right thing no matter if radius is an integer or a floating point variable:

cin >> radius;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Try printing your radius value before using it and you will know

cin.get(); as the page says,

Reads one character and returns it if available. Otherwise, returns Traits::eof() and sets failbit and eofbit.

The number you type on your console is taken as a character and its ASCII value is used.

You need to use cin>>radius for this

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
0

Try printing out what you get after EVERY calculation, also bug might be cause of:

radius = cin.get();
//Use cin or scanf instead
cin >> radius;
scanf("%d",&radius);

Or because area and circumflex are (float) and receive a value type (double) at lines:

area = PI * (radius * radius);
circumference = (2 * radius) * PI;

I would suggest either casting, or making every variable type float, as sizeof(float) = sizeof(int) = 4 bytes.

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76