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>
usingnamespace 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>
usingnamespace std;
string fTest();
int main()
{
string str = fTest();
cout << str << ", this is a test. " << "\n";
cin.get();
return 0;
}
string fTest()
{
return ("Hello");
}
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.
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:
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.
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.