I'm in a C++ class and have completed my assignment. Everything works correctly, but I was hoping for some tips or anything I could do to make it more streamlined.
I actually had this same assignment today. It's not quite as streamlined as some of the others, but it is pretty easy to follow I think and is a pretty simple way to do it.
// Find the area or circumference of a circle
#include <iostream>
usingnamespace std;
int main() {
cout << "I will tell you the area or circumference of your circle." << endl;
cout << "All I need is the radius." << endl;
cout << "First tell me what you would like me to do." << endl;
bool keep_going = true;
while(keep_going == true) {
int choice;
float radius;
// Prompts the user for which value they wish to compute.
cout << endl << "Enter '1' for the area" << endl;
cout << "Enter '2' for the circumference. " << endl;
cout << "Enter '0' to exit this program." << endl;
cin >> choice;
if(choice == 1 || choice == 2){
cout << endl << endl << "What is the radius of your circle: ";
cin >> radius;
}
// Gets the area of the circle.
if (choice == 1){
cout << "The area of your circle is: " << radius * radius * 3.14159 << endl;
keep_going = true;
}
// Get the circumference of the circle.
elseif (choice == 2) {
cout << "The circumference of your circle is: " << radius * 2 * 3.14159 << endl;
keep_going = true;
}
// Ends the program if something other than 1 or 2 is entered.
elseif (choice == 0) {
keep_going = false;
}
}
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
cout << "I will tell you the area or circumference of your circle." << endl;
cout << "All I need is the radius." << endl;
cout << "First tell me what you would like me to do." << endl;
int choice;
do
{
float radius;
// Prompts the user for which value they wish to compute.
cout << endl << "Enter '1' for the area" << endl;
cout << "Enter '2' for the circumference. " << endl;
cout << "Enter '0' to exit this program." << endl;
cin >> choice;
if(choice == 1 || choice == 2){
cout << endl << endl << "What is the radius of your circle: ";
cin >> radius;
}
// Gets the area of the circle.
if (choice == 1){
cout << "The area of your circle is: " << radius * radius * 3.14159 << endl;
}
// Get the circumference of the circle.
elseif (choice == 2) {
cout << "The circumference of your circle is: " << radius * 2 * 3.14159 << endl;
}
}
} while ( choice );
return 0;
}
Is it unnecessary to continually redefine keep_going as true when you can simply allow it to loop until you define it as false. The value is set to true at the beginning and if it wasn't true the program wouldn't be reading in the loop so it is just unnecessary to define it as true within the loop.
Ok thanks! Now that I'm a little further into the semester with it I noticed it. I appreciate the advice though. Everything I can learn certainly helps.