Give me a Number

In class we need to rewrite code into OOP and the book assigned goes against it but i'm trying to figure out how to properly get the Give me a Number program to ask and give a number. After which I plan on adding a function overloading. But what i'm not getting is that you really can't add in:
 
  int askNumber(int high, int low)

after
 
int askNumber(int high, int low = 1);


without getting a error because its being used more than once in the same class.
This is what I have so far up to the error, at the moment i'm still reading through the book and researching on possible solutions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Get my number HW.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string"
#include "iostream"

using namespace std;

class myNumber{


	int askNumber(int high, int low = 1);

	int numberOut(){
		
		int number = askNumber(5);
		cout<< "Thanks for entering: " << "\n\n" << endl;

		number = askNumber(10, 5);
		cout << "Thanks for entering: " << "\n\n" << endl;		
	}

// error of being stated twice
           int askNumber(int high, int low){
		int num;
		do {
			cout << "Please enter a number " << " ( " << low << " - " << high << " ): ";
			cin >> num;

		}while (num > high || num < low); 
		return num;
	}

};

int _tmain(int argc, _TCHAR* argv[])
{

	myNumber get;

	return 0;
}
error of being stated twice

That's because both line 13 and line 25 takes two ints.
The function signatures for both are the same.

The fact that line 13 has a default value on the second argument does not affect the function signature.

You don't have to declare it first(in line 13) or if you do you must delete the ints in line 25.
Just delete line 13 and add = 1 to line 25.

If you were to do this in header/source style it would look like this:

Header:
1
2
3
4
class MyNumber {
    int numberOut();
    int askNumber(int high, int low = 1);
};


Source:
1
2
3
4
5
6
int MyNumber::numberOut(){
    ...
}
int MyNumber::askNumber(int high, int low) {
    ...
}


But yeah, in classes you don't need to worry about forward declarations.
Last edited on
Aw man I should really kick my self for over thinking that. Thanks I got it working.

I also wanted to see if someone could give me a hint on how to properly triple a inputted string.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//out of the class (at least where the book says its suppose to be)
int triple(int number);
string triple(string text);
//

void tripleLoad(){
		
	string text;
		
		cout << "Type a word to repeat three times: " << triple(text);
		cin >> text;
	
	}
	
	string triple(string text)
	{
		return (text + text + text);
	}


I know if I just replace inside the ( ) with for example ( "dog ") it'll triple the word. But for ever reason when I type in a word/number it'll output the word but won't triple it.
Use cout << text << text << text;
you need to cout << triple(text); after cin >> text;
Topic archived. No new replies allowed.