problem with Calling a Function

Calling a Function HELP :)

Page 1 of 1

8 Replies - 1292 Views - Last Post: 09 March 2007 - 06:23 AM Rate Topic: -----

#1 Manny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 30-May 06

problem with Calling a Function

Post icon  Posted 06 March 2007 - 07:01 PM

Hi all:

I am having a problem calling a function from another function. What I want to occur is after the user enters their firstName and lastName. I want use the fullName thought the program

Thank you,


string Getname()
{


	string firstName, lastName, fullName;
	
	cout << "\tPlease enter your first name: ";
	cin >> firstName;
	
	//getline(cin, firstName);
	cout << endl;

	cout << "\tPlease enter your last name: ";
	cin >> lastName;
	//getline(cin, lastName);

	firstName[0] = toupper (firstName[0]);
	lastName[0] = toupper (lastName[0]);
	
	fullName = firstName + " " + lastName;
	
	cout << endl;
	
	return fullName;
}

void showMainMenu()
{

	int choice;
	
	string fullName;
	
	fullName = Getname();
	
	cout << endl << endl;

	cout << "\tWelcome " << fullName << " to CS-162 Main menu.\n";
	cout << "\tPlease Enter a menu number: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Movie menu		2) report\n";
	cout << "\t3) alibi		4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;

	cout << "\t...: ";
	cin >> choice;


	while (choice != 5)
	{

		switch (choice)
		{

			case 1 : showMovieMenu();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.";
				 break;
			case 4 : comfort();
				 break;
			default : cout << "\tThat's not a good choice.\n";

		}

		showMainMenu();

		cout << "\t...: ";

		cin >> choice;


	}

	if (choice == 5)
	{
		
		cout << endl << endl;	
		cout << "\tThank you.\n\n\n";
		
		exit(1);
	}

}


void showMovieMenu()
{
	int ans;

	cout << endl << endl;
	cout << "\tWelcome to CS-162 Movie menu.\n";
	cout << "\tPlease Enter a Movie menu item: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Add Movie			2) report\n";
	cout << "\t3) alibi			4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;


	cout << "\t...: ";
	cin >> ans;	


	while (ans != 5)
	{


		switch(ans)
		{

			case 1 : addMovie();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.\n";
				 break;
			case 4 : comfort();
				 break;
			default  : cout << "\tThat's not a choice.\n";

		}

		showMovieMenu();

		cout << "\t...: ";

		cin >> ans;

	}

	cout << endl << endl;	
	cout << "\tThank you for using CS-162 Movie menu!\n\n\n";


	if (ans == 5)
	{
		showMainMenu();

	}


}




Is This A Good Question/Topic? 0
  • +

Replies To: problem with Calling a Function

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: problem with Calling a Function

Posted 06 March 2007 - 07:28 PM

Can you specify the problem?
Was This Post Helpful? 0
  • +
  • -

#3 Manny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 30-May 06

Re: problem with Calling a Function

Posted 06 March 2007 - 07:42 PM

View PostManny, on 6 Mar, 2007 - 07:01 PM, said:

Hi all:

I am having a problem calling a function from another function. What I want to occur is after the user enters their firstName and lastName. I want use the fullName thought the program

Thank you,


string Getname()
{


	string firstName, lastName, fullName;
	
	cout << "\tPlease enter your first name: ";
	cin >> firstName;
	
	//getline(cin, firstName);
	cout << endl;

	cout << "\tPlease enter your last name: ";
	cin >> lastName;
	//getline(cin, lastName);

	firstName[0] = toupper (firstName[0]);
	lastName[0] = toupper (lastName[0]);
	
	fullName = firstName + " " + lastName;
	
	cout << endl;
	
	return fullName;
}

void showMainMenu()
{

	int choice;
	
	string fullName;
	
	fullName = Getname();
	
	cout << endl << endl;

	cout << "\tWelcome " << fullName << " to CS-162 Main menu.\n";
	cout << "\tPlease Enter a menu number: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Movie menu		2) report\n";
	cout << "\t3) alibi		4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;

	cout << "\t...: ";
	cin >> choice;


	while (choice != 5)
	{

		switch (choice)
		{

			case 1 : showMovieMenu();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.";
				 break;
			case 4 : comfort();
				 break;
			default : cout << "\tThat's not a good choice.\n";

		}

		showMainMenu();

		cout << "\t...: ";

		cin >> choice;


	}

	if (choice == 5)
	{
		
		cout << endl << endl;	
		cout << "\tThank you.\n\n\n";
		
		exit(1);
	}

}


void showMovieMenu()
{
	int ans;

	cout << endl << endl;
	[color=#FFFF33]cout << "\tWelcome to CS-162 Movie menu.\n"[/color]				
				[color=#33FFFF]cout << "\tWelcome " << fullName << " to CS-162 Movie		menu.\n";[/color]	   
	  
		   	cout << "\tPlease Enter a Movie menu item: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Add Movie			2) report\n";
	cout << "\t3) alibi			4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;


	cout << "\t...: ";
	cin >> ans;	


	while (ans != 5)
	{


		switch(ans)
		{

			case 1 : addMovie();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.\n";
				 break;
			case 4 : comfort();
				 break;
			default  : cout << "\tThat's not a choice.\n";

		}

		showMovieMenu();

		cout << "\t...: ";

		cin >> ans;

	}

	cout << endl << endl;	
	cout << "\tThank you for using CS-162 Movie menu!\n\n\n";


	if (ans == 5)
	{
		showMainMenu();

	}


}



Was This Post Helpful? 0
  • +
  • -

#4 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: problem with Calling a Function

Posted 06 March 2007 - 07:44 PM

It looks like your code is missing a few things - namely, some sort of #import somewhere, and your main() function. Are you leaving some code out, or are those the problems?
Was This Post Helpful? 0
  • +
  • -

#5 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: problem with Calling a Function

Posted 06 March 2007 - 08:24 PM

Yeah you definitely need #include <iostream>...and using namespace std;
but like girasquid said did you just leave those out or is what you posted your whole program?
Was This Post Helpful? 0
  • +
  • -

#6 Manny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 30-May 06

Re: problem with Calling a Function

Posted 06 March 2007 - 09:13 PM

View Postmattman059, on 6 Mar, 2007 - 08:24 PM, said:

Yeah you definitely need #include <iostream>...and using namespace std;
but like girasquid said did you just leave those out or is what you posted your whole program?


Ok here is the program


/*
	Program:	mainMenu.cpp
	Programmer:	Manuel Alminha
	Date:		02/17/07
*/


/*
	This program will display the main Menu system.

*/   



#include <iostream>
#include <string>
#include <cctype>


using namespace std;



	// Structures
/*
struct user_name
{

	string firstName;
	string lastName;
	string fullName;

};
*/

struct movies_m
{

	char ch;
	char title[256];
	int year;
};



	// function Prototypes

string Getname();
void showMainMenu();
void report();
void comfort();
void showMovieMenu();
void addMovie();



int main()
{

//	string fullName;

	cout << endl << endl;	
	cout << "\tWelcome to your IPod Move and Music tool.\n";
	cout << "\tPlease enter your menu choice from your,\n";
	cout << "\tMain Menu options listed.\n";
	cout << endl <<endl;

//	Getname(); // Function call to prompt the user to enter thier first and last name. 

		
	showMainMenu(); // Function call to show the main menu to the user.

	
	return 0;

	
}

string Getname()
{


	string firstName, lastName, fullName;
	
	cout << "\tPlease enter your first name: ";
	cin >> firstName;
	
	//getline(cin, firstName);
	cout << endl;

	cout << "\tPlease enter your last name: ";
	cin >> lastName;
	//getline(cin, lastName);

	firstName[0] = toupper (firstName[0]);
	lastName[0] = toupper (lastName[0]);
	
	fullName = firstName + " " + lastName;
	
	cout << endl;
	
	return fullName;
}

void showMainMenu()
{

	int choice;
	
	string fullName;
	
	fullName = Getname();
	
	cout << endl << endl;

	cout << "\tWelcome " << fullName << " to CS-162 Main menu.\n";
	cout << "\tPlease Enter a menu number: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Movie menu		2) report\n";
	cout << "\t3) alibi		4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;

	cout << "\t...: ";
	cin >> choice;


	while (choice != 5)
	{

		switch (choice)
		{

			case 1 : showMovieMenu();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.";
				 break;
			case 4 : comfort();
				 break;
			default : cout << "\tThat's not a good choice.\n";

		}

		showMainMenu();

		cout << "\t...: ";

		cin >> choice;


	}

	if (choice == 5)
	{
		
		cout << endl << endl;	
		cout << "\tThank you.\n\n\n";
		
		exit(1);
	}

}


void showMovieMenu()
{
	int ans;

	cout << endl << endl;
	cout << "\tWelcome to CS-162 Movie menu.\n";
	cout << "\tPlease Enter a Movie menu item: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Add Movie			2) report\n";
	cout << "\t3) alibi			4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;


	cout << "\t...: ";
	cin >> ans;	


	while (ans != 5)
	{


		switch(ans)
		{

			case 1 : addMovie();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.\n";
				 break;
			case 4 : comfort();
				 break;
			default  : cout << "\tThat's not a choice.\n";

		}

		showMovieMenu();

		cout << "\t...: ";

		cin >> ans;

	}

	cout << endl << endl;	
	cout << "\tThank you for using CS-162 Movie menu!\n\n\n";


	if (ans == 5)
	{
		showMainMenu();

	}


}

void addMovie()
{

	movies_m *yours = new movies_m;
	
	cout << endl << endl;
	cout << "\tEnter the name of the movie to be added to your\n";
	cout << "\tlist of movies: ";
	
	cin.ignore(256, '\n');
	cin.getline(yours -> title, 256);

	cout << endl << endl;
	cout << "\tWhat year was the move made: ";
	cin >> yours -> year;

	cout << endl << endl;
	cout << "\tThe name of your move is: " << " \" " << yours -> title  << "\" "<< "\n\t";
	cout << "And your move was made in: " << "(" << yours -> year << ")\n";	

	
	
	delete [] yours;
	
}

void report()
{

	cout << "\tIt's been an excellent week for business.\n";
	cout << "\tSales are up 120%. Expeness are down 35%. \n";

}

void comfort()
{

	cout << "\tYour employees think you are the best CEO\n";
	cout << "\tin thr industry. The board of directors think\n";
	cout << "\tyou are the finst CEO in the industry.\n";


}



Was This Post Helpful? 0
  • +
  • -

#7 koushaltk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-March 07

Re: problem with Calling a Function

Posted 09 March 2007 - 04:33 AM

Luks like u hvnt read any C++ books s of nw.. Go and purchase any c++ book.. this is a very small problm n u shudnt b wastin people's time on such a trivial problm.. But jus 2 add to the solution to ur problm, here's wat u can do..

1. Either make the first- & last-name as public, which is not a good practice; OR
2. write 2 public getter functions (GetFirstName & GetLastName) which return the required strings.
Was This Post Helpful? 0
  • +
  • -

#8 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: problem with Calling a Function

Posted 09 March 2007 - 05:40 AM


void showMainMenu()
{

	int choice;
	
	string fullName;
	
	fullName = Getname();

//you can have this inside while to remove recursion	
	cout << endl << endl;

	cout << "\tWelcome " << fullName << " to CS-162 Main menu.\n";
	cout << "\tPlease Enter a menu number: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Movie menu		2) report\n";
	cout << "\t3) alibi		4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;

	cout << "\t...: ";
	cin >> choice;


	while (choice != 5)
	{

		switch (choice)
		{
 // You do not need showMovieMenu() here as it is same to current function
			case 1 : addMovie();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.";
				 break;
			case 4 : comfort();
				 break;
			default : cout << "\tThat's not a good choice.\n";

		}
//recursion???
		showMainMenu();

		cout << "\t...: ";

		cin >> choice;


	}
//this is obvious you can not come out till choice == 5
	if (choice == 5)
	{
		
		cout << endl << endl;	
		cout << "\tThank you.\n\n\n";
		
		exit(1);
	}

}

//remove this as this does not serve any purpose [similar to above function.]
void showMovieMenu()
{
	int ans;

	cout << endl << endl;
	cout << "\tWelcome to CS-162 Movie menu.\n";
	cout << "\tPlease Enter a Movie menu item: 1, 2, 3, 4, or 5 to exit.\n";
	cout << endl;
	cout << "\t1) Add Movie			2) report\n";
	cout << "\t3) alibi			4) comfort\n";
	cout << "\t5) quit\n";
	cout << endl << endl;


	cout << "\t...: ";
	cin >> ans;	


	while (ans != 5)
	{


		switch(ans)
		{

			case 1 : addMovie();
				 break;
			case 2 : report();
				 break;
			case 3 : cout << "\tThe boss was in all day.\n";
				 break;
			case 4 : comfort();
				 break;
			default  : cout << "\tThat's not a choice.\n";

		}

		showMovieMenu();

		cout << "\t...: ";

		cin >> ans;

	}

	cout << endl << endl;	
	cout << "\tThank you for using CS-162 Movie menu!\n\n\n";

// not again...
	if (ans == 5)
	{
		showMainMenu();

	}


}

void addMovie()
{

	movies_m *yours = new movies_m;
	
	cout << endl << endl;
	cout << "\tEnter the name of the movie to be added to your\n";
	cout << "\tlist of movies: ";
	
	cin.ignore(256, '\n');
	cin.getline(yours -> title, 256);

	cout << endl << endl;
	cout << "\tWhat year was the move made: ";
	cin >> yours -> year;

	cout << endl << endl;
	cout << "\tThe name of your move is: " << " \" " << yours -> title  << "\" "<< "\n\t";
	cout << "And your move was made in: " << "(" << yours -> year << ")\n";	

	
	
	delete [] yours;
	
}

void report()
{

	cout << "\tIt's been an excellent week for business.\n";
	cout << "\tSales are up 120%. Expeness are down 35%. \n";

}

void comfort()
{

	cout << "\tYour employees think you are the best CEO\n";
	cout << "\tin thr industry. The board of directors think\n";
	cout << "\tyou are the finst CEO in the industry.\n";


}




Well, don't worry about what "koushaltk" has said [though he was angry for a valid reason]. But let us consider you are not following OOP concept and just used C++ syntax to write a C program...

There are few issues in your program which are not supposed to be there. Like unnecessary recursion or the if statement you have.

But let's focus on your main problem. You want to use the same name everywhere right? Then just send that name as a parameter to a function where you want to print it [or make FullName Global :D ].

example : addMovie(FullName);

and signature of function will be like addMovie(char *FName)
now you can use FName to print the full name. Got the point?

So wherever you want to send the name just send it like above.

now the other things :

Instead of calling same function recursively you can add those cout statements in while itself making it more simple. [It has something to do with stack memory].

And you come out of while only if choice==5 so you do not need to check whether it is 5 right?

now check your program which I commented above and make appropriate changes.

Hope this will solve your problem. else we will meet here again... :D
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: problem with Calling a Function

Posted 09 March 2007 - 06:23 AM

View Postkoushaltk, on 9 Mar, 2007 - 06:33 AM, said:

Luks like u hvnt read any C++ books s of nw.. Go and purchase any c++ book.. this is a very small problm n u shudnt b wastin people's time on such a trivial problm..

Hi koushaltk,

Dream In Code welcomes programming and design questions of all levels. The goal of the site is to help others understand, and to provide some guidance in their efforts. If you feel that a specific question may be 'trivial', then you do not need to post in the thread.

Thanks,

Amadeus
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1