Increment help??

Hey guys! In the process of learning C++ and my issue now is how to output a result depending on the user's wanted increments.

Say, the user requested the value of PI be evaluated in 5 steps, and to display a result every step, my program works. *See that the output shows "1. PI = XXX, 2. PI = XXX...5. PI = XXX" because I used displayCount++;
(line74)
Where I find my issue, is when the user wants PI evaluated at 500 steps, and results shown every 100th step. It does not work with displayCount++;

Does anyone have any suggestions as to how I can manipulate my program to output at increments chosen by the user??

***This may be clearer instruction:
After a valid number of terms has been entered, call the same user-defined function a second time, to read in a display count representing how often the program should display the current total value of the series. For example, if the user inputs 1, the result after every term will be output. If the user inputs 10, then the program should output after the 10th, 20th, 30th, etc. calculations (see sample outputs below). Using the same user-defined function will work, because the user input should again be positive and non-zero. Pass in the prompt for steps and return the validated step value to main.***

All help is greatly appreciated!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include <iostream>                                       // for I/O
#include <iomanip>                                        // for formatting output
#include <string>                                         // for using string data type
using namespace std;

// -------------------------------------------------------
// Function readInput verifies input is positive and non-zero
//   Input parameter: promptA
//   Returns: numberOfTerms
// -------------------------------------------------------
double readInput (string promptA)
{
	double numberOfTerms = 0;
	
    do
    {
        cout << promptA;
        cin >> numberOfTerms;
		if (numberOfTerms <= 0)
		{
            cout << "ERROR - Input must be positive and non-zero! Please try again." << endl << endl;
        }
	}
    while (numberOfTerms <= 0);
    
	return numberOfTerms;
}

int main ()
{
    double numberOfTerms = 0;
	double displayCount = 0;
	double pi = 0.0;
	double finalPi;
	int count = 1;
	double denom = 1;
	string promptA = "Enter the number of terms to use: ";
	string promptB = "Display Pi after every how many steps? ";
	bool condition = true;

 	cout << "Program will approximate Pi" << endl << endl;
	
    numberOfTerms = readInput (promptA);
    
    cout << endl;
 
    displayCount = readInput (promptB);
    
    cout << endl << endl;
	
    cout << "RESULTS: " << endl;
    
	do
	{
        if(condition)
		{	
		    pi += 4 / denom;
			condition = false;
		}
		else
		{
	        pi -= 4 / denom;
			condition = true;
		}
		
        denom+=2;
		count++;
		
        cout << endl << showpoint << fixed << setprecision (0) << displayCount << ": Pi = ";
		cout << showpoint << fixed << setprecision (9) << pi;
        
        finalPi = pi;
        displayCount++;
        
    }
        	
    while ( count <= numberOfTerms );
	
	cout << endl << endl << "Final Pi = " << finalPi << endl << endl;
	
    system ("pause");
	
    return 0;
}
Last edited on
Topic archived. No new replies allowed.