Function or loop to accept only positive numbers?

Hey guys,

First post here. I'm working on a numerical palindrome program for class. One function is supposed to prompt for a non-negative number and keep on prompting the user for a number if he enters a number less than zero. Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int getNumber()
{
	// 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 = Number;
	cout << "Please enter a non-negative number!\n";
	cin >> n;
	if (!cin || (n < 0)) //the number is negative
	{
	cout << "Please enter a non-negative number!\n";
	return 0;
	}
	else //the number is non-negative
	{
	cout << "The number you entered is " << n << endl;
}


I'm guessing I'll need a sort of loop, so I don't think this works. Any suggestions? Thanks!
If this is just a program that you desire to have loop I would suggest using void for this function. You may wish to implement a While loop in your main set with the condition for the termination.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getNumber()
{
	// 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;

	do
	{
		cout << "Please enter a non-negative number!\n";
		cin >> n;
	} while ( cin && n < 0 ); //the number is negative

	return ( ( cin ) ? n : -1 );
}
Last edited on
What about this? This is only part of the program. We only have to do one part at a time for class. Right now I have to put in the function to get a positive number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int getNumber(int n)
{
	// 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
    	do
    {
	cout << "Please enter a non-negative number\n";
	cin >> n
	cout << endl;
    }
	while (n < 0 && ( (int) n != n));
}
1. What is the point of the parameter n?
2. Your function states that it returns an integer, but no where in its source does it return anything.
Here's the whole code (the rest of it was supplied by the instructor):

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
62
63
64
65
66
67
68
69
void printGreeting();
// Prints a simple greeting to the user stating what this program does

int getNumber();
// Returns a non-negative integer input from user

bool isPalindrome(int n);
// Precondition: n is a non-negative integer
// Returns true if n is a palindrome, if not false

void printResult(int n);
// Precodition: n is a non-negative integer
// Postcondition: prints to the screen whether n is a palindrome or not

int main( )
{
	int number;
	
	printGreeting();
	number = getNumber();

	printResult(number);
	return 0;
}

void printGreeting()
{
	// prints out the following greeting to the screen
	// Welcome to palindrome checker.
	cout << "Welcome to palindrome checker!\n";
}

int getNumber(int n)
{
	// 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
    	do
    {
	cout << "Please enter a non-negative number\n";
	cin >> n
	cout << endl;
    }
	while (n < 0 && ( (int) n != n));
        return 0;
}

bool isPalindrome(int n)
{
	// This is a stub, ignore it for now
	return true;
}

void printResult(int n)
{
	// prints out to the screen whether n is a palindrome
	// it calls on isPalindrome with n
	// if isPalindrome returns true then the number is a palindrome
	// else the number is not a palindrome
	if (isPalindrome(n))  //the number is a palindrome
    {
       	cout << n << " is a palindrome!\n";
    }
    else //the number is not a palindrome
    {
       	cout << n << " is not a palindrome!\n";
    }
} 
Last edited on
return 0;

returning zero renders the entire function useless (you will always get zero back!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getNumber(int n)
{
	// 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
    	do
    {
	cout << "Please enter a non-negative number\n";
	cin >> n
	cout << endl;
    }
	while (n <= 0));
        return n;
}
Still, why do you have to give the function a number if it des not use it?
So what would I do then? This program determines if a given number is a palindrome. When coding it as georgewashere above did, I get this error:

error LNK2001: unresolved external symbol "int __cdecl getNumber(void)" (?getNumber@@YAHXZ)

EDIT:
Nevermind, I think I got it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getNumber()
{	
	
	// 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;
do
{
	cout << "Please enter a non-negative number!\n";
	cin >> n;
}
while (n < 0);
return n;
}
Last edited on
I showed you the function code in the very beginning. What is the problem?!
1
2
3
4
5
6
int getNumber(int n)
{
	// 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 
What is the point of the integer parameter? Does it make sense to have to give the function a number only for it to be ignored?
Vlad,

You're function wasn't working in the context of the whole program, but it helped me!

LB, that portion you are quoting is what the instructor wrote to instruct us as to what this stub's function is. The number does not get ignored. The number will be used in another part of the program.
marblesmike wrote:
The number does not get ignored. The number will be used in another part of the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getNumber(int n) //parameter: int n
{
	// 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
    	do
    {
	cout << "Please enter a non-negative number\n";
	cin >> n; //You just overwrote the value of n, the original parameter is lost
	cout << endl;
    }
	while (n <= 0));
        return n;
}
Last edited on
LB,

I see what you're saying. I think you missed my updated code that did work when the program was run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getNumber()
{	
	
	// 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;
do
{
	cout << "Please enter a non-negative number!\n";
	cin >> n;
}
while (n < 0);
return n;
}
Topic archived. No new replies allowed.