-
warning c4700
learning functions and I am having trouble with some local variables. I keep getting "warning C4700: uninitialized local variable 'y' used", same for the variable x. Are my loops in combinations with cin >> messing up the code? I am pretty new to programming, any help would be greatly appreciated. Heres the code:
Code:
#include <iostream>
using namespace std;
//function prototype
double calculateRetail(double, double);
int main()
{
double x, y, z;
while(x < 0)
{
cout << "This progam calculates and displays an items retail price. Enter the whole sale price...\n";
cin >> x;
}
while(y < 0)
{
cout << "Enter the markup percentage...\n";
cin >> y;
}
z = calculateRetail(x, y);
cout << z << " is the retail price.\n";
return 0;
}
double calculateRetail(double x, double y)
{
return x * y * .01;
}
-
As the message says, you compare x < 0 and y < 0 before initialising x and y with some value. Perhaps a do while loop is what you want, but note that you are assuming that the user enters integral input.
-
Thank you. The Do-while loop worked perfectly, I also had my math wrong lol =).
Anyways, heres what the code is now.
Code:
#include <iostream>
using namespace std;
//function prototype
double calculateRetail(double, double);
int main()
{
double x, y, z;
do
{
cout << "This progam calculates and displays an items retail price. Enter the whole sale price...\n";
cin >> x;
}while(x < 0);
do
{
cout << "Enter the markup percentage...\n";
cin >> y;
}while(y < 0);
z = calculateRetail(x, y);
cout << z << " is the retail price.\n";
return 0;
}
double calculateRetail(double x, double y)
{
return x * (y + 100) * .01;
}
-
Incidentally, instead of naming your variables x, y and z, use descriptive names like whole_sale_price, markup_percentage, and retail_price.
-
Thank you for the tip, I will make sure to do that in the future =)