Sign in to follow this  
ukdeveloper

Help - this code won't work!

Recommended Posts

Hi, I'm relatively new to C++, and I've run into difficulties with what should be very easy code. Here it is:
#include <iostream.h>

void f_1();
void f_2();
void solution(int x, int y, int z);

int main(void)
{
	void f_1();
	void f_2();
	void solution();
	return 0;
}

void f_1()
{
	int x;
	cout<<"Enter a number:\n\n";
	cin>>x;
}

void f_2()
{
	int y;
	cout<<"Enter another number:\n\n";
	cin>>y;
}

void solution(int x,int y)
{
	int z;
	z=x*y;
	cout<<"x * y = ";
}

I can get the same program to work if I declare x,y,z globally and do everything within int main(). However, this code compiles and runs with 0 errors, 0 warnings (I'm using VC++ 6 Standard), but just exits. Doesn't even show any of the text in the cout<< bits, or ask me for my input. I can pass parameters in Visual Basic no problem, but this stumps me as to why it won't even work. Hope somebody can help me!

Share this post


Link to post
Share on other sites
I could list and explin all the mistakes you made, but IMO a book on C++ is better suited for that. Please read about variable scope, function declaration and definition as well as <>calling functions and passing parameters.

Regards,
Pat.

Share this post


Link to post
Share on other sites
The big problem is that inside main() when you do:
void f_1();
void f_2();
void solution();


Those aren't calling the functions, those are declaring function prototypes. If you get rid of the void in front of f_1() and f_2() in main, it should then call the functions. Fixing the call to solution() is trickier, since you declared solution() to take arguments, but you don't have any arguments to pass to it.

One way to fix that, is to change f_1() and f_2() to functions that return an int value. And then declare local variables in main() to store the values f_1() and f_2() return. Then you can use the variables when calling solution().

It would go something like this:


#include <iostream>

int f_1();
int f_2();
void solution(int x, int y);

int main(void)
{
int x = f_1();
int y = f_2();
solution(x, y);
return 0;
}

int f_1()
{
int x;
std::cout << "Enter a number:\n\n";
std::cin >> x;
return x;
}

int f_2()
{
int y;
std::cout << "Enter another number:\n\n";
std::cin >> y;
return y;
}

void solution(int x, int y)
{
int z = x * y;
std::cout << "x * y = " << z << std::endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
What mistakes have I made?

For starters, the definition of the solution method (void solution(int x, int y, int z)) is different than its declaration (void solution(int x, int y)).

Also, as SiCrane noted above, you're not calling the functions in main, just redefining them. I'm at a loss to explain why your compiler isn't complaining about this.

Share this post


Link to post
Share on other sites
Quote:
Original post by kSquared
Also, as SiCrane noted above, you're not calling the functions in main, just redefining them. I'm at a loss to explain why your compiler isn't complaining about this.

Those are local function declarations, not definitions. Local function declarations are valid in C++. Local function definitions are not.

Share this post


Link to post
Share on other sites
Well, you sort of can locally define a function, but you have to do it inside a struct:
int main() {
struct foo {
static int bar() { return 3; }
};
foo.bar();
}

Share this post


Link to post
Share on other sites
I believe the big reason for allowing local function declarations is for compatibility with C code. If C++ disallowed local function declarations, a lot of legacy C code would break.

It's also sometimes useful when you're in crunch mode and you need something done yesterday. Let's say you add in a quick hack that uses a function from a header that the source file doesn't currently include (for some reason it's usually Windows API calls for me), and the good solution doesn't involve using that source file. Rather than including the whole source file, put the local function declaration for the one function you need to use, and then all the horrible hacks will all be in one spot for when you go through and pull it out later. Not a practice I recommend for every day use, but it comes in handy once in a while.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this