943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 693
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 15th, 2009
0

need help with calculations

Expand Post »
hello this code seems to give me wrong results and i carnt figure out why

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. int main() {
  3.  
  4. float pi = 3.142;
  5. char area;
  6. float diameter;
  7.  
  8. area = diameter * pi;
  9.  
  10. std::cout << "Hello. Please imput the diameter of your circle... ";
  11. std::cin >> diameter;
  12.  
  13. std::cout << "The area of your circle is " << 'area' << ".\n";
  14. std::cin.get();
  15. return 0;
  16. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slawted is offline Offline
19 posts
since Jun 2009
Jun 15th, 2009
0

Re: need help with calculations

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'...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 15th, 2009
0

Re: need help with calculations

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. int main() {
  3.  
  4. float pi = 3.142;
  5. char area;
  6. float diameter;
  7.  
  8. std::cout << "Hello. Please imput the diameter of your circle... ";
  9. std::cin >> diameter;
  10.  
  11. area = diameter * pi;
  12.  
  13. std::cout << "The area of your circle is " << area;
  14. std::cin.get();
  15. return 0;
  16. }

gives me a wierd symbol for the area? why is this i cannot see anything wrong with my code (newbie)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slawted is offline Offline
19 posts
since Jun 2009
Jun 15th, 2009
2

Re: need help with calculations

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 15th, 2009
0

Re: need help with calculations

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slawted is offline Offline
19 posts
since Jun 2009
Jun 15th, 2009
0

Re: need help with calculations

and to stop the program flashing of the screen?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slawted is offline Offline
19 posts
since Jun 2009
Jun 15th, 2009
0

Re: need help with calculations

>...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; ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 15th, 2009
0

Re: need help with calculations

Woop everything works fine now, except the program flashes off straight away lol ? should std::cin.get(); stop that happening?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slawted is offline Offline
19 posts
since Jun 2009
Jun 15th, 2009
0

Re: need help with calculations

anyone?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slawted is offline Offline
19 posts
since Jun 2009
Jun 15th, 2009
0

Re: need help with calculations

Insert std::cin.ignore(1000,'\n'); after std::cin >> diameter;
Last edited by ArkM; Jun 15th, 2009 at 3:45 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: pure virtual function in constructor
Next Thread in C++ Forum Timeline: installing GTK+ on Ubuntu





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC