Displaying Function from Reference

Having a little trouble getting my function to display the results. I got my getNumber function working and I know I can't use return to return multiple values so I created a function that would display my results but I am a little lost on how I get the results from one function to another.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <array>
#include <iomanip>

using namespace std;

void printResult(int, int);
void getNumber(int &, int &);

void printResults (int, int)
{
	int n, n1;
	cout << "You have entered " << n << "and " << n1;
}

void getNumber(int &, int &)
{	
	
	// prompts the user for a non-negative number (>= 0)
	// reads in the number and check
	// keeps re-prompting user if the input is invalid (negative)
	// returns the non-negative input
	int n, n1;
	do
	{
		cout << "Please enter a non-negative number!\n";
		cin >> n >> n1;
	}
	while (n < 0 && n1 < 0);
	return;
}

int main() // Begins Program
{
	//Declare Arrays and Pointers
	int *arr;
	arr = new int[3];
	int *x, **y, ***z;
	x = arr;
	y = &x;
	z = &y;
	
	int s1,s2,n,n1;
	getNumber( s1, s2 ); // Calls Enter 2 Non Negative Number Function
	printResults(n,n1);


	//Program Termination

	cout << "Program Over\n" << endl;
	cout << "Press Enter to end -->" << endl;

	char c1, c2; // Variables to store user input
	c1 = getchar(); // get first input
	c2 = getchar(); // get second input
	if (c1 == '\n' && c2 == '\n') // if post inputs are enter
	exit(1); // exit
}





Why not get one number at a time? The function even asks for only one number, so imagine the user's surprise when they enter one number and then they think the program has frozen as it waits for a second number.
I mistyped the output Its meant to ask for 2 non-negative values
You should still ask for one number at a time. Currently if only one number is invalid they must retype both, which is unintuitive.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void getNumber(int &, int &)
{	
	
	// prompts the user for 2 non-negative numbers (>= 0)
	// reads in the 2 numbers and check
	// keeps re-prompting user if the input is invalid (negative)
	// returns the 2 non-negative input by reference.
	int n,n1;
	do
	{
		cout << "Please enter your first non-negative number!\n";
		cin >> n;
	}
	while (n < 0);
	do
	{
		cout << "Please enter your second non-negative number!\n";
		cin >> n1;
	}
	while (n1 < 0);
	return;
}

so something along the lines of this? and how would I go about passing the values of those into the display results
I mean the entire function should only ask for one number, return it, and then you can give the values it returned from calling it twice to the other function.
so call the getNumbers function ->return first value -> call getNumbers function -> return second value -> then im kinda lost on what to do after this.
Call printResults with the two values you got ;)
Now I get this error when I try to return the value that is input :(..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void getNumber(int &)
{	
	
	// prompts the user for a non-negative numbers (>= 0)
	// reads in the a number and checks
	// keeps re-prompting user if the input is invalid (negative)
	// returns the a non-negative input.
	int n;
	do
	{
		cout << "Please enter your first non-negative number!\n";
		cin >> n;
	}
	while (n < 0);
	return (n);
}
Change the return type from void to int and remove that int & parameter as you do not need it anymore ;)
Sorry im having a lot of trouble with this...
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <array>
#include <iomanip>

using namespace std;



void printResult(int, int);
void printResults (int, int)
{
	int n, n1;
	cout << "You have entered " << n << "and " << n1;
}

int getNumber()
{	
	
	// prompts the user for a non-negative numbers (>= 0)
	// reads in the a number and checks
	// keeps re-prompting user if the input is invalid (negative)
	// returns the a non-negative input.
	int n;
	do
	{
		cout << "Please enter 2 non-negative numbers!\n";
		cin >> n;
	}
	while (n < 0);
	return (n);
}

int main() // Begins Program
{
	//Declare Arrays and Pointers
	int *arr;
	arr = new int[3];
	int *x, **y, ***z;
	x = arr;
	y = &x;
	z = &y;

	int n,n1;
	getNumber(); // Calls Enter 2 Non Negative Number Function
	getNumber(); // Calls Enter 2 Non Negative Number Function
	printResults(n,n1);


	//Program Termination

	cout << "Program Over\n" << endl;
	cout << "Press Enter to end -->" << endl;

	char c1, c2; // Variables to store user input
	c1 = getchar(); // get first input
	c2 = getchar(); // get second input
	if (c1 == '\n' && c2 == '\n') // if post inputs are enter
	exit(1); // exit
}

Also my teacher doesn't want me to ask the question twice.. He wants it in this format.
Please enter two non-negative numbers.
1)
2)
I am still trying to figure out how to to get the 1) and 2) on their with cin lol...
You could pass a string to the getNumber function, e.g.
1
2
n1 = getNumber("1)");
n2 = getNumber("2)");
and in getNumber you display the string before asking for input.

Also on line 10 for printResults, name the parameters, don't create those new ints on line 12 as they are unrelated to the ones you just got from the user.
I'm so confused I kinda got the getNumber to work except it asks the question twice again.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <array>
#include <iomanip>
#include <string>

using namespace std;



void printResult(int, int);
void printResults (int ,int )
{
	cout << "You have entered " << n1 << "and " << n2;
}


int getNumber(string n1)
{	
	
	// prompts the user for a non-negative numbers (>= 0)
	// reads in the a number and checks
	// keeps re-prompting user if the input is invalid (negative)
	// returns the a non-negative input.
	int n;
	do
	{
		cout << "Please enter 2 non-negative numbers!\n";
		cout << n1;
		cin >> n;
	}
	while (n < 0);
	return (n);
}

int main() // Begins Program
{
	//Declare Arrays and Pointers
	int *arr;
	arr = new int[3];
	int *x, **y, ***z;
	x = arr;
	y = &x;
	z = &y;

	int n1,n2;
	n1 = getNumber("1)"); // Calls Enter 2 Non Negative Number Function
	n2 = getNumber("2)"); // Calls Enter 2 Non Negative Number Function
//	printResults(n,n1);


	//Program Termination

	cout << "Program Over\n" << endl;
	cout << "Press Enter to end -->" << endl;

	char c1, c2; // Variables to store user input
	c1 = getchar(); // get first input
	c2 = getchar(); // get second input
	if (c1 == '\n' && c2 == '\n') // if post inputs are enter
	exit(1); // exit
}

and Im still very lost on how to do printResults
Solved!! Thank You very much LB
Topic archived. No new replies allowed.