Calling a function in main function

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1300 Views - Last Post: 25 April 2010 - 06:42 PM Rate Topic: -----

#1 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Calling a function in main function

Posted 23 April 2010 - 08:07 PM

I'm having trouble getting my program to work right. I'm trying to calling a function (in my case, isGeometric) in the void main() function. It compiles (although there are 5 warnings, not sure if that's has anything to do with it) but it outputs the address instead. My code:
#include <iostream>
#include <cstdarg>

using namespace std;
int a;
double num1;

int isGeometric (int a, double num1 ...);

void main()
{
	cout << "Geometric Sequence" << endl;
	cout << "Enter a number to be used as a common ratio." << endl;
	cin >> a;
	if (a <= 0)
	{
		cout << "Please enter a positive number." << endl;
		cin >> a;
	}
	else
		a = a;

	cout << "Enter a number to start calculating the geometric sequence." << endl;
	cin >> num1;
	if (num1 <= 0)
	{
		cout << "Please enter a positive number." << endl;
		cin >> a;
	}
	else
		num1 = num1;
	
	cout << "The geometric sequence with the common ratio you entered, " << a
		<< ", starting at " << num1 << " is: \t" << isGeometric
        << endl;
}

int isGeometric (int a, double num1 ...)
{
	va_list arguments;
    double num = num1, total = num;

    va_start(arguments, num1);
    do
    {
		total = a * num;
		cout << num << " ";
    } while( (num = va_arg(arguments, double) ) != 0);

    va_end(arguments);
    return total;
}


Feedback is appreciated, my assignment is due next week too just to throw that in there. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a function in main function

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,659
  • Joined: 19-February 09

Re: Calling a function in main function

Posted 23 April 2010 - 08:18 PM

You have the function declared like so,
int isGeometric (int a, double num1 ...);


I believe that you need to add a comma before the 3 dots, like so.
int isGeometric (int a, double num1, ...);

Was This Post Helpful? 0
  • +
  • -

#3 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: Calling a function in main function

Posted 24 April 2010 - 09:16 AM

Thanks! I added the comma and when I run the program, it still prints the address and not the results I'd like. I think the problem is how I'm calling the function in main() but I don't know how to fix it. Sorry if it's a silly question, I'm really new to this!
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Calling a function in main function

Posted 24 April 2010 - 09:18 AM

Can you show me the line of code where you call the function>
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Calling a function in main function

Posted 24 April 2010 - 09:18 AM

"void main()" is wrong. Always use "int main()".
Read here the words of the man who invented C++:
http://www.research.....html#void-main
Can I write "void main()"?
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it ever been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1.


Why are these being declared as globals?
int a;
double num1;


Show us your code as it is now.

This post has been edited by janotte: 24 April 2010 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: Calling a function in main function

Posted 24 April 2010 - 11:40 AM

My code is now as follows after making a few changes:
#include <iostream>
#include <cstdarg>

using namespace std;

int isGeometric (int a, double num1, ...);

void main()
{
	int a;
	double num1;
	cout << "Geometric Sequence" << endl;
	cout << "Enter a number to be used as a common ratio." << endl;
	cin >> a;
	if (a <= 0)
	{
		cout << "Please enter a positive number." << endl;
		cin >> a;
	}
	else
		a = a;

	cout << "Enter a number to start calculating the geometric sequence." << endl;
	cin >> num1;
	if (num1 <= 0)
	{
		cout << "Please enter a positive number." << endl;
		cin >> a;
	}
	else
		num1 = num1;
	
	cout << "The geometric sequence with the common ratio you entered, " << a
		<< ", starting at " << num1 << " is: \t" << isGeometric
        << endl;
}

int isGeometric (int a, double num1, ...)
{
	va_list arguments;
    double num = num1, total = num;

    va_start(arguments, num1);
    do
    {
		total = a * num;
		cout << num << " ";
    } while( (num = va_arg(arguments, double) ) != 0);

    va_end(arguments);
    return total;
}


I use void main() because it's what my professor told us to use, unless the function is returning a value.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Calling a function in main function

Posted 24 April 2010 - 11:46 AM

WTF?? where do they hire programming professors these days?

But you need to pass parameters to your function
        cout << "The geometric sequence with the common ratio you entered, " << a
                << ", starting at " << num1 << " is: \t" << isGeometric << endl; //<---


Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Calling a function in main function

Posted 24 April 2010 - 11:47 AM

Quote

I use void main() because it's what my professor told us to use, unless the function is returning a value.
Tell your professor to buy a copy of the standard (about $30, I think?) and read the section on the main function. It says absolutely that main must return int.
Was This Post Helpful? 0
  • +
  • -

#9 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: Calling a function in main function

Posted 24 April 2010 - 12:29 PM

Oh, haha, he's been a professor for years. I passed the parameters to the function and now get 3 errors.
	cout << "The geometric sequence with the common ratio you entered, " << a
		<< ", starting at " << num1 << " is: \t" << isGeometric (int &a, double &num1, ...)
        << endl;

The errors are:
.\Assignment2Problem2.cpp(34) : error C2144: syntax error : 'int' should be preceded by ')'
.\Assignment2Problem2.cpp(34) : error C2660: 'isGeometric' : function does not take 0 arguments
.\Assignment2Problem2.cpp(34) : error C2059: syntax error : ')'

I'm probably way off, but bear with me. If you need me too, I'll post the whole code, but this is all I changed in the program.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Calling a function in main function

Posted 24 April 2010 - 12:32 PM

isGeometric (int &a, double &num1, ...) Would you say this is a function call?

If you say yes, I want you to look at your books, notes, Google, on examples of function calls. Just look properly.
Was This Post Helpful? 0
  • +
  • -

#11 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: Calling a function in main function

Posted 24 April 2010 - 01:11 PM

I looked in my book and other various sources, and changed my code to:
	cout << "The geometric sequence with the common ratio you entered, " << a
		<< ", starting at " << num1 << " is: \t" << isGeometric (a, num1)
        << endl;


Now the output displays a bunch of very negative numbers. I'm assuming this is now something to do with the function itself and that I need to change the scope to calculate the numbers properly? Thank you for being patient with me, I do appreciate it!
Was This Post Helpful? 0
  • +
  • -

#12 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Calling a function in main function

Posted 24 April 2010 - 01:11 PM

#include <iostream>
#include <cstdarg>

using namespace std;

int isGeometric (int a, double num1, ...);  // I don't believe a function call has any "..."'s
// int isGeometric(int, double); leaves it open to name the variables later.

void main()  // void should not be used with main... you always return 0 at the end, thus "int main()"
{
        int a;
        double num1;
        cout << "Geometric Sequence" << endl;
        cout << "Enter a number to be used as a common ratio." << endl;
        cin >> a;
        if (a <= 0)
        {
                cout << "Please enter a positive number." << endl;
                cin >> a;
        }
        else
                a = a;  // redundant, if it isn't less than zero, 'a' does not lose the value stored in it

        cout << "Enter a number to start calculating the geometric sequence." << endl;
        cin >> num1;
        if (num1 <= 0)
        {
                cout << "Please enter a positive number." << endl;
                cin >> a;  // You are checking num1, but if less than zero, you store its value in a
        }
        else
                num1 = num1;  // redundant
        
        cout << "The geometric sequence with the common ratio you entered, " << a
                << ", starting at " << num1 << " is: \t" << isGeometric  // missing your function paramters! look at the definition above
        << endl;
return 0;
}

int isGeometric (int a, double num1, ...)  // once again, those "..."'s
{
        va_list arguments;
    double num = num1, total = num;

    va_start(arguments, num1);
    do
    {
                total = a * num;
                cout << num << " ";
    } while( (num = va_arg(arguments, double) ) != 0);

    va_end(arguments);
    return total;
}



I would suggest opening your book and looking and funtions. Also, you have a lot of basic mistakes in the code which I commented on.
Was This Post Helpful? 0
  • +
  • -

#13 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: Calling a function in main function

Posted 24 April 2010 - 01:22 PM

I changed a few of the things that you commented on, thanks for the feedback! The "..." is because I need to have a function with a variable number of arguments. My code now stands as:
#include <iostream>
#include <cstdarg>

using namespace std;

int isGeometric (int &a, double &num1, ...);

int main()
{
	int a;
	double num1;
	cout << "Geometric Sequence" << endl;
	cout << "Enter a number to be used as a common ratio." << endl;
	cin >> a;
	if (a <= 0)
	{
		cout << "Please enter a positive number." << endl;
		cin >> a;
	}

	cout << "Enter a number to start calculating the geometric sequence." << endl;
	cin >> num1;
	if (num1 <= 0)
	{
		cout << "Please enter a positive number." << endl;
		cin >> num1;
	}
	
	cout << "The geometric sequence with the common ratio you entered, " << a
		<< ", starting at " << num1 << " is: \t" << isGeometric (a, num1)
        << endl;

	return 0;
}

int isGeometric (int &a, double &num1, ...)
{
	va_list arguments;
    double num = num1, total = num;

    va_start(arguments, num1);
    do
    {
		total = a * num;
		cout << num << ", ";
    } while( (num = va_arg(arguments, double) ) != 0);

    va_end(arguments);
    return total;
}

Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Calling a function in main function

Posted 24 April 2010 - 03:07 PM

@Hax, that is an ellipsis (...). Its functionality will let you enter any amount of function parameters

but i see this in a few places in your code
        cout << "Enter a number to start calculating the geometric sequence." << endl;
        cin >> num1;
        if (num1 <= 0)
        {
                cout << "Please enter a positive number." << endl;
                cin >> num1;
        }


what if the user just inputs the wrong number twice?

this is why you want a loop to check the conditions
	do{
	    cout << "Enter a number to start calculating the geometric sequence." << endl;
	    cin >> num1;
	}while(num1<=0); //keep looping while this condition is true




if you would like to display an error you could do your error checking with in the loop
	while(true) //loop until i hit a break
	{
		cout << "Enter a number to start calculating the geometric sequence." << endl;
		cin >> num1;
		if(num1<=0)
			cerr<<"PLEASE ENTER A NUMBER GREATER THAN ZERO..."<<endl;
		else
			break;//exit loop, condition is good
	}


Was This Post Helpful? 2
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Calling a function in main function

Posted 24 April 2010 - 03:21 PM

@Hax
Some more reading on the use of the ellipsis (...)
http://www.cplusplus...ibrary/cstdarg/
(I am not sure that the insistence on placing a comma before the ellipsis at the above link is correct - i believe it is optional - can anyone correct me?)

@xsunkisses44x
So how are things with your latest version of the code?
Are you getting any errors?
How does the output compare to what you want / expect?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2