need help with calculations

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 19
Reputation: slawted is an unknown quantity at this point 
Solved Threads: 0
slawted slawted is offline Offline
Newbie Poster

need help with calculations

 
0
  #1
Jun 15th, 2009
hello this code seems to give me wrong results and i carnt figure out why

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: need help with calculations

 
0
  #2
Jun 15th, 2009
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'...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: slawted is an unknown quantity at this point 
Solved Threads: 0
slawted slawted is offline Offline
Newbie Poster

Re: need help with calculations

 
0
  #3
Jun 15th, 2009
  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)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: need help with calculations

 
0
  #4
Jun 15th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: slawted is an unknown quantity at this point 
Solved Threads: 0
slawted slawted is offline Offline
Newbie Poster

Re: need help with calculations

 
0
  #5
Jun 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: slawted is an unknown quantity at this point 
Solved Threads: 0
slawted slawted is offline Offline
Newbie Poster

Re: need help with calculations

 
0
  #6
Jun 15th, 2009
and to stop the program flashing of the screen?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: need help with calculations

 
0
  #7
Jun 15th, 2009
>...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; ...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: slawted is an unknown quantity at this point 
Solved Threads: 0
slawted slawted is offline Offline
Newbie Poster

Re: need help with calculations

 
0
  #8
Jun 15th, 2009
Woop everything works fine now, except the program flashes off straight away lol ? should std::cin.get(); stop that happening?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: slawted is an unknown quantity at this point 
Solved Threads: 0
slawted slawted is offline Offline
Newbie Poster

Re: need help with calculations

 
0
  #9
Jun 15th, 2009
anyone?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: need help with calculations

 
0
  #10
Jun 15th, 2009
Insert std::cin.ignore(1000,'\n'); after std::cin >> diameter;
Last edited by ArkM; Jun 15th, 2009 at 3:45 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC