uninitialized local variable question

This is a discussion on uninitialized local variable question within the C++ Programming forums, part of the General Programming Boards category; what am i missing here? Code: include <iostream> #include "empinfo.h" using namespace std; void getempdata(struct empinfo emp1[], int &count1, int ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    12

    uninitialized local variable question

    what am i missing here?
    Code:
    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;
    		count1++;
    		cout << "Input another - Y or N:";
    		cin >> ans;
    		cin.get(ret);
    	}
    	
    	return;
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,420
    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
    Location
    Singapore
    Posts
    21,883
    Alternatively, you can change to use a do while loop.
    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

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    12

    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, 01:26 PM

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