Thread: Any ideas why this isn't working?

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Question Any ideas why this isn't working?

    Hi everyone,

    I'm trying out an exersize of a book I'm reading.
    Have a look at the code:

    [I]#include <iostream.h>
    #include <conio.h>
    class employee
    {
    private:
    char name[20];
    int n;
    int serial_number;
    public:
    void input()
    {
    char ch;
    n = 0;
    cout<<"Enter Name: ";
    do
    {
    ch = getche();
    name[n++] = ch;
    }while(ch != '\r');
    cout<<"Enter Serial Number: ";
    cin>>serial_number;
    }
    void display()
    {
    cout<<"Name: ";
    for (int i=0;i<n;++i)
    cout<<name[i];
    cout<<"\nSerial Number: "<<serial_number;
    }
    void purge()
    {
    serial_number = 0;
    for (int i=0;i<n;++i)
    name = ' ';
    }
    };
    void main()
    {
    employee e1;
    e1.input();
    e1.display();
    e1.purge();
    e1.display();
    }


    I'm using Visual C++ 6 Standard Edition on WIN XP.
    The above compiles with no errors & no warnings.

    However, when I run the program, nothing comes...
    Then, I press ENTER and this comes: "Enter name: Enter serial number: ".

    What dya think might be the problem?

    Thanks in advance
    Marc
    No matter how much you know, you NEVER know enough.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I think it's got to do with getche() and stuff being left in the input buffer.

    Could you please re-post your code using tags:

    Code:
    //your code
    It will be so much easier to read.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Just do this

    [X]
    type your code here
    [/X]

    Replace the 'X' with 'CODE'

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    To make this example work you could try -

    cout<<"Enter Name: "<<flush;

    However, unless the book is teaching you how to write non-standard code I'd throw it out before attempting any more excersises.

  5. #5
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Code:
    #include <iostream.h>
    #include <conio.h>
    class employee
    {
    private:
    	char name[20];
    	int n;
    	int serial_number;
    public:
    	void input()
    	{
    		char ch;
    		n = 0;
    		cout<<"Enter Name: ";
    		do
    		{
    			ch = getche();
    			name[n++] = ch;
    		}while(ch != '\r');
    		cout<<"Enter Serial Number: ";
    		cin>>serial_number;
    	}
    	void display()
    	{
    		cout<<"Name: ";
    		for (int i=0;i<n;++i)
    			cout<<name[i];
    		cout<<"\nSerial Number: "<<serial_number;
    	}
    	void purge()
    	{
    		serial_number = 0;
    		for (int i=0;i<n;++i)
    			name[i] = ' ';
    	}
    };
    void main()
    {
    	employee e1;
    	e1.input();
        e1.display();
        e1.purge();
        e1.display();
    }
    No matter how much you know, you NEVER know enough.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Have you tried this:

    Code:
    void input()
    {
    	char ch;
    	n = 0;
    	cout<<"Enter Name: ";
    	do
    	{
    		ch = getche();
    		name[n++] = ch;
    	}while(ch != '\r');
    	name[--n] = '\0'; 
                    cout<<"Enter Serial Number: ";
    	cin>>serial_number;
    }

  7. #7
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    The Dog: Yup, no luck!
    The following worked tho:
    Code:
    cout<<"Enter Name: "<<flush;
    What dya think?
    No matter how much you know, you NEVER know enough.

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    i think
    Originally posted by me.
    I think it's got to do with getche() and stuff being left in the input buffer.
    That's what i think!!

  9. #9
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Hmm, thanks .... this seems to work well:

    Code:
    #include <iostream.h>
    #include <conio.h>
    class employee
    {
    private:
    	char name[20];
    	int n;
    	int serial_number;
    public:
    	void input()
    	{
    		char ch;
    		n = 0;
    		cout<<"\nEnter Name: "<<flush;
    		do
    		{
    			ch = getche();
    			name[n++] = ch;
    		}while(ch != '\r');
    		cout<<"\nEnter Serial Number: ";
    		cin>>serial_number;
    	}
    	void display()
    	{
    		cout<<"\nName: "<<flush;
    		for (int i=0;i<n;++i)
    			cout<<name[i];
    		cout<<"\nSerial Number: "<<serial_number;
    	}
    	void purge()
    	{
    		serial_number = 0;
    		for (int i=0;i<n;++i)
    			name[i] = ' ';
    	}
    };
    void main()
    {
    	employee e1;
    	e1.input();
                    e1.display();
                    e1.purge();
                    e1.display();
    }
    No matter how much you know, you NEVER know enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project ideas.
    By Wraithan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-25-2009, 03:29 PM
  2. Need Game ideas.
    By RealityFusion in forum Game Programming
    Replies: 9
    Last Post: 08-17-2004, 03:42 PM
  3. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  4. job ideas
    By dP munky in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-19-2003, 02:42 AM
  5. working out...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-10-2003, 10:20 AM