Thread: Undeclared Identifer

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    23

    Undeclared Identifer

    Hi guys,

    I keep trying to compile the code of a small program that I have been creating just as a learning exerciser again but keep coming up against an error - "error C2065: 'gets' : undeclared identifier" but I can't see where I'm going wrong in the code for that to happen - anyone any ideas ? My code is:

    Code:
    void add_video(videorecord_type videos[])
    {
    	int no_of_videos, next_record_no, year;
    	float price;
    	char tempstring[STOCK_ID_SIZE+1];
    
    	//Display heading
    	clear_screen();
    	cout << "						ADD A VIDEO TO STOCK"
    		 << endl << endl << endl;
    
    	//Count how many videos are already in the database
    	no_of_videos = video_count(videos);
    
    	//Report to user
    	cout << endl << "There are " << no_of_videos << " videos in VIDEO LIBRARY. " << endl;
    
    	//Set the record number at which to add the new video
    	next_record_no = no_of_videos;
    
    	//If database is not full, add a video
    	if (no_of_videos < MAX_VIDEOS)
    	{
    		//Get the stock ID as a temp string
    		cout << endl << "Stock ID: " << flush;
    		cin  >> tempstring;
    
    		//Check this stock_ID doesn't already exist
    		if (record_no(videos,tempstring) == -1)
    		{
    			strcpy(videos[next_record_no].stock_ID, tempstring);
    			cout << "Video Title: " << flush;
    			gets(videos[next_record_no].title);
    
    			do
    			{
    				cout << "Year of release: ";
    				cin >> year;
    				if (year < 1950 || year > 2050)
    				{
    					cout << "Year muct be in range 1950 to 2050\a" << endl;
    				}
    			}while (year < 1950 || year > 2050);
    			videos[next_record_no].year = year;
    
    			do
    			{
    				cout << "Price: ";
    				cin >> price;
    				if (price < 1.0 || price > 20.0)
    				{
    					cout << "Price must be in the range £1 to £20\a" << endl;
    				}
    			}while (price < 1.0 || price > 20.0);
    			videos[next_record_no].price = price;
    		}
    		else
    		{
    			cout << "This stock ID already exists\a" << endl;
    		}
    	}
    	//If database full, give message
    	else
    	{
    		cout << "No room for any more videos\a" << endl;
    	}
    
    	//Database has changed so save it
    	save_data(videos);
    }
    Thanks guys for any input

    H_M

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Dont use gets. The answer is probably that you didnt include the appropriate header, but you might as well use std::getline() or std::cin.getline(), as the case may be.
    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

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >"error C2065: 'gets' : undeclared identifier"
    We wish. Did you include stdio.h? Do you know that gets should never be used? Do you know that this is the C++ forum and cin.getline would be a better choice?
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Thanks guys. Yeah, I had forgotten to include the 'stdio.h' header ! Always the simplest things.

    Sorry for using 'gets'. I've not been learning C++ long and the book I was/am using has 'gets' and not 'cin.getline'. Does this work in just the same way with the same header ?? Also, is there any on-line references that you would recommend to look at that ??

    Thanks again

    H_M

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Like laserlight's sig suggests, you should just look up a reference.
    C++ I/O
    cin.getline

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >book I was/am using has 'gets' and not 'cin.getline'
    It sounds like you need to burn that book and get a good one. Any C++ book that uses gets is begging to be torched.
    My best code is written with the delete key.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Prelude
    It sounds like you need to burn that book
    Back then, people who used to read these type of books used to burn on the stake too. After prolonged, exquisite and agonizing torture.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Cheers guys for the info on 'getline' - I'm trying to look into it but it's quite hard for me ! How would i go about using that in the code I pasted in my earlier post instead of 'gets' ??

    If I get used to using it now, from now on I can use that instead.

    On a seperate note, do any of you recommend any good books on learning C++ for a beginner upwards ??

    Thanks again

    H_M

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Make sure you include cstring.
    Something like
    cin.getline( videos[next_record_no].title, strlen(videos[next_record_no].title) );
    depending on what title is. If it's a c-string strlen will work, if it's a std::string, use the length method.

    There's a sticky for reccommended books at the top of this forum. Give that a read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM