-
March 29th, 2009, 12:22 PM
#1
Inifinite loop problem (easy question)
Hey all, sorry for all the beginner's questions
Anyways, I'm trying to build a simple loop that will protect against unwanted input (i.e. when the user enters a characters instead of an integer). Here's what I have so far:
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter a number: ";
int number;
cin >> number;
while (!isdigit(number)) //Created an infinite loop!
{cout << endl << "That is not a number, please try again: ";
cin >> number;}
cout << endl << "Enter a character: ";
char ch;
cin >> ch;
cout << endl << number << " " << ch;
int wait;
cin >> wait;
return 0;
}
First of all, am I correct that (!isdigit(number)) is really saying "Do this while the input is not an integer?"
Whenever I input a numeric value, say 3, I get the message stating "That is not a number, please try again"??!
Whenever I input a character value, I get an infinite loop stating" "That is not a number, please try again", and the program ignores the cin >> number within the loop.
Can anyone tell me whats wrong here?
Thanks.
-
March 29th, 2009, 12:31 PM
#2
Re: Inifinite loop problem (easy question)
Be careful. 2484 is an integer, but not a digit. I have no time to check it, sorry.
PD: You can read a char* and then check if num[i] is a digit.
-
March 29th, 2009, 12:32 PM
#3
Re: Inifinite loop problem (easy question)
Hmmm...nevermind, this appears to be a character function. Not an integer function. I'll have to come up with a different way to do this
-
March 29th, 2009, 01:28 PM
#4
Re: Inifinite loop problem (easy question)
Hi
I think you should declare the "number" variable as char. I don know why. In another way, I would like to advise you some method to check the number which is atoi. It return 0 if the input cannot be converted to a value of that type.
correct me if t wrong
thx
-
March 29th, 2009, 02:16 PM
#5
Re: Inifinite loop problem (easy question)
Sunton
Yeah, I think that solution works. I'll just have to remember not to use the variable as an INT later on.
Thanks
-
March 29th, 2009, 02:20 PM
#6
Re: Inifinite loop problem (easy question)
do this:
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
int number;
while(true)
{
cout << "Enter a number: ";
string numberS;
cin >> numberS;
number = atoi(numberS.c_str());
if (itisanumber(numberS))
break;
else
cout << "\nThat is not a number, please try again: ";
}
cout << endl << "Enter a character: ";
char ch;
cin >> ch;
cout << endl << number << " " << ch;
int wait;
cin >> wait;
return 0;
}
itisanumber() function you write yourself to check if the string is a number.
-
March 29th, 2009, 04:17 PM
#7
Re: Inifinite loop problem (easy question)
Thanks for the help everyone. I was able to find fix to the problem by using cin.clear and cin.get() to clean up my input. Here's a slightly modified version of the program:
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter a number (or enter 77 to exit): "; //"Lost" reference
int number=0;
int sum=0;
while (number!=77)
{
if (!(cin >> number))
{ cout << "That is not a number. Please enter a number: ";
cin.clear(); // Resets cin error flag
cin.get(); // Removes bad input
}
else if (number==77)
break;
else
{
sum+=number;
cout << endl << "Enter another number: ";
}
}
cout << endl << "Total is: " << sum;
int wait;
cin >> wait;
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|