-
March 27th, 2015, 10:52 PM
#1
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;
}
}
-
March 28th, 2015, 04:57 AM
#2
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
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 28th, 2015, 07:47 AM
#3
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.
-
March 28th, 2015, 08:39 AM
#4
Re: Code Error C4700 - Please Help
-
March 28th, 2015, 12:12 PM
#5
Re: Code Error C4700 - Please Help
Originally Posted by puck2020
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|