← Back to Kurakani

C++ guru, plz help

Original Post

I need to create a C++ program that calculates diameter, perimeter and area of a semicircle. The formula I used is: diameter = 2r; perimeter = r ( 2 + PI ); area = PI ( r-squired ) /2; How do I declare r? This is supposed to be in 1 nested if-then statement. I am new to c++ and I am LOST. Please help.

ujl
Sep 23, 2014 2:28 PM · 18,262 views
11 Replies
khoikkhoik Sep 23, 2014 3:13 PM
alece Sep 23, 2014 7:15 PM
#include using namespace std; int main(){ const float PI = 3.1459; float r; double diameter, perimeter, area; cout > r; diameter = 2 * r; perimeter = r*(2 + PI); area = pow(r, 2)* PI / 2; cout << "The diameter of circle is: " << diameter << endl; cout << "The perimeter of circle is: " << perimeter << endl; cout << "The area of the circle is: " << area << endl; system("pause"); }
ujl Sep 23, 2014 8:57 PM
Hi Alece, can you check where have i gone wrong? #include #include using namespace std; //Constant Declaration const float PI = 3.14159265; //Variable Declaration float r; int main() { string diameter; string perimeter; string area; float r; //Radius of a semicircle diameter, perimeter, area; cout > r; // ***** COMPUTATION ***** // Using nested if statements to calculate the diameter, // perimeter, and area based on formulas provided on // Prog1.txt { if ( r > 0 ) diameter = 2 * r; perimeter = r * ( 2 + PI ); area = PI * pow(r,2) / 2; else if ( r <= 0 ) cout << "You should have entered a positive value." << endl; } cout << "The diameter of a semicircle is " << diameter << endl; cout << "The perimeter of a semicircle is " << perimeter << endl; cout << "The area of a semicircle is " << area << endl; return 0; }
alece Sep 24, 2014 9:52 AM
I have no idea what you are doing bro...doesn't make any sense to me; Are you looking for something like this ? #include #include using namespace std; int main(){ const float pi = 3.14159; float r; double diameter, perimeter, area; string mystr; cout > r; if (r > 0){ if (mystr == "diameter"){ diameter = 2 * r; cout << "The diamter is :" << diameter << endl; } else if (mystr == "perimeter"){ perimeter = r*(2 + pi); cout << "The perimeter is :" << perimeter << endl; } else if (mystr == "area"){ area = pow(r, 2)*pi / 2; cout << "The area is :" << area << endl; } } else{ cout << "idk wth to do"; } system("pause"); }
alece Sep 24, 2014 9:54 AM
oh and include string and iostream library.... looks like sajha does not allow text between greater and less than sign to prevent XSS attack ?
ujl Sep 24, 2014 1:54 PM
Hi Alece, thank you for your help. If the user inputs r 0 ) } If command is diameter, do diameter = 2 * r else if command is perimeter do this... else if command is area do this.... } else if ( r < 0 ) } cout << "You should have enter a positive value for radius." << endl; } return o;
sajhamitra Sep 24, 2014 3:24 PM
sojoketo, C++ is still king of all programming languages so please don't under estimate it. Microsoft, Google, Amazons are still hiring those who are proficient in C++. Infact, If you are good at C++, Algorithm & Data Structure your chance of getting hired is very high in those companies.
alece Sep 24, 2014 3:43 PM
bro..what i have written works well...u can tweak a bit to make it work the way you want...i dont know what level you are in c++ but looking at above code..it shud be easier to achieve what you want...ur code has too many flaws...post what you have done so far and let us know where you get stuck...me writing all code wont help u in longer run..
sajhamitra Sep 24, 2014 8:43 PM
sojoketo, Well there are tons. How do I know?? Well I used to work for one of those companies. Check to see their SDE, SDET role's JD. Most of them requires you to have either proficient in C++ or C# or Java. It's not because you are going to work in C++ but if you are confident in C++ then you can crack any code. Not to mention they have managed C++ too. Learning new programming language syntax is not a big deal. But you should know the core. I have seen people using C++ for middle tier applications too. So it's not just system programming. and your quote "but i don't think we Nepalese are that level flexible (financially and technically)into system programming," is very funny dude. I have seen lot of nepalese doing system programming too. Please don't be so stereotype :). Btw, Are you working in Big Data? I am also marching toward that career.
aaitey Sep 25, 2014 6:44 AM
चट्ट लाएर ग्याँस स्टेसनमा घण्टा हान्ने, क्यासमा काम गर्ने, फुडस्ट्याम्प खाने, सरकारी ईन्सुरेन्स लिने, सुटकेशमा डलर ढुसी लागुन्जेल जम्मा गर्न छोडेर, किन c++ हो कि v++ तिर लाग्न थाले केटकेटीहरु :)
rufuz Sep 28, 2014 10:10 AM
Ok this is where you are doing wrong. You are declaring diameter perimeter area as a string and calculating as a numerical value. You can not compute math on string declaration. Its is good practice to initialize the values of diameter perimeter area to 0.0. Also read the precedence of mathematical signs in c++. Whatever is in () is carried out first so make sure you have first calculated area= (pi *pow(r,2))/2; For logical condition: If (r>0.0){ Do this } Else { output this } Hope that helps.
Login to join the conversation.

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all
Ask Sajha AI Assistant