The program asks the user what they want to do by selecting their choice from the MENU. even in case 1. the program asks the user to input physician name ,but as I do it only works for single character for example.if I input physician name <Sisay> the loop goes infinite but If I put only one letter <S> it works fine.How can I modify the program with out using struct.
thank you for your help !

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

//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;
}

Recommended Answers

All 5 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).

commented: good answer :) +25

I have to use character of array I have tried many times but still I do not fix the problem ...

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).

Make your arrays 2D, with the number of names (or doctor names, patient names) in the first dimension and the max number of chars in the name for the second dimension.
Step through them using the first dimension and write into them with a getline with the first parameter being the array name (names indicating the ith array of 80 chars) and the second being the length of the character array.
Here's an example:

char names[3][80];

	for (int i = 0;i<3;i++)
	{
		cout <<"Enter a name:"<<endl;
		cin.getline(names[i],80);
		
	}

As for the arrays with int values, I would honestly change them to char arrays because a) You're not doing any arithmetic with them and b) It will make life easier with using the getlines for everything.

I have put the code you have given to me in my program code but the problem is still there..I am using microsoft visual studio 6.00

Make your arrays 2D, with the number of names (or doctor names, patient names) in the first dimension and the max number of chars in the name for the second dimension.
Step through them using the first dimension and write into them with a getline with the first parameter being the array name (names indicating the ith array of 80 chars) and the second being the length of the character array.
Here's an example:

char names[3][80];

	for (int i = 0;i<3;i++)
	{
		cout <<"Enter a name:"<<endl;
		cin.getline(names[i],80);
		
	}

As for the arrays with int values, I would honestly change them to char arrays because a) You're not doing any arithmetic with them and b) It will make life easier with using the getlines for everything.

Repost the code please.

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.