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.
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
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.
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.
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
Re: need help with my source code but it wont complie and giving me errors
Quote:
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?:thumb: Sp you should now be able to get your program to at least compile.