Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem with For Loop?

$
0
0
Just looking for some insight here. When I first run the program, it outputs everything correctly. Then when I the user repeat the calculation, it just repeats the previous answer instead of doing the updated equation. Any thoughts? Code below:

#include <iostream>
#include <cmath>
//File Name:HW02_Project_11.cpp
//Description:Lesson 2, Project 11
//Last Changed:2/2/13
//Calculating e^x

using namespace std;

int main()
{
		
	double x,library;
	double fact=1; //factorial
	double ex=0; //answer for e^x
	

	char ans;
		
	cout.setf(ios::fixed);					
	cout.setf(ios::showpoint);				
	cout.precision(3);
	
	do
	{
	cout << "Enter a value for x: ";
	cin >> x;
	cout << endl;
	
	library = exp(x); //library answer
	
	cout << "Calculating e to the " << x << " power..." << endl;
	
	int n; //used for the counter
	for (n = 0;n <= 99;n++) //counter to put out 100 lines (10 x 10)
		{
							
		ex += pow(x,n) / fact; //calculation for e^x
		fact *= n + 1; //calculation for factorial (n!)
		cout << ex <<"\t";
		
							
		}
	
	
	cout << endl;
	cout << "Calculated e to " << x << " power: " << ex << endl;
	cout << "Library e to " << x << " power: " << library << endl;
	
	cout << "\nPerform another calculation? Press y or n " << endl;
	cin >> ans;
	
	cout << endl;
	
	}
	while (ans == 'y' || ans == 'Y');
  
	cout << "Goodbye" << endl;
	system("Pause");
	return 0;
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>