-
November 1st, 2017, 12:54 PM
#1
Please help me with Function in C ++
Code:
#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;
int test(int num);
int main()
{
int number;
cout << "please input a number"<< endl;
cin >> number;
test (number);
if (test(number) == 12)
{
cout << "test(number) returned 12" << endl;
//cout << test(number) << endl;
}
else
{
cout << "test(number) returned different number" << endl<<endl;
//cout << test(number) << endl;
}
return 0;
}
int test (int num)
{
cout << "the value is " << num << endl << endl;
int randomNumber = rand();
return randomNumber;
}
Output:
I cannot understand why the output just kept repeating the function twice while I only call the function once. Thanks.
Last edited by 2kaud; November 1st, 2017 at 02:27 PM.
Reason: Added code tags
-
November 1st, 2017, 02:30 PM
#2
Re: Please help me with Function in C ++
When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
I cannot understand why the output just kept repeating the function twice while I only call the function once.
You call the function twice
Code:
test (number);
if (test(number) == 12)
Both these statements call the function test(). I suspect you don't require the first.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 1st, 2017, 09:27 PM
#3
Re: Please help me with Function in C ++
Thank you very much for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|