Image

Imageiyouboushi wrote in Imagecpp

validing incoming data

Hi everyone. I'm rather new in C++ and I'm running into a problem with something I'm trying to attempt.

Basically here's what I'm trying to do:

I'm asking the user to input an integer number, up to 100 numbers, to be stored into an integer array. No problem. The problem is that I want it to break the loop and stop asking for numbers if anything ELSE is entered (be it string, float, char, short, whatever). That's where I'm having problems.

At first I thouht I could do it by comparing the footprints of the incoming number, as a character uses less bytes than an int, which uses less than a float, and so on. I figured "well, if they don't match then it's not an integer, boom I'm out of the loop." But no, it's not working. In fact, it's producing crazy results in which I type in a character, or a float/double/whatever and it basically loops the last number over and over until the array is filled.

Does anyone know of a way to help me fix this problem?


int main() {
	
	// Declare constants, arrays and variables
	const int MAXNUMINTEGERS = 100;
	int integerArray[MAXNUMINTEGERS];
        int totalNumberOfElements = 0;
	int currentInput = 0;

    // Display the program information to the user
	std::cout << "*********************************************************\n";
	std::cout << "This program will accept up to 100 integers and create an\n";
	std::cout << "array that will return the mean and median.\n";
	std::cout << "*********************************************************\n\n";

	// Get the integer inputs from the user until either a non-integer is entered
	// or an "End of File" (in this case, we want MAXNUMINTEGERS) is reached.
    do
	{
		std::cout << "Please input an integer number to be added to the array: ";
		std::cin >> currentInput;

		if (sizeof(currentInput) == sizeof(0)) 
		{
			// In this case totalNumberOfElements also acts as the current element slot that is available
			integerArray[totalNumberOfElements] = currentInput;
			totalNumberOfElements++;
		}

	} 	while ((totalNumberOfElements < MAXNUMINTEGERS) && (sizeof(currentInput) == sizeof(0)));

	// Display the original array to the user
	std::cout << "\n*********************************************************\n";
	std::cout << "You have entered the following numbers:\n";

	for (int i = 0; i < totalNumberOfElements; ++i) { 
		// Format the output so that all elements below the 100th will input a ,
		// While the final output won't have that
		if (i < 99)
			std::cout << integerArray[i] << ", ";
		else 
			std::cout << integerArray[i];
	}
	return 0;
}



Of course I'm going to be doing some operations on the array once I figure out how to make the loop break when a non-integer is entered.

Thanks for any help.
=========

Edit:

My thanks goes to coises. I eventually decided to allow floating point numbers with a warning that they would result in a loss of accuracy, as I'll be dropping the decimal point and casting it into an int. I really don't want to deal with strings and conversions or checking every digit that comes in to make sure it's within 0 to 9, and I think this will be easier. I mean I have it say "INTEGER" at least two or three times before the first number is even entered. But it was coises who helped me get it working so that it'd stop if a nondigit was entered.


int main() {
	
	// Declare constants, arrays and variables
	const int MAXNUMINTEGERS = 100;
	int integerArray[MAXNUMINTEGERS];
        int totalNumberOfElements = 0;
	float currentInputNumber = 0;

    // Display the program information to the user
	std::cout << "\n************************************************************\n";
	std::cout << "This program will accept up to 100 integers and create an\n";
	std::cout << "array that will return the mean and median.\n\n";
	std::cout << "NOTE: Entering a floating-point number will result in a loss\n";
	std::cout << "of precision, as it will be converted into an integer.\n";
	std::cout << "************************************************************\n\n";

	// Get the inputs from the user until either a nondigit is entered 
	// or an "End of File" (in this case, MAXNUMINTEGERS) is reached.
    do
	{
		std::cout << "Please input an integer number to be added to the array: ";
		if(!(std::cin >> currentInputNumber)) {
			std::cout << "\nA nondigit number was detected.  Input has been cancelled.\n";
			break;
		}

		integerArray[totalNumberOfElements] = (int)currentInputNumber; 
		totalNumberOfElements++;

	} 	while (totalNumberOfElements < MAXNUMINTEGERS);

	return 0;
}