First off I would like to state that this is a homework assignment. However, I am not looking for you to complete anything for me. I code I am posting is the completed homework assignment. However, since this is my first time using C++ I was curious if there might have been a better method to accomplish the same task.
/*
* PROJECT: Week 1 - Homework (Circle Calculations)
* VERSION: 0.0.0 as of 201210104
*
* DESC:
* In this homework assignment you will create a program that
* will calculate the area and circumference of a circle.
*
*/
#include <iostream>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
int main(){
const double PI = 3.14;
double radius;
double circumference;
double area;
// display backing
cout << "In order to determine a circle's circumference and area the";
cout << "\nradius is required." << endl;
// display question
cout << "\nWhat is the radius?" << endl;
cin >> radius; // except input
// do the math
circumference = PI * 2 * radius; // circumference of a circle
area = PI * pow(radius,2.0); // area of a circle
// display answer
cout << "\nA circle with a radius of " << radius << endl;
cout << "has a circumference of: " << circumference << endl;
cout << "has an area of: " << area << endl << endl;
system("pause");
return 0;
}
Here is the outcome of all changes I have made to the code above. I was able to solve how to handle an input other than of the the required data type.
/*
* PROJECT: Week 1 - Homework (Circle Calculations)
* VERSION: 1.0.0 as of 201210106
*
* DESC:
* In this homework assignment you will create a program that
* will calculate the area and circumference of a circle.
*
*/
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool GetInt(float & n)
{
string str;
getline(cin,str);
stringstream buffer(str);
buffer >> n;
if (!buffer)
{
cout << "non numerical data!" << endl;
return false;
}
if (!buffer.eof())
{
cout << "buffer not consumed!" << endl;
return false;
}
return true;
}
int main(){
const float PI = acos(-1);
float radius;
float circumference;
float area;
// display backing
cout << "In order to determine a circle's circumference and area the";
cout << "\nradius is required." << endl;
// display question
cout << "\nWhat is the radius? (type float)" << endl;
while (true){
cout << "enter a floating point number (0 to quit): ";
if (!GetInt(radius)){
cout << "\nyou did not enter a floating point number..." << endl;
}
else{
if (radius==0)
{
cout << "ok, bye!" << endl;
break;
}
// do the math
circumference = PI * 2 * radius; // circumference of a circle
area = PI * pow(radius,2.0); // area of a circle
// display answer
cout << "\nA circle with a radius of " << radius << endl;
cout << "has a circumference of: " << circumference << endl;
cout << "has an area of: " << area << endl << endl;
}
}
/*
using while loop to catch unexpected data types being
submitted, issue and error message, and ask for another
value to be submitted
while(!(cin >> radius)){// loop if not cin returns false
// display the error message
cout << "\nThat is not a floating point number." << endl;
cout << "Please enter a value that is a floating point number." << endl;
// display question again
cout << "\nWhat is the radius? (type float)" << endl;
// cin.clear() - reset the error control state
cin.clear();
/*
cin.ignore() - clear out last input sequence.
numeric_limits<streamsize>::max() - returns the max length of the
cin streamsize.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
*/
return 0;
}
