I have completed this project for my Computer Science I class. I received full credit because the program is functional with no issues at a glance. However, upon testing the program again (after it was graded), I found that under specific circumstances an infinite loop occurs. I couldn't go on with life until I figure out what I've done wrong! I have posted the full instructions below for reference:
An approximate value of pi can be calculated using the series given below:
pi = 4 [ 1 - 1/3 + 1/5 - 1/7 + 1/9 . . . + ((-1)^n)/(2n + 1) ]
Write a C++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation. Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.
Savitch, Walter J., and Kenrick Mock. "Chapter 3." Problem Solving with C++. 8th ed. Boston: Pearson Addison Wesley, 2012. 174. Print.
Here is the completed program:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int terms;
double pi = 1;
char restart = 'N';
// Do the entire program as long as the user wants
do
{
// Set pi back to 1 if user wants to restart
if ((restart == 'Y') || (restart == 'y'))
pi = 1;
cout << "Input the number of terms to approximate pi to. The number of terms must be greater than zero." << endl;
cout << ">> ";
cin >> terms;
// The number of terms must be greater than 0 for approximation purposes
if (terms > 0)
{
// The series approximates to the value terms times
for (int i = 1; i <= terms; i++)
{
pi += 4 * (pow(-1,i))/((2*i)+1);
}
// Once loop is complete, add 3 for final pi value
pi += 3;
cout << "The approximated value of pi is: " << pi << "." << endl << endl;
// Allow the user to restart program
cout << "Enter Y to start a new approximation or any other key to terminate the program." << endl;
cout << ">> ";
cin >> restart;
// A new line is inserted for spacing if the user wants to restart
cout << endl;
}
// Stop the program if the user enteres an invalid value.
else cout << endl << "You did not enter a valid value.\n" << endl;
} while ((restart == 'Y') || (restart == 'y'));
return 0;
}
Steps to produce error:
- Run program. Enter a number, as requested.
- Results are shown. Hit Y to restart program.
- Instead of typing a number, type a letter.
- Infinite loop occurs.
If the program is started and a letter is entered instead of a number, it does reject it and will end the program. The infinite loop occurs only after the program has been run at least one time. What it should do is always reject invalid entries, even after the program has been run once.
I would like advice on how I can make the program always stop the user if the user enteres incorrect information. It would also be nice to know how I can make the program restart if the user enters incorrect information instead of the program just ending.
I am constantly trying to improve my problem solving and programming skills, so offer advice for anything you see that could be better! Thank you.