3 Replies - 2655 Views - Last Post: 29 September 2007 - 02:51 AM Rate Topic: -----

#1 ppilar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-September 07

user defined functions that return value

Post icon  Posted 29 September 2007 - 02:15 AM

I am trying to call a user defined function and then also return to main the outcome of this function below is my code which I had partially working at one point but now have managed to mess it all up. Any help out their
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

	int input(int termspi);
	
int main()
{
	  double pi;
	  const double NUMERATOR = 4.0;
	  int termspi;
	 pi = 0;	  
int countDenominator;
	  cout << "Program will approximate Pi" << endl;							
	  
	  
	  
	 input(int termspi);
	  
{
for (countDenominator=1; countDenominator <=(2*termspi-1); countDenominator += 2) 
	   if ((countDenominator -1)%4 ==0)
	 cout << NUMERATOR/(countDenominator);
else 
	 cout << NUMERATOR /(-countDenominator);
}	  
	
	  


cout << endl <<endl;
	cout << "Press [enter] to exit" << endl;
	cin.ignore();
	cin.get();
return 0;

	  }
	  
	  
	  int input(int termspi)
	  {
		   int termspi;
cout << "Enter the number of terms to use: ";
	  cin >> termspi;
	  cout << endl;
	  
	  while (termspi < 1){
	cout << "Your input is not valid" << endl;
	cout << "Enter a positive non zero number: " << endl;
	cin >> termspi;
	cout << endl;
	return termspi;
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: user defined functions that return value

#2 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: user defined functions that return value

Posted 29 September 2007 - 02:39 AM

What problems are you having? Do a syntax check. There are quite a few syntax errors in you code: the '{' before the for loop, incorrect method of calling a function, re-declaring a function's argument within the function.
Was This Post Helpful? 0
  • +
  • -

#3 ppilar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-September 07

Re: user defined functions that return value

Posted 29 September 2007 - 02:43 AM

I am getting all kinds of problems. (I don't know what a syntax error is) I can't get the loop to continue if the person keeps entering in negative or zero values for termspi. and I am getting errors that I don't undersand. I also can not get the function to return back to main



View Postppilar, on 29 Sep, 2007 - 02:15 AM, said:

I am trying to call a user defined function and then also return to main the outcome of this function below is my code which I had partially working at one point but now have managed to mess it all up. Any help out their
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

	int input(int termspi);
	
int main()
{
	  double pi;
	  const double NUMERATOR = 4.0;
	  int termspi;
	 pi = 0;	  
int countDenominator;
	  cout << "Program will approximate Pi" << endl;							
	  
	  
	  
	 input(int termspi);
	  
{
for (countDenominator=1; countDenominator <=(2*termspi-1); countDenominator += 2) 
	   if ((countDenominator -1)%4 ==0)
	 cout << NUMERATOR/(countDenominator);
else 
	 cout << NUMERATOR /(-countDenominator);
}	  
	
	  


cout << endl <<endl;
	cout << "Press [enter] to exit" << endl;
	cin.ignore();
	cin.get();
return 0;

	  }
	  
	  
	  int input(int termspi)
	  {
		   int termspi;
cout << "Enter the number of terms to use: ";
	  cin >> termspi;
	  cout << endl;
	  
	  while (termspi < 1){
	cout << "Your input is not valid" << endl;
	cout << "Enter a positive non zero number: " << endl;
	cin >> termspi;
	cout << endl;
	return termspi;
}
}


This post has been edited by ppilar: 29 September 2007 - 02:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: user defined functions that return value

Posted 29 September 2007 - 02:51 AM

Syntax error:
A programming error caused by a violation of the rules that specify how a command or instruction should be given to a computer.

Example: missing out a ; at the end of each line

It isn't returning to main because it seems to me you're having an infinite loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1