7 Replies - 1003 Views - Last Post: 15 February 2009 - 03:11 AM Rate Topic: -----

#1 bishopssix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-February 09

Trouble converting a program into user-defined functions

Posted 12 February 2009 - 07:47 PM

Well, I had this code done to find the biggest number out of a set amount of numbers and one to get smallest out of an unset amount of numbers and I need to change the biggest and smallest to use user defined functions, this is confusing me, I know how to do simple functions, addition subtraction etc but this one got me mixed up here's what i tried which failed.

 #include <iostream>
#include <string>
#include <cstdlib> // re. atoi(..)

using namespace std;

int aNum;
int scanNum;
int getInt()
{
	string tmp;
	for(;;)
	{
		bool ok = true;
		cin >> tmp;
		for( unsigned i=0; i<tmp.length(); ++i )
		{
			if( i == 0 && tmp[0]=='-' ); // that's ok too ...
			else if( tmp[i]<'0' || tmp[i] > '9' )
			{
				cout << "Error ... integers only please : ";
				ok = false;
				break; // out of inner for loop
			}
		}

		if( ok ) // then we have a good integer ... so exit function now
		{
			return atoi( tmp.c_str() );
		}
		// ... else do outer 'forever loop' again ...
	}
}

void getBiggest()
{
	cout << "Please enter the number of integers you want to enter : ";
	int enteredNum = getInt();
	if (enteredNum > 0)
	{
		cout << "Enter integer #1 : ";
		int bigNum = getInt(); // get first number into bigNum ...
		// now ... get the rest ... and compare each ...
		for(int i = 1; i < enteredNum; i++)
		{
			cout << "Enter integer #" << i+1 << " : ";
			int scanNum = getInt();
			if(scanNum > bigNum) (bigNum = scanNum);
		}
		cout << "Your largest integer is " << bigNum << "." << endl;
	}
}

bool more() // defaults to yes ...
{
	cout <<"More (y/n) ? ";
	string reply;
	getline( cin, reply );
	return !( reply[0] =='n' || reply[0] =='N' );
}

void getSmallest()
{
	cout << "Please enter the first number to compare : ";
	int smallNum = getInt();
	cout << "Now enter the rest of the numbers end with -99 when in a space between the next number.\n";
	do
	{
		cout << "Next number: ";
		int scanNum = getInt();
		cin >> aNum;
		if (scanNum < smallNum) smallNum = scanNum; // update smallNum ...
		
	}while(aNum != -99);
	cout << "Your smallest integer is " << smallNum << "." << endl;
}



int main()
{
	for(;;) // loop forever ... until return from main ...
	{
		cout << "\nPlease enter one of the following ...\n"
			 << "A - Find the largest number in a pre-fixed quantity of numbers.\n"
			 << "B - Find the smallest number in a user terminated input loop of numbers.\n"
			 << "C - Quit.\n" << endl;

		string input;
		cin >> input;
		if (input=="A" || input=="a") getBiggest();
		else if (input=="B" || input=="b") getSmallest();
		else if (input=="C" || input=="c")
		{
			cout << "Press 'Enter' to terminate the program ... " << flush;
			// while( cin.get() != '\n' ); // if you have a non standard compiler //
			cin.sync(); // flush cin stream ...
			cin.get();
			return 0;
		}
	}

	double biggest(
		int scanNum = getInt());{
	if(scanNum > bigNum) (bigNum = scanNum);
	return bigNum;
	}
}

any help would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble converting a program into user-defined functions

#2 Linkowiezi  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: Trouble converting a program into user-defined functions

Posted 13 February 2009 - 12:48 AM

Well, your functions and your program seems to be working.
Except for this part:
    double biggest(
        int scanNum = getInt());{
    if(scanNum > bigNum) (bigNum = scanNum);
    return bigNum;
    }

You haven't declared bigNum in main().
I honestly don't know what you are trying to do here, since you never reach this part of the program because you terminate the program from the infinite for loop.
So I'd just replace all of that with a simple return 0; and your program will compile and run just fine.

Hope this was of any help to you :)
Was This Post Helpful? 0
  • +
  • -

#3 bishopssix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-February 09

Re: Trouble converting a program into user-defined functions

Posted 13 February 2009 - 05:13 AM

I think I got it but I'm not sure i'll try doing what i'm thinking now, but i'm trying to get A and B into user-defined functions is all really, it's just that i don't really understand how to make big functions like that.


\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

nah, i got a bunch of errors

 #include <iostream>
#include <string>
#include <cstdlib> // re. atoi(..)

using namespace std;

double biggest({
int scanNum = getInt());{
if(int scanNum > int bigNum) (int bigNum = int scanNum);
return int bigNum;
}


int aNum;
int scanNum;
int getInt()
{
	string tmp;
	for(;;)
	{
		bool ok = true;
		cin >> tmp;
		for( unsigned i=0; i<tmp.length(); ++i )
		{
			if( i == 0 && tmp[0]=='-' ); // that's ok too ...
			else if( tmp[i]<'0' || tmp[i] > '9' )
			{
				cout << "Error ... integers only please : ";
				ok = false;
				break; // out of inner for loop
			}
		}

		if( ok ) // then we have a good integer ... so exit function now
		{
			return atoi( tmp.c_str() );
		}
		// ... else do outer 'forever loop' again ...
	}
}

void getBiggest()
{
	cout << "Please enter the number of integers you want to enter : ";
	int enteredNum = getInt();
	if (enteredNum > 0)
	{
		cout << "Enter integer #1 : ";
		int bigNum = getInt(); // get first number into bigNum ...
		// now ... get the rest ... and compare each ...
		for(int i = 1; i < enteredNum; i++)
		{
			cout << "Enter integer #" << i+1 << " : ";
			int scanNum = getInt();
			if(scanNum > bigNum) (bigNum = scanNum);
		}
		cout << "Your largest integer is " << bigNum << "." << endl;
	}
}

bool more() // defaults to yes ...
{
	cout <<"More (y/n) ? ";
	string reply;
	getline( cin, reply );
	return !( reply[0] =='n' || reply[0] =='N' );
}

void getSmallest()
{
	cout << "Please enter the first number to compare : ";
	int smallNum = getInt();
	cout << "Now enter the rest of the numbers end with -99 when in a space between the next number.\n";
	do
	{
		cout << "Next number: ";
		int scanNum = getInt();
		cin >> aNum;
		if (scanNum < smallNum) smallNum = scanNum; // update smallNum ...
		
	}while(aNum != -99);
	cout << "Your smallest integer is " << smallNum << "." << endl;
}



int main()
{
	for(;;) // loop forever ... until return from main ...
	{
		cout << "\nPlease enter one of the following ...\n"
			 << "A - Find the largest number in a pre-fixed quantity of numbers.\n"
			 << "B - Find the smallest number in a user terminated input loop of numbers.\n"
			 << "C - Quit.\n" << endl;

		string input;
		cin >> input;
		if (input=="A" || input=="a") getBiggest();
		else if (input=="B" || input=="b") getSmallest();
		else if (input=="C" || input=="c")
		{
			cout << "Press 'Enter' to terminate the program ... " << flush;
			// while( cin.get() != '\n' ); // if you have a non standard compiler //
			cin.sync(); // flush cin stream ...
			cin.get();


	double biggest(
		int scanNum = getInt());{
	if(int scanNum > int bigNum) (int bigNum = int scanNum);
	return bigNum;
	}

			return 0;
		}
	}


  

}
} 


------ Build started: Project: week6, Configuration: Debug Win32 ------
Compiling...
week6.cpp
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(7) : error C2143: syntax error : missing ')' before '{'
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(8) : error C2059: syntax error : ')'
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(8) : error C3861: 'getInt': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(9) : error C2059: syntax error : '>'
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(10) : error C2062: type 'int' unexpected
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(17) : error C2601: 'getInt' : local function definitions are illegal
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(7): this line contains a '{' which has not yet been matched
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(43) : error C2601: 'getBiggest' : local function definitions are illegal
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(7): this line contains a '{' which has not yet been matched
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(62) : error C2601: 'more' : local function definitions are illegal
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(7): this line contains a '{' which has not yet been matched
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(70) : error C2601: 'getSmallest' : local function definitions are illegal
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(7): this line contains a '{' which has not yet been matched
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(88) : error C2601: 'main' : local function definitions are illegal
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(7): this line contains a '{' which has not yet been matched
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(45) : error C3861: 'getInt': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(49) : error C3861: 'getInt': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(54) : error C3861: 'getInt': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(72) : error C3861: 'getInt': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(77) : error C3861: 'getInt': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(78) : error C2065: 'aNum' : undeclared identifier
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(81) : error C2065: 'aNum' : undeclared identifier
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(98) : error C3861: 'getBiggest': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(99) : error C3861: 'getSmallest': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(109) : error C3861: 'getInt': identifier not found
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(110) : error C2059: syntax error : '>'
c:\documents and settings\bishopssix\my documents\visual studio 2008\projects\week6\week6\week6.cpp(111) : error C2065: 'bigNum' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\BishopsSix\My Documents\Visual Studio 2008\Projects\week6\week6\Debug\BuildLog.htm"
week6 - 22 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This post has been edited by bishopssix: 13 February 2009 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 bishopssix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-February 09

Re: Trouble converting a program into user-defined functions

Posted 14 February 2009 - 09:10 AM

trying to do this I think is right but i keep getting this error (14) : error C2064: term does not evaluate to a function taking 1 arguments

  #include <iostream>
#include <string>
#include <cstdlib> // re. atoi(..)

using namespace std;

int aNum;
int scanNum;
int bigNum;
int getInt();

double biggest
(
(scanNum > bigNum)(bigNum = scanNum)
);
{
	string tmp;
	for(;;)
	{
		bool ok = true;
		cin >> tmp;
		for( unsigned i=0; i<tmp.length(); ++i )
		{
			if( i == 0 && tmp[0]=='-' ); // that's ok too ...
			else if( tmp[i]<'0' || tmp[i] > '9' )
			{
				cout << "Error ... integers only please : ";
				ok = false;
				break; // out of inner for loop
			}
		}

		if( ok ) // then we have a good integer ... so exit function now
		{
			return atoi( tmp.c_str() );
		}
		// ... else do outer 'forever loop' again ...
	}
}

void getBiggest()
{
	cout << "Please enter the number of integers you want to enter : ";
	int enteredNum = getInt();
	if (enteredNum > 0)
	{
		cout << "Enter integer #1 : ";
		int bigNum = getInt(); // get first number into bigNum ...
		// now ... get the rest ... and compare each ...
		for(int i = 1; i < enteredNum; i++)
		{
			cout << "Enter integer #" << i+1 << " : ";
			int scanNum = getInt();
			if(scanNum > bigNum) (bigNum = scanNum);
		}
		cout << "Your largest integer is " << bigNum << "." << endl;
	}
}

bool more() // defaults to yes ...
{
	cout <<"More (y/n) ? ";
	string reply;
	getline( cin, reply );
	return !( reply[0] =='n' || reply[0] =='N' );
}

void getSmallest()
{
	cout << "Please enter the first number to compare : ";
	int smallNum = getInt();
	cout << "Now enter the rest of the numbers end with -99 when in a space between the next number.\n";
	do
	{
		cout << "Next number: ";
		int scanNum = getInt();
		cin >> aNum;
		if (scanNum < smallNum) smallNum = scanNum; // update smallNum ...
		
	}while(aNum != -99);
	cout << "Your smallest integer is " << smallNum << "." << endl;
}



int main()
{
	for(;;) // loop forever ... until return from main ...
	{
		cout << "\nPlease enter one of the following ...\n"
			 << "A - Find the largest number in a pre-fixed quantity of numbers.\n"
			 << "B - Find the smallest number in a user terminated input loop of numbers.\n"
			 << "C - Quit.\n" << endl;

		string input;
		cin >> input;
		if (input=="A" || input=="a") getBiggest();
		else if (input=="B" || input=="b") getSmallest();
		else if (input=="C" || input=="c")
		{
			cout << "Press 'Enter' to terminate the program ... " << flush;
			// while( cin.get() != '\n' ); // if you have a non standard compiler //
			cin.sync(); // flush cin stream ...
			cin.get();


	double biggest(
		
	(int scanNum > int bigNum) (int bigNum = int scanNum)
	return bigNum;
	}

			return 0;
		}
	} 

 

Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Trouble converting a program into user-defined functions

Posted 14 February 2009 - 03:01 PM

What is this block of code supposed to do?

double biggest
(
(scanNum > bigNum)(bigNum = scanNum)
);

Was This Post Helpful? 0
  • +
  • -

#6 bishopssix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-February 09

Re: Trouble converting a program into user-defined functions

Posted 14 February 2009 - 06:11 PM

disregard this thread
Was This Post Helpful? 0
  • +
  • -

#7 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Trouble converting a program into user-defined functions

Posted 15 February 2009 - 02:15 AM

:: for God Sake is anyone of you a programmer by the way ?!!

:: you can't return a functional variable ...

:: return in main function means number of errors


 if (input=="A" || input=="a") getBiggest(); // this return a value !

 double biggest(  // what does this mean ???

 (int scanNum > int bigNum) (int bigNum = int scanNum)
	return bigNum;  // hello .. is somebody there ?!!

 return 0; // the program will terminate after loop 1 time !!!
		}
	}




:: all i can say is .. you need to rewrite your code,
Was This Post Helpful? 0
  • +
  • -

#8 bishopssix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-February 09

Re: Trouble converting a program into user-defined functions

Posted 15 February 2009 - 03:11 AM

insults are the way to go...anyway all i had was a misunderstanding of how to do the function formatting correctly i found out how i fixed it all works well now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1