Hello all!
I use two functions that solves the problem I had.
One from MSDN that clears the cin buffer and one function that I wrote myself, that checks if the input is an integer or not.
int main(void)
{
char *buffer = 0;
buffer = new char[11];
int x=0;
int nState=0;
bool inmatning = true;
while(inmatning)
{
// ................
// other code goes here
// ...................
// I only saves 9 numbers so the biggest
// number a user can enter is 999999999.
cin.getline(buffer,10);
nState = ClearError(cin);
if(CheckForInteger(buffer))
{
x=atoi(buffer);
if (x>=0)
{
//.................
// other code goes here..
//..................
} else
inmatning=false;
} else
cout << "\nWrong input!";
}
}
// Function from MSDN
int ClearError(istream& isIn) // Clears istream object
{
streambuf* sbpThis;
char szTempBuf[20];
int nCount, nRet = isIn.rdstate();
if (nRet) // Any errors?
{
isIn.clear(); // Clear error flags
sbpThis = isIn.rdbuf(); // Get streambuf pointer
nCount = sbpThis->in_avail(); // Number of characters in buffer
while (nCount) // Extract them to szTempBuf
{
if (nCount > 20)
{
sbpThis->sgetn(szTempBuf, 20);
nCount -= 20;
}
else
{
sbpThis->sgetn(szTempBuf, nCount);
nCount = 0;
}
}
}
return nRet;
}
// Function that checks if it's an integer or not.
bool CheckForInteger(char *input)
{
// int max = 2147483647;
// Initialize some variables.
bool heltal = true;
int length = 0;
// continue until the last char.
while(*input !='\0')
{
// if we find a '-' sign at the first pos.
// we ignore it.
if ( !(*input=='-' && length==0) )
{
// check if the current char is a
// digit.
if (!isdigit(*input))
{
// if not, we set the returnvalue to false.
heltal = false;
}
}
// go to next char and count allt the chars.
input++; length++;
}
// go back to first char in buffer.
input = input - length;
// return result.
return heltal;
}
Thanks for all your help and thanks for a great forum and a great community.
BR
Jörgen Jönsson
===========================================
Now I only owns one A500. I will buy a A-One when a new
model reaches the market (I'm optimistic

)