Problem creating simple function

Hello! I have a problem with this simple program, and I don't know what's wrong =\

#include "stdafx.h"
#include <iostream>
using namespace std;

string fTest();

int _tmain(int argc, _TCHAR* argv[])
{
cout << fTest << ", this is a test. " << "\n";
cin.clear();
cin.get();
return 0;
}

string fTest(void)
{
return ("Hello");
}


I should be getting "Hello, this is a test." as an output, but I am getting
00411168, this is a test.

Why is this?? I have created functions that return an integer before,
but never a string. Please, is there a simple way to do this, without using
char arrays and pointers? thanks.
I am using VisualC++ 2008, the line
int _tmain(int argc, _TCHAR* argv[])
was automatically created.

I don't know why you're getting a number (and im suprised you're not getting an error) but the way you're using that function is wrong. Here are two methods you can use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
using namespace std;

void fTest();

int _tmain(int argc, _TCHAR* argv[])
{
   cout << fTest() << ", this is a test. " << "\n";
   cin.get();
   return 0;
}

void fTest(void)
{
   cout << "Hello";
}


OR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

string fTest();

int main()
{
   string str = fTest();
   cout << str << ", this is a test. " << "\n";
   cin.get();
   return 0;
}

string fTest()
{
   return ("Hello");
}
Maltadav wrote:
I should be getting "Hello, this is a test." as an output, but I am getting
00411168, this is a test.

You forgot the parentheses,

cout << fTest << ", this is a test. " << "\n";

should be

cout << fTest() << ", this is a test. " << "\n";.

The number you're getting is the address of the function fTest. In c++ the name of a function is a way to get its address.
Last edited on
Thank you for your quick replies, it worked :)
Quite a stupid mistake, couldn't figure out what was wrong. I used to program in vb before, and it used to add the parentheses automatically, but c++ is a different story.
Last edited on
I used to program in vb before, and it used to add the parentheses automatically, but c++ is a different story.

I imagine that's down to the IDE you're using...
I imagine that's down to the IDE you're using...

No, it's not. In BASIC you can omit the parentheses if you want. I mean it's a compiler issue, not an IDE one.
@Albatross:

Don't think that I didn't see your post...
Don't think that I didn't see your post...

I didn't, what did it say? :p

He insisted that the parentheses thing is an IDE issue. I guess he then googled around a bit
(+basic +functions +omit +parentheses) and found something like this:

http://visualbasic.about.com/od/learnvbnet/a/byvalbyref.htm

Then he scrolled down a bit and found something like this:

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

And then he deleted his post. :P

But here's something that I didn't know...

In VB6 and earlier versions, using parentheses to enclose the arguments you're passing to a procedure was optional under certain circumstances (as when you called Sub procedures). In VB .NET, that's no longer true; you must always use parentheses now, unless you're not passing any arguments to the procedure, in which case you can either use empty parentheses or omit them altogether.

Found here:

http://www.yaldex.com/vb-net-tutorial-2/library.books24x7.com/book/id_5526/viewer.asp@bookid=5526&chunkid=0140268750.htm
Last edited on
I deleted the post just to be certain. However, I'm certain that adding parenthesis into the code (as opposed to requiring/not requiring them) is an IDE thing, and I'm pretty sure that's what Maltadav was talking about when he said "it".

If he was talking about requiring them, then that's a compiler issue.

-Albatross
Last edited on
Ok then.
Topic archived. No new replies allowed.