We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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!

3
Contributors
7
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
Nevicar
Newbie Poster
10 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Achieved Level 70
Team Colleague
32,137 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,576
Skill Endorsements: 69

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
Skill Endorsements: 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
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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
Skill Endorsements: 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
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

Thanks a lot! That fixed it. :)

Nevicar
Newbie Poster
10 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by zeroliken and Ancient Dragon

Thanks a lot! That fixed it. :)

Good to hear... have fun programming :)

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0805 seconds using 2.76MB