OK, with some help from all of you, I have this working, but I am at a loss as to how to display the number of steps the user asks to show (if they say 10, then every tenth answer displays until the final one) and we need this to display 9 decimal places which I can do with setprecision I think. Then I will have a final answer to display once all is said and done. How in the world would I even begin to get it to display this?

You all rock!

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//function to read inputs from the user test for proper input
//and return the value to main if input is invalid,
//the user will be reprompted for proper input

int validInput (string prompt)

{

int userIn1 = 0;

cin >> userIn1; cout << endl;

while (userIn1 < 1)
{
cout << "Error, cannot be 0 or negative, please try again" << endl;
cin >> userIn1; cout << endl;
}

return userIn1;
}

int main()
{

double pi = 0;
int numCalc = 0;
int userIn1 = 0;
int times = 0;
string userInput = "Please enter the number of terms to use: ";
string userDisplay = "Please enter how often to display steps: ";

cout << "This program calculates the value of PI to varying degrees of accuracy " << endl;
cout << endl;
cout << "The accuracy is based on the desired number of calculations requested by user" << endl;
cout << "The user may also decide the sequence of displayed output during calculations" << endl;
cout << endl << endl;

cout << userInput << endl;

numCalc = validInput (userInput);

cout << userDisplay << endl;

times = validInput (userDisplay);

// Calculating pi/4
for (double n = 1; n <= numCalc; n++)

{
pi += (double) pow(-1, n+1)/(2*n-1);
}
// Calculating pi
pi *= 4;

cout << pi << endl;

cout << endl << endl;
system ("PAUSE");
return 0;

}
2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by cougarclaws

Here is an example :

int howMany = 0;
cin >> howMany;
for(int i = 0; i < 100; i ++)
if(i % howMany == 0) cout << i;

I am still confused, but I see where you are going. I will plat with it some to see how I can incorporate it.

Thanks!

You ask the user to give you a number so you can
show every multiple answers of that number.

so int your loop check if the index 'i' is a multiple of that number,
if so then print the answer. Using the mod operator gives
the remainder of the division. If the remainder is a 0 , then the
number is a multiple of that number.

for example : 6 % 2 = 0. because 2 goes into 6 3 times with no
remainder.

First, thank you again for all your time and hard work.

I think I have this now.

Only 3 more weeks to go, then on to the next class! I am so happy for these forums and people like yourself who are willing to help out.

Cougarclaws