need help with my source code but it wont complie and giving me errors - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: need help with my source code but it wont complie and giving me errors

  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    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.

  2. #17
    Join Date
    Nov 2013
    Posts
    49

    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

  3. #18
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    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.

  4. #19
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    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 02:28 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #20
    Join Date
    Nov 2013
    Posts
    49

    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

  6. #21
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: need help with my source code but it wont complie and giving me errors

    Quote Originally Posted by Krrish1 View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 2 of 2 FirstFirst 12

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center