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
  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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
    Location
    Florida
    Posts
    12,635

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. 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)

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
  •  





Click Here to Expand Forum to Full Width

Featured