C++: Variable not initialized within "while" loop

This is a discussion on C++: Variable not initialized within "while" loop within the C++ Programming forums, part of the General Programming Boards category; Quick backstory: I am a college student taking C++ for an engineering major. I have never done any programming at ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    C++: Variable not initialized within "while" loop

    Quick backstory: I am a college student taking C++ for an engineering major. I have never done any programming at all so my problems will be a touch elementary. I apologize in advance for my ignorance and greatly appreciate all the help I can get.

    Now to my problem.

    I have to write a simple program to calculate the volume of cylinders while drawing the numbers needed from a file. When I build the program, it builds successfully. However, when I run my program, it tells me my variable "h" is not initialized.

    Code:
    int main()
    {
    	cout << "Zachary Jones" << endl;
    	cout << "Project 3" << endl;
    	cout << "Febuary 23, 2011" << endl;
    
    	const float PI = 3.141592;
    	float volume2, h, r, i, V, c;   // h = height, r = radius, i = increase %, v = volume, c = radius of new cylinder
    
    	char filename[100] = "cylinderdata.txt";
    	
    	// Create File
    	ifstream cylinderdata;
    	cylinderdata.open(filename);
    
    	
    
    	//Volume Program and loop
    	while (h > -999)
    	{
    		// Read in data
    	cylinderdata >> h >> r >> i ;
    
    		V = PI * pow(r,2) * h;
    		cout << "Volume of Cylinder is " << V <<endl <<endl;
    		volume2 = ((i/100) + 1) * V;
    		c = sqrt((volume2)/(PI*h));
    		cout << "The radius of the new cylinder is " << c << endl << endl;
    
    	}
    
    	return 0;
    }
    I want to think it has something to do with the source file for the numbers but as best I can tell, it's right where it needs to be.

    Any help or suggestions?

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,407
    float volume2, h,
    It's fine where you put it but it is not initialised no, initialise means 'assign a value to' (In general terms when talking about variables anyway)

    It is always a good idea to set a zero for example to any number variables. When they are created, sometimes it is obvious that the value will immediately be initialised anyway, like in a FOR loop, but it is a good habit to get into nontheless, Especially in cases like this, your while loop needs to be sure what ' h ' is before it starts executing, otherwise how can it work correctly? In your case 'h' will just have some wild value assigned that canot be predicted.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Basically, the first time through the loop, h doesn't have a value yet.

    Some things to consider:
    1) Any input error (such as the user accidentally inputting text) will cause this program to loop forever, wildly writing out data. You might want to learn about validating input.
    2) Instead of writing
    float h; // h = height
    why not write
    float height;
    ?
    You have lots and lots of one-letter variables and comments explaining what they mean. It would be much better if your variable names explained what they mean by themselves. Modern IDEs even save you from having to type the long names.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable assignment in a 'for' loop won't stick.
    By wyntrewolfe in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2010, 11:21 AM
  2. looping a variable within a loop.
    By vespine in forum C Programming
    Replies: 3
    Last Post: 06-27-2010, 10:30 PM
  3. Loop variable
    By anirban in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 10:36 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. for loop not letting me input variable data
    By RealityFusion in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 04:29 PM

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