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.
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 );
}
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));
}
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
returntrue;
}
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";
}
}
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;
}
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;
}
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?
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.
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;
}
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;
}