954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Overloading Functions

Hello everyone, I'm trying to figure out how to overload a function to solve the following problem:

"Write two functions with the same name (overloaded function) that print out a phrase. The first function should take a string as an argument, and print out the string once. The second function should take a string and an integer as arguments, and print out the string as many times as the integer."

I can't figure out how to get this to run though, here's the code that I have so far:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

void print (string word)
{
	print (word);
}

void print (string word, int number)
{
	print (word, number);
}


int main(){

	string word;
	int number(0);
	char c;
		
	cout << "Please enter your word: " << endl;
	cin >> word;
	print(word);
	cout << "\n";

	cout << "Please enter a word and a number: " << endl;
	cin >> word, number;
	print (word, number);
	cout << "\n";

	return 0;
}


Any and all help would be cool!

Nevicar
Newbie Poster
10 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

The first function is incorrect

void print (string word)
{
	cout << word << '\n'; // display the string
}

Now do similar for second function, but this time put cout statement inside a loop.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks! Did that and now its working but when I get to the last one with the word and integer it just spits out a 0 when I type them in and hit enter. Nothing in the code has changed except the cout statements at the top.

Nevicar
Newbie Poster
10 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
The second function should take a string and an integer as arguments, and print out the string as many times as the integer.


what exactly did you put in that function? Did you use a loop?

void print (string word, int number)
{
        /*create loop that will print the following string as many times as the integer passed*/
	cout << word << endl; // display the string
}
zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

I did, but its not running the right way.
Here's the updated code.

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

void print (string word)
{
	cout << word << "\n";
}

void print (string word, int number)
{
	for (int i = 0; i < number; i++){
	cout << (word);
	}
}


int main(){

	string word;
	int number(0);
	char c;
		
	cout << "Please enter your word: " << endl;
	cin >> word;
	print(word);
	cout << "\n";

	cout << "Please enter a word and a number: " << endl;
	cin >> word, number;
	print(word);
	cout << "\n";

	return 0;
}
Nevicar
Newbie Poster
10 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

at line 32
try it like this

cin >> word; //receive input for word
cin >> number; //receive input for number

or simply

cin >> word >> number;


then at line 33 call the function print (word, number);

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Thanks a lot! That fixed it. :)

Nevicar
Newbie Poster
10 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
Thanks a lot! That fixed it. :)


Good to hear... have fun programming :)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You