954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help with calculations

hello this code seems to give me wrong results and i carnt figure out why :(

#include <iostream>
int main() {
    
    float pi = 3.142;
    char area;
    float diameter;
    
    area = diameter * pi;
    
    std::cout << "Hello. Please imput the diameter of your circle... ";
    std::cin  >> diameter;
    
    std::cout << "The area of your circle is " << 'area' << ".\n";    
    std::cin.get();
    return 0;
}
slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Fill a glass with water (or what else) then drink, not vice versa ;)

Calculate a circle area AFTER its diameter request.
Output the area variable, not a wrong character literal 'area'...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
#include <iostream>
int main() {
    
    float pi = 3.142;
    char area;
    float diameter;
    
    std::cout << "Hello. Please imput the diameter of your circle... ";
    std::cin  >> diameter;
    
    area = diameter * pi;
    
    std::cout << "The area of your circle is " << area;    
    std::cin.get();
    return 0;
}


gives me a wierd symbol for the area? :( why is this i cannot see anything wrong with my code (newbie)

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

>gives me a wierd symbol for the area?
area is declared as a char, and cout is assuming you want to print a character rather than the integral value. Change char area; to int area; and you'll get a slightly more reasonable result.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thank you that has gave me a realistic result but it doesnt show the deicmals just rounds it to a whole number ? is there anyway to let it do this thank you once again

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

and to stop the program flashing of the screen?

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

>...why is this i cannot see anything wrong with my code...
Oh, this harmful Daniweb engine! I see 'area' in the OP snippet but area in the post #3 code ;)
By the way, change char area; to float area; ...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Woop :D everything works fine now, except the program flashes off straight away lol :( ? should std::cin.get(); stop that happening?

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

anyone?

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Insert std::cin.ignore(1000,'\n'); after std::cin >> diameter;

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Thank you man :D

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

I believe you still have a problem with the math?

Circumference = 2 .0 * PI * Radius;
Area = PI * Radius * Radius;

To borrow a very old math joke, "Pie aren't square, Pie are Round!"

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You