Why cant I pass variables into a void printPoint function, and not have to return a float ave. I tried just printing the coordinates but I got too errors.

so my function looked like this

void printPoint(float x , float y, float z){	
	cout << " < " << x << " , " << y << " , " << z << " > " << endl;
}

any ideas on how you over come this problem?

#include <iostream>
#include <string>

using namespace std;

float printPoint(float x, float y, float z);

int main()
{
	
	int y = 0;
	float e,b,c, num , number = 0.0f;
	
	while(true)
	{
		for (int i = 88; i < 91; i++)
		{
			char a = i;
			cout << "Enter the " << a << " coordinate: ";
			cin >> number;
			++y;
			switch(y)
			{
			case 1:
					e = number;
					number = 0.0f;
				break;
			case 2:
					b = number;
					number = 0.0f;
				break;
			default:
					c = number;
					number = 0.0f;
				break;

			}
			
		}
			y = 0;
			cout << endl;
			num = printPoint(e, b, c);
			cout << " Avarage: " << num << endl;
			
				if(num == 1)
					break;
			
	}
}


float printPoint(float x , float y, float z){	
	cout << " < " << x << " , " << y << " , " << z << " > " << endl;
	float count = (x + y + z ) / 3;
	return  count;
}

Recommended Answers

All 13 Replies

Hi,
Is your function of type void or float?

Hi,
Is your function of type void or float?

You have mixed return type. The first code says it is void. In your code it is float

You have mixed return type. The first code says it is void. In your code it is float

Both the signature and implementation are float, so I was wondering what the first snippet is about. If the code looks like the second snippet there shouldn't be any problems.

And the code works. I guess he tried to implement elsewhere and defined it as void that is why the post includes void. But that is my guess :)

Just to make myself clear the code below doesn't work, the code above how ever does. I was just wondering why the code below doesn't, and could some please tell me what the errors I get are?

#include <iostream>
#include <string>

using namespace std;

void printPoint(float x, float y, float z);

int main()
{
	
	int y = 0;
	float e,b,c, num , number = 0.0f;
	
	while(true)
	{
		for (int i = 88; i < 91; i++)
		{
			char a = i;
			cout << "Enter the " << a << " coordinate: ";
			cin >> number;
			++y;
			switch(y)
			{
			case 1:
					e = number;
					number = 0.0f;
				break;
			case 2:
					b = number;
					number = 0.0f;
				break;
			default:
					c = number;
					number = 0.0f;
				break;

			}
			
		}
			y = 0;
			cout << endl;
			 printPoint(e, b, c);
			
			
	}
}


void printPoint(float x , float y, float z){	
	cout << " < " << x << " , " << y << " , " << z << " > " << endl;

}

Actually it works , it just showed me a Warning for num. But why didn't it work last time it gave me two errors (shame I'll probably never know) :(

But why didn't it work last time it gave me two errors (shame I'll probably never know) :(

It would have been better to have posted the actual error messages (as given by the compiler) along with the code that wasn't compiling. Otherwise people here can only guess what might be wrong.

Here it is again with Array

#include <iostream>
#include <string>

using namespace std;

void printPoint(float p[3]);

int main()
{
	
	float fArray[3] = {0.0f, 0.0f, 0.0f};
	float  number = 0.0f;
	int y = 0;
	while(true)
	{
		for (int i = 88; i < 91; i++)
		{
			
			char a = i;
			cout << "Enter the " << a << " coordinate: ";
			cin >> number;
			fArray[y] = number;
			++y;
		}
			
			y = 0;
			cout << endl;
			printPoint(fArray);
			
			
	}
}


void printPoint(float p[3]){	
	cout << " < " << p[0] << " , " << p[1] << " , " << p[2] << " > " << endl;

}

Does this work? If not then :
I don't think you need this :

void printPoint(float p[3]);

Try it like this :

void printPoint(float p[]);

>Try it like this
The size of the first dimension of an array parameter isn't required, but you can put it there and it'll be ignored. The two lines of code you posted are functionally identical.

Why can't i give a default to my array?

void printPoint(float p[] = {0.0f, 0.0f, 0.0f});

int main()
{
....
}

I can do this though

void hello(string word = "default");

>Why can't i give a default to my array?
Because your array isn't really an array, it's a pointer. Oh, and C++ doesn't support array literals. So I guess that's two pretty good reasons. ;)

okay thank you Narue, they are good reasons :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.