The program asks the user to enter physician name as array of character,but do not work properly ,only work for single character how can i input array of character for the physician name.
look case 1 on my code.

for( i=0;i<n;i++)
		{
			cout<<"\nEnter physician "<<i+1 <<" name :";
			cin>>phy_name[i];
			cout<<"Enter physician "<<i+1<<" id :";
			cin>>phy_id[i];
			cout<<"Enter physician "<<i+1  <<" specification :";
			cin>>phy_spec[i];
			cout<<"Enter physician "<<i+1 <<"work experiance :";
			cin>>phy_workex[i];
			cout<<endl;
			}

It only works for single character ,if I try to input array of character for example .
enter physician name >S.....it works but for
enter physician name>Sisay ..the loop goes infinite..
how can I solve the problem please correct it by adding some codes for me


************************************************

//program to write the information of patients registered 
// and physician available in the hospital.The program asks 
//the user to register physician,register patient,
//list all available physicians and lisit all registered patients
// depending on the interest of the user. 
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int i;
	char name[12],*nam=&name[0];
    int ch,n,phy_id[100],phy_workex[100];
	char phy_name[100],phy_spec[100];
	char *phyname=&phy_name[0];
	char pat_name[100],pat_gender[10],pat_case[50],ass_phy[12];
	char *patname=&pat_name[0];
	int m,pat_age[4],pat_cardno[20],pat_date[4];
		do
		{
			cout<<"\t\t\t TASK SELECTION WINDOW \n";
			cout<<"\t\t\t ********************* \n";
			cout<<"\t\tWhat would you like to do ?\n";
			cout<<"\t\t Please enter the number you choice \n";
			cout<<"\t\t 1.Register a physician/s \n";
			cout<<"\t\t 2.Register a patient/s \n";
			cout<<"\t\t 3.List all available physicians \n";
			cout<<"\t\t 4.List all registered patients \n";
			cout<<"\t\t 5.Quit a program \n";
			cout<<endl;
		    cout<<"Please enter your choice !\n";
            cin>>ch;
switch(ch)
{
case 1:
		cout<<"\t How many physician you want to register ?\n";
		cout<<"\t------------------------------------------\n";
		cin>>n;
		cout<<"Enter name,id,specification and work experiance of "<<n<<"physicians"<<endl;
		cout<<"--------------------------------------------------------------"<<endl;
		for( i=0;i<n;i++)
		{
			cout<<"\nEnter physician "<<i+1 <<" name :";
			cin>>phy_name[i];
			cout<<"Enter physician "<<i+1<<" id :";
			cin>>phy_id[i];
			cout<<"Enter physician "<<i+1  <<" specification :";
			cin>>phy_spec[i];
			cout<<"Enter physician "<<i+1 <<"work experiance :";
			cin>>phy_workex[i];
			cout<<endl;
			}
		break;

case 2:
		cout<<"\t How many patient you want to register ?\n";
		cout<<"\t -------------------------------------- \n";
		cin>>m;
		cout<<"Enter name,age,gender,card no,case,date and assigned physician of"<<m<<"patients"<<endl;
		cout<<"-------------------------------------------------------------------------"<<endl;
		for( i=0;i<m;i++)
		{
			cout<<"\n Enter patient "<<i+1<<" name :";
			cin>>pat_name[i];
			cout<<"\n Enter patient "<<i+1<<" age :";
			cin>>pat_age[i];
			cout<<"\nEnter patient  "<<i+1<<" gender :";
			cin>>pat_gender[i];
			cout<<"\n enter patient "<<i+1<<" card number :";
			cin>>pat_cardno[i];
			cout<<"\n Enter patient"<<i+1<<" case :";
			cin>>pat_case[i];
			cout<<"\n Enter patient "<<i+1<<" date :";
			cin>>pat_date[i];
			cout<<"\n Enter patient "<<i+1<<" assigned physician :";
			cin>>ass_phy[i];
			cout<<endl;
		}
		break;
case 3:
	cout<<"\t program to list all available physicians \n";
	cout<<"\t ---------------------------------------- \n";
	cout<<"Number "<<" name "<<"\t"<<"ID" <<"\t"<<"specification "<<"\t"<<"work exepriance \n";
	for(i=0;i<n;i++)
	{
		
		cout<< i+1<<"\t"<<phy_name[i]<<"\t"<<phy_id[i];
		cout<<"\t"<<phy_spec[i]<<"\t\t"<<phy_workex[i];
		cout<<endl;
	}
	break;
case 4:
	cout<<"\t\tprogram to list all registered patients \n";
	cout<<"\t\t****************************************\n";
	cout<<"number "<<"\t"<<"age"<<"\t"<<"gender"<<"\t"<<"card number ";
	cout<<"\t"<<"case"<<"\t"<<"date"<<"\t"<<"assigned physician";
	cout<<endl;
	for( i=0;i<m;i++)
	{
		
		cout<< i+1<<"\t"<<pat_name[i]<<"\t"<<pat_age[i]<<"\t"<<pat_gender[i];
		cout<<"\t"<<pat_cardno[i]<<"\t"<<pat_case[i]<<"\t"<<pat_date[i];
		cout<<"\t"<<ass_phy[i]<<endl;
	}
	break;
case 5:
	cout<<"\tCongratulation you finished your program !\n";
	cout<<"\t-----------------------------------------\n";
	return 0;
	break;
	}
}
while(true);
return 0;
}
jonsca commented: Learn to use code tags +0
Nick Evan commented: How many warnings do you need? -2

Recommended Answers

All 3 Replies

When going through your loop after entering in the physician's name

for( i=0;i<n;i++)
{
	cout<<"\nEnter physician "<<i+1 <<" name :";
	cin>>phy_name[i];

it's trying to write the entire name to phyname which is only one character. Consequently the extra characters remain in the stream, writing into the next set of variables and so on.

Using strings would be the best idea (std::string). However, sure I know it's probably your assignment to use char arrays, so look into a function like getline() (used as cin.getline(chararrayname,capacity); see this for a reference) but you'll need to cin.ignore() some characters that are left over in the stream after the getline (especially if you are still using cin >> for your ints and such).

I knew I had heard that somewhere before. You've tried twice shove this onto our plate but the thing is you need to be proactive in changing your program. I've told you that using cin >> to write 1 character (which is what phy_name is!) you will get 1 character. So if you type in a name where 1 character is supposed to go you're going to get a major overflow that affects the subsequent calls to cin.

If you have trouble with cin.getline() try the C function fgets (need to include <cstdio>)http://www.cplusplus.com/reference/clibrary/cstdio/fgets/, but I'm not honestly sure how well it behaves with cin >>

See http://augustcouncil.com/~tgibson/tutorial/iotips.html#problems for the issues I spoke of with cin.getline() and cin together.

how can I solve the problem

Start off by reading the many answers you've already gotten so many times. If you don't understand a specific piece of advice, then ask for more details about that specific case. (I assume using English is not the problem for you here (?)).

And please pay close attention to what jonsca said above:
you need to be proactive in changing your program
If you don't, sorry to say but, in my opinion, you might as well stop posting problems related to that program.

PS. Learn how to use code tags when you post code. It is actually quite easy.

The statement cin>>phy_name[i]; reads a single character -- phy_name. You need to read the entire string using cin>>phy_name; Adding the subscript references the i'th character only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.