2 Replies - 279 Views - Last Post: 26 April 2012 - 03:24 AM Rate Topic: -----

#1 ackuang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-April 12

How to convert this code by using function?

Posted 25 April 2012 - 09:39 AM

#include <iostream>
using namespace std;

const int SIZE=30;
const int SIZE2=1;

struct Speaker
{
	char name[SIZE];
	int phoneNo;
	char topic[SIZE];
	double fee;
};

int main()
{
	Speaker s[SIZE2];
	int choice,spNum,choice2;
	char searchTopic[SIZE];
	char *strPtr;
	int i;

	do
	{
		cout<<"Speakers' Bureau"<<endl;
		cout<<"***********************"<<endl;
		cout<<"1. New speaker registration"<<endl;
		cout<<"2. Change data"<<endl;
		cout<<"3. Display data"<<endl;
		cout<<"4. Search data"<<endl;
		cout<<"5. Quit"<<endl<<endl;
		cout<<"Enter your choice: ";
		cin>>choice;
		cout<<endl;
		switch(choice)
		{
			case 1:	for(i=0;i<SIZE2;i++)
					{
						cout<<"Speaker #"<<i+1<<endl;
						cout<<"Enter name: ";
						cin.ignore();
						cin.getline(s[i].name,SIZE);
						cout<<"Enter telephone number: ";
						cin>>s[i].phoneNo;
						cout<<"Enter speaking topic: ";
						cin.ignore();
						cin.getline(s[i].topic,SIZE);
						cout<<"Enter fee: ";
						cin>>s[i].fee;
						while((s[i].fee)<0)
						{
							cout<<"Please do not enter negative values!!"<<endl;
							cout<<"Enter fee: ";
							cin>>s[i].fee;
						}
						cout<<endl;
					}
					break;
			case 2: cout<<"Enter the account number: ";
					cin>>spNum;
					cout<<"Which of the following data you want to change?"<<endl;
					cout<<"1. Name"<<endl;
					cout<<"2. Telephone Number"<<endl;
					cout<<"3. Speaking Topic"<<endl;
					cout<<"4. Fee"<<endl;
					cout<<"Enter your choice: ";
					cin>>choice2;
					if(choice2==1)
					{
						cout<<"Enter new name: ";
						cin.ignore();
						cin.getline(s[spNum-1].name,SIZE);
					}
					else if(choice2==2)
					{
						cout<<"Enter new telephone number: ";
						cin.ignore();
						cin>>s[spNum-1].phoneNo;
					}
					else if(choice2==3)
					{
						cout<<"Enter new topic: ";
						cin.ignore();
						cin.getline(s[spNum-1].topic,SIZE);
					}
					else if(choice2==4)
					{
						cout<<"Enter new fee: ";
						cin.ignore();
						cin>>s[spNum-1].fee;
						while(s[spNum-1].fee<0)
						{
							cout<<"Please do not enter negative values!!"<<endl;
							cout<<"Enter fee: ";
							cin.ignore();
							cin>>s[spNum-1].fee;
						}
					}
					break;
			case 3:	for(i=0;i<SIZE2;i++)
					{
						cout<<"Speaker #"<<i+1<<endl;
						cout<<"Name : "<<s[i].name<<endl;
						cout<<"Telephone : "<<s[i].phoneNo<<endl;
						cout<<"Speaking Topic : "<<s[i].topic<<endl;
						cout<<"Fee : "<<s[i].fee<<endl<<endl;
					}
					break;
			case 4: cout<<"Enter the speaker's topic you want to search: ";
					cin.ignore();
					cin.getline(searchTopic,SIZE);
					for(i=0;i<SIZE2;i++)
					{
						strPtr=strstr(s[i].topic,searchTopic);
						if(strPtr!=NULL)
							break;
					}
					if(strPtr!=NULL)
						cout<<s[i].topic<<endl<<endl;
					else
						cout<<"No topic was found"<<endl<<endl;
					break;
			default:cout<<"You are exiting the system"<<endl;
		}
	}while(choice!=5);

	system("pause");
	return 0;
}



just for case 4 in switch, I would like to use function instead of write it into main. The function should accept part of the customer's name as an argument. Can someone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert this code by using function?

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3045
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: How to convert this code by using function?

Posted 25 April 2012 - 09:46 AM

What do you need help with? Do you know how to write a function? Do you know how to pass variables into and back from functions? If not I suggest you read the tutorials contained in my signature. They should help. Otherwise ask specific questions.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 ackuang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-April 12

Re: How to convert this code by using function?

Posted 26 April 2012 - 03:24 AM

View Postjimblumberg, on 25 April 2012 - 09:46 AM, said:

What do you need help with? Do you know how to write a function? Do you know how to pass variables into and back from functions? If not I suggest you read the tutorials contained in my signature. They should help. Otherwise ask specific questions.

Jim


i know how to use function a bit, but don't know how to pass structure array into a function and call it into main.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1