-
November 12th, 2013, 02:28 PM
#16
Re: need help with my source code but it wont complie and giving me errors
You either use global variables or pass them as parameters.
-
November 12th, 2013, 02:32 PM
#17
Re: need help with my source code but it wont complie and giving me errors
can you show an example of it in regular function
-
November 12th, 2013, 03:18 PM
#18
Re: need help with my source code but it wont complie and giving me errors
I'm sure whatever resource you're using to learn has examples. Look up passing values to functions.
-
November 12th, 2013, 03:26 PM
#19
Re: need help with my source code but it wont complie and giving me errors
I'm not going to rewrite your code for you as this looks like an assignment and that would be considered cheating. Is this an assigment?
However, consider carefully the program below which shows some of the scoping rules, local and global variables and passing by value parameters to functions and returning a value from a function.
Code:
#include <iostream>
using namespace std;
int gvar1 = 10;
void func1(int fvar1)
{
int lvar1 = 30;
cout << "in func1 first fvar1 = " << fvar1 << " lvar1 = " << lvar1 << " gvar1 = " << gvar1 << endl;
lvar1 = 40;
cout << "in func1 second fvar1 = " << fvar1 << " lvar1 = " << lvar1 << " gvar1 = " << gvar1 << endl;
}
int func2(int fvar1)
{
int lvar1 = 50;
cout << "in func2 first fvar1 = " << fvar1 << " lvar1 = " << lvar1 << " gvar1 = " << gvar1 << endl;
gvar1 = 60;
cout << "in func2 second fvar1 = " << fvar1 << " lvar1 = " << lvar1 << " gvar1 = " << gvar1 << endl;
return lvar1;
}
int main()
{
int lvar1 = 20,
lvar2 = 0;
cout << "in main first gvar1 = " << gvar1 << " lvar1 = " << lvar1 << " lvar2 = " << lvar2 << endl;
func1(lvar1);
cout << "in main second gvar1 = " << gvar1 << " lvar1 = " << lvar1 << " lvar2 = " << lvar2 << endl;
lvar2 = func2(lvar1);
cout << "in main third gvar1 = " << gvar1 << " lvar1 = " << lvar1 << " lvar2 = " << lvar2 << endl;
return 0;
}
Before you even start to debug the logic errors in your program and look at the sort issues you need to be able to write a program that at least compiles without errors.
Have a look at my simple sample code, read carefully the comments that other gurus have provided and try and alter your program so that ir compiles.
Last edited by 2kaud; November 12th, 2013 at 03:28 PM.
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 12th, 2013, 03:55 PM
#20
Re: need help with my source code but it wont complie and giving me errors
this is a practice assignment and next week there is a assignment which is 50% similar
-
November 12th, 2013, 04:22 PM
#21
Re: need help with my source code but it wont complie and giving me errors
Originally Posted by Krrish1
this is a practice assignment and next week there is a assignment which is 50% similar
So you now know about local and global variables and passing and returning variables from functions - right? Sp you should now be able to get your program to at least compile.
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)
Tags for this Thread
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
|