Thread: uninitialized local variable question

  1. #1
    Registered User
    Join Date
    Nov 2006

    uninitialized local variable question

    what am i missing here?
    include <iostream>
    #include "empinfo.h"
    using namespace std;
    void getempdata(struct empinfo emp1[], int &count1, int MAX)
    char ans,ret;
    	count1 =0;
    	while (((ans=='Y')||(ans =='y'))&&(count1<MAX))
    		cout << "Input last name:";
    		cin.getline (emp1[count1].name,21);
    		cout << "Input first name:";
    		cin.getline (emp1[count1].name1,21);
    		cout << "input hours:";
    		cin >> emp1[count1].hours;
    		cout << "Input payrate:";
    		cin >> emp1[count1].payrate;
    		cout << "Input another - Y or N:";
    		cin >> ans;

  2. #2
    Registered User
    Join Date
    Nov 2006
    here is my error
    1>c:\documents and settings\pat\desktop\fifteena\getdata15.cpp\getdat a15.cpp\empinfo.cpp(13) : warning C4700: uninitialized local variable 'ans' used

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > while (((ans=='Y')||(ans =='y'))
    Here you test a value

    > cin >> ans;
    Sometime later, you give it a value

    > char ans,ret;
    Here you declared it, but gave it no initial value to compare against.

    It means that unless you do something to initialise it, the code will randomly fail to run at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Coming into your loop, the variable ans is uninitialised. Technically, the comparison '((ans == 'Y')||(ans == 'y')) therefore yields undefined behaviour first time through. In practice, that comparison will probably yield false (odds are the value coming in is neither 'Y' nor 'y'), so your function will have no effect. Some good quality compiler will probably complain (issue a warning) about ans being uninitialised, but not all compilers will.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Alternatively, you can change to use a do while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2006

    Talking thank you

    tahnks for the help, again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 9
    Last Post: 04-12-2009, 06:14 PM
  3. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM
  4. Variable Arguments Question
    By moonwalker in forum C Programming
    Replies: 8
    Last Post: 08-04-2002, 09:08 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM