Help - this code won't work!

Started by
8 comments, last by SiCrane 19 years, 4 months ago
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!
Advertisement
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.
What mistakes have I made?
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;}
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
It's working great now. Thanks for helping me, I'll do a bit more reading next time!
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.
Okay then riddle me this - what's the point in locally declaring a function if you can't locally define it? :-)
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
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();}
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.

This topic is closed to new replies.

Advertisement