Thread: cin doesn't work after the first time

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    cin doesn't work after the first time

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    class publication
    {
    protected:
    	char title[20];
    	float price;
    public:
    	publication() : price(0.0)
    	{
    		title[0] = '\0';
    	}
    
    	virtual void getData()
    	{
    		cout<<"\nInput title: "; cin.get(title, 20);
    		cout<<"\nInput price: "; cin>> price;
    	}
    	
    	virtual void putData()
    	{
    		cout<<"\nTitle: "<< title;
    		cout<<"\nPrice: "<< price;
    	}
    };
    
    class book : public publication
    {
    private:
    	int pages;
    public:
    	book() : publication(), pages(0)
    	{	}
    
    	void getData()
    	{
    		publication::getData();
    		cout<<"\nInput pages: "; cin>> pages;
    	}
    
    	void putData()
    	{
    		publication::putData();
    		cout<<"\nPages: "<< pages;
    	}
    };
    
    class tape : public publication
    {
    private:
    	int minutes;
    public:
    	tape() : publication(), minutes(0)
    	{	}
    
    	void getData()
    	{
    		publication::getData();
    		cout<<"\nInput minutes: "; cin>> minutes;
    	}
    
    	void putData()
    	{
    		publication::putData();
    		cout<<"\nMinutes: "<< minutes;
    	}
    };
    
    int main()
    {
    	publication* ptr[10];
    	int pubs = 0;
    	int i;
    
    	do
    	{
    		if (pubs < 10)
    		{
    			cout<<"\nChoose between a book or tape (b/t): ";
    
    			if (_getche() == 'b')
    				ptr[pubs] = new book;
    			else
    				ptr[pubs] = new tape;
    			
    			ptr[pubs]->getData();
    			pubs++;
    
    			cout<<"\nInput another? (y/n): ";
    		}
    		else
    		{
    			cout<<"\nSorry, out of room.";
    			break;
    		}
    	}
    	while(_getch() == 'y');
    
    	for (i = 0; i < pubs; i++)
    	{
    		ptr[i]->putData();
    	}
    
    	cout<<"\n\n";
    	system("Pause");
    
    	for (i = 0; i < pubs; i++)
    	{
    		delete ptr[i];
    	}
    
    	return 0;
    }
    In the above program, the main loop loops through once successfully, but on subsequent times it's as if the cin calls in the different getData() functions don't exist. It cout's the correct text (Inupt title:, Input price, etc.) but it never allows the user to cin the data; it just moves on to the next cout.

    Any ideas why this is happening?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You have a trailing newline leftover from one of your input operations that is screwing things up. A call to cin.ignore in the right spot should clear that up.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    What exactly is a trailing newline, and how does it come about?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What exactly is a trailing newline, and how does it come about?
    Run this and then see if you understand:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int value;
        char str[20];
    
        cout << "Enter an int: ";
        cin >> value;
        cout << "Enter some text: ";
        cin.get(str,sizeof(str));
    
        cout << "You entered " << value << " for the int and " << str << " for the text." << endl;
    
        return 0;
    }
    Ponder the results for a bit and see if the light bulb comes on.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why use char? Use std::string and std::getline instead. Safe, no worries, and left over data (hopefully).
    Also, why use pointers? You don't need them.
    Furthermore, the class function names are misleading: get should be named set and put should be named get. And while we're at it, pass in the data to store via arguments and make separate get functions for retrieving them. Don't use std::cin inside the class; that hurts flexibility.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    I ran that code and I think I understand what a trailing newline is, but I still don't know where it's occurring in my program. I changed the main loop to this:

    Code:
    do
    	{
    		if (pubs < 10)
    		{
    			cout<<"\nChoose between a book or tape (b/t): ";
    
    			if (_getche() == 'b')
    				ptr[pubs] = new book;
    			else
    				ptr[pubs] = new tape;
    			
    			if (pubs > 0)
    				cin.ignore();
    			ptr[pubs]->getData();
    			pubs++;
    
    			cout<<"\nInput another? (y/n): ";
    		}
    		else
    		{
    			cout<<"\nSorry, out of room.";
    			break;
    		}
    	}
    	while(_getche() == 'y');
    which fixes the problem, but I still have no idea where the trailing newline is. The last two inputs before ptr[pubs]->getData() are calls to _getche(), which don't even require the user to press enter. Is the trailing newline still from either book::getData() or tape::getData() the last time through the loop? I would think if this was the case it would affect _getche(), but it doesn't. Unless input with cin is handled completely separate from _getche()??? Or maybe I'm just completely misunderstanding what a trailing newline is in the first place.

    Quote Originally Posted by Elysia View Post
    Why use char? Use std::string and std::getline instead. Safe, no worries, and left over data (hopefully).
    Also, why use pointers? You don't need them.
    Furthermore, the class function names are misleading: get should be named set and put should be named get. And while we're at it, pass in the data to store via arguments and make separate get functions for retrieving them. Don't use std::cin inside the class; that hurts flexibility.
    This is simply an example program testing out various concepts that I've learned recently. I'm sure there are more efficient ways to do things or better function names to use, but I'm not really concerned with this particular program being perfect.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Ok, first off getch is not standard C/C++ while cin is standard C++. Your mixing of the two is likely the cause of at least some of your difficulties here. You can go with one or the other method for your input. My recommendation is that you drop getch/conio.h and stick with the standard since the behavior is going to be well defined if you do that.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by LyTning94 View Post
    This is simply an example program testing out various concepts that I've learned recently. I'm sure there are more efficient ways to do things or better function names to use, but I'm not really concerned with this particular program being perfect.
    You have to ask yourself: is it worth using dangerous functions that you're most likely never going to use in the future?
    If you aim to be a C++ programmer, then you should use C++ constructs, and not C constructs. Using those will not help you to be a good C++ programmer.
    And as such, it is my recommendation that you use C++ constructsi n your code only and trying to get it work right.
    It will be easier and you will learn more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Example from book - time(NULL) doesn't work
    By FernandoBasso in forum C Programming
    Replies: 6
    Last Post: 10-30-2011, 05:59 PM
  2. first time compiling with external .c and .h file, doesn't work
    By Adam Rinkleff in forum C Programming
    Replies: 17
    Last Post: 06-21-2011, 05:38 PM
  3. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. Time Delay doesn't work!
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-17-2001, 01:12 PM