dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Code Error C4700 - Please Help

  1. #1
    Join Date
    Mar 2015
    Posts
    5

    Code Error C4700 - Please Help

    Please help. I've been staring at and trying to fix this for hours. In Visual Studios I keep getting this error. cpp(36): error C4700: uninitialized local variable 'Eligible' used

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    
    
    using namespace std;
    
    void Getinput(int& Loantype, double& Income, double& Totaldebt, double& Loanamount);
    void Autoloan(double Income, double Totaldebt, double Loanamount, bool& Eligible);
    void Mortage(double Income, double Totaldebt, double Loanamount, bool& Eligible);
    void Printreport(int& Loantype, double& Loanamount, bool& Eligible);
    
    
    
    
    void main()
    {
    	int Loantype = 0;
    	double Income, Totaldebt, Loanamount;
    	bool Eligible;
    	string Loantype2;
    	
    	
    	while (Loantype != 3)
    	{
    
    	Getinput(Loantype, Income, Totaldebt, Loanamount);
    		if (Loantype == 1)
    		{
    			Autoloan(Income, Totaldebt, Loanamount, Eligible);
    			Printreport(Loantype, Loanamount, Eligible);
    		}
    
    		else if (Loantype == 2)
    		{
    			Mortage(Income, Totaldebt, Loanamount, Eligible);
    			Printreport(Loantype, Loanamount, Eligible);
    		}
    
    		if (Loantype == 1)
    			Loantype2 = "Auto";
    		else if (Loantype == 2)
    			Loantype2 = "Mortage";
    	}
    	         
    
    
    }
    
    	void Getinput(int& Loantype, double& Income, double& Totaldebt, double& Loanamount)
        {
    		cout << "SELECT ONE OF THE FOLLOWING OPTIONS:" << endl << endl;
    		cout << " 1    AUTO LOAN" << endl;
    		cout << " 2    HOME MORTAGE LOAN" << endl;
    		cout << " 3    STOP PROGRAM" << endl;
    	
    		cout << "ENTER A NUMBER:";
    		cin >> Loantype;
    		if (Loantype == 1 || Loantype == 2)
    		{
    			cout << "ENTER INCOME, TOTAL DEBT, AND LOAN AMOUNT." << endl;
    			cin >> Income >> Totaldebt >> Loanamount;
    		}
    
         }
    
    	void Autoloan(double Income, double Totaldebt, double Loanamount, bool& Eligible)
    
    	{
    		if (Income - Totaldebt >= .5 * Loanamount)
    			Eligible = true;
    		else
    			Eligible = false;
    	}
    
    	void Mortage(double Income, double Totaldebt, double Loanamount, bool& Eligible)
    
    	{
    		if (Income - Totaldebt >= .3 * Loanamount)
    			Eligible = true;
    		else
    			Eligible = false;
    	}
    
    	void Printreport(int& Loantype, double& Loanamount, bool& Eligible)
        {	
    		if (Eligible == true)
    		{
    			cout << "Loan Type" << Loantype << endl;
    
    			cout << "Loan Amount" << Loanamount << endl;
    			cout << "Eligible: YES" << endl;
    		}
    		if (Eligible == false)
    		{
    			cout << "Loan Type" << Loantype << endl;
    			cout << "Loan Amount" << Loanamount << endl;
    			cout << "Eligible: NO" << endl;
    		}
    	 }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,910

    Re: Code Error C4700 - Please Help

    Using MS VS 2013 this compiles OK with no warnings/errors level 4. What version of VS are you using?

    The issue might be that Eligible is defined but not initialised at the start of main() and is then passed by reference to autoloan() and mortage(). Try changing
    Code:
    bool Eligible;
    to
    Code:
    bool Eligible = false;
    Note that in main() you don't need the two sets of tests for Loantype. Loantype2 can be set within the previous Loantype conditions.

    Also within Getinput() you are not checking for invalid input (eg entering a 'q' for an option which would cause the input stream to fail.

    Printreport() also doesn't need its parameters passed by ref when they are not changed with the function and are POD types. They could be passed by value as in other functions.
    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++17 Compiler: Microsoft VS2019 (16.3.9)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,554

    Re: Code Error C4700 - Please Help

    You have some stylistic issues.

    Autoloan and Mortgage would be better returning a bool than setting one passed in as an output parameter.

    Eligible passed into PrintReport should be a const bool, not a bool& if you're not changing its value in the function.

  4. #4
    Join Date
    Mar 2015
    Posts
    5

    Re: Code Error C4700 - Please Help

    I'm running VS 2013.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,554

    Re: Code Error C4700 - Please Help

    Quote Originally Posted by puck2020 View Post
    I'm running VS 2013.
    The compiler's right. You're using it without initializing it. Initialize it, ignore it, tell the compiler to ignore it or better yet, have your function return a bool as I suggested.

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




On-Demand Webinars (sponsored)