warning c4700

This is a discussion on warning c4700 within the C++ Programming forums, part of the General Programming Boards category; learning functions and I am having trouble with some local variables. I keep getting "warning C4700: uninitialized local variable 'y' ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    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;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    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;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Incidentally, instead of naming your variables x, y and z, use descriptive names like whole_sale_price, markup_percentage, and retail_price.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Thank you for the tip, I will make sure to do that in the future =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21