How to add Error Checking

I've written a program to create two random arrays, splice the first up to a point the user chooses, and combine the splice and second array to a third array. My program works fine, my only thing is I'm running into trouble adding error checking for not accepting special characters or letters. I want to make it so if the user adds a number over 100, special characters or letters the program will spit out an error and ask them to re-enter a number without having to restart or crashing the program. Thoughts?
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
70
71
72
73
#include <iostream>

// Splice funtion to set the first two arrays, and amount of elements to be copied before spliced. Then points to new array.
int* splice(int *firstArray,  int firstArraySize, int *secondArray, int secondArraySize, int spliceSize)
{
	int *thirdArraySize = new int[firstArraySize + secondArraySize];
	for (int i = 0; i < spliceSize; i++)
		*(thirdArraySize + i) = *(firstArray+ i);
	for (int i = 0; i < secondArraySize; i++)
		*(thirdArraySize + spliceSize + i) = *(secondArray + i);
	for (int i = spliceSize; i < firstArraySize; i++)
		*(thirdArraySize + secondArraySize+ i) = *(firstArray + i);
	return thirdArraySize;
}

int main()
{
	int firstArraySize, secondArraySize, spliceSize;
	std::cout << "Enter the size of the first array: "; // Asks user for the size of first array.
	
	std::cin >> firstArraySize; // Assings inputed size to first array.
	
	std::cout << "Enter the size of the second array: ";  // Asks user for the size of second array.
	
	std::cin >> secondArraySize;// Assings inputed size to second array.
	
	std::cout << "Enter the number of elements of the first array to be copied before splice: ";// Asks user for the size of the splice.
	
	std::cin >> spliceSize; // Assings input ti splice size

	
	srand(100); // Creats the first two arrays and randomly fills them with 100 values
	int *firstArray = new int[firstArraySize];
	for (int i = 0; i < firstArraySize; i++)
		*(firstArray + i) = rand() % firstArraySize;

	int *secondArray = new int[secondArraySize];
	for (int i = 0; i < secondArraySize; i++)
		*(secondArray + i) = rand() % secondArraySize;

	
	std::cout << "The contents of the first array is: " << std::endl;
	for (int i = 0; i < firstArraySize; i++) // Outputs the contents of the first array in rows with 10 values/row.
	{
		std::cout << *(firstArray + i) << "\t";
		if ((i + 1) % 10 == 0)
			std::cout << std::endl;
	}
	
	std::cout << std::endl << "The contents of the second array is: " << std::endl;
	for (int i = 0; i < secondArraySize; i++) // Outputs the contents of the first array in rows with 10 values/row.
	{
		std::cout << *(secondArray+ i) << "\t";
		if ((i + 1) % 10 == 0)
			std::cout << std::endl;
	}

	int *thirdArray = splice(firstArray, firstArraySize, secondArray, secondArraySize, spliceSize);
	std::cout << " \nThe contents of the spliced array is: " << std::endl;
	for (int i = 0; i < firstArraySize + secondArraySize; i++) // Outputs the contents of the spliced array in rows with 10 values/row.
	{
		std::cout << *(thirdArray + i) << "\t";
		if ((i + 1) % 10 == 0)
			std::cout << std::endl;
	}

	delete[] firstArray; // Clears first array
	delete[] secondArray; // Clears second array
	delete[] thirdArray; // Clears third array

	system("pause");
	return 0;
}
I want to make it so if the user adds a number over 100, special characters or letters the program will spit out an error and ask them to re-enter a number without having to restart or crashing the program. Thoughts?


When the user enters the number, check if it's greater than 100, and if it is, ask them to enter it again.
Also, don't use new. DON'T USE NEW. There is NO need in this program for you to be doing manual memory management. If you're going to write C++ code, then write C++ code; not just C code with some extra bits in. Use a vector.

srand(100);
I see that you've hard-coded the seed value, so you'll get exactly the same results every time you run the program Is that really what you want?
Last edited on
The assignment requires us to allocate the memory though.
Write a function called splice() that “splices” two integer arrays together by first allocating memory for a dynamic array with enough room for both integer arrays, and then copying the elements of both arrays to the new array


Also yes
To test the function, write a program that:
Prompts the user for the sizes of the two integer arrays and the number of
elements of the first array to be copied before the splice (e.g., 32 means insert
32 elements of the first array and then the second array),
Creates the two arrays and fills them with random values. Use the seed 100 for
srand().

I see. You're being taught C code with some extra bits in. Unfortunate, but if that's the course being taught, not much you can do about it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int input_value;
bool input_value_is_bad = true;
while (input_value_is_bad)
{
  cout << "Enter value: ";
  cin >> input_value;
  if (input_value < 1 ||
      input_value > 100)
  {
      cout << "Value is unacceptable; must be greater than 0 and less than 101. Enter again. \n"
  }
  else
  {
    input_value_is_bad = false;
  }
}

Last edited on
How I would I do this without new then? It's supposed to be a C++ class.
1
2
3
4
5
6
7
8
9
vector<int> someNumbers;
int size;
cout << "How many numbers?";
cin >> size;

for (int i = 0; i < size; i++)
{
  someNumbers.push_back(rand() % size);
}


I see that you fill the sets of numbers with numbers from zero to size. Is that deliberate? Why are you putting rand() % size in, rather than just rand() ?

Here's Kate Gregory talking about this phenomenon: https://www.youtube.com/watch?v=YnWhqhNdYyk
Last edited on
a C with cout language solution, if I didnt forget anything. 1994 called and wants this code back when you are done with it.

int a[] = {1,2,3}; //these can be pointers if you are required.
int b[] = {4,5,6,7};
int *c = new int[7];
memcpy(c, a, 3*sizeof(int)); //3 is the size of the array
memcpy(c+3, b, 4*sizeof(int)); //c+3 is the next location to start writing in, 4* is size of second array.

Last edited on
Here is my updated code. Running out of time to finish this before I go to work (less then an hour and I won't get off before its due). Anything you suggest fixing real fast? I really appreciate the help!
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>

// Splice funtion to set the first two arrays, and amount of elements to be copied before spliced. Then points to new array.
int* splice(int *firstArray,  int firstArraySize, int *secondArray, int secondArraySize, int spliceSize)
{
	int *thirdArraySize = new int[firstArraySize + secondArraySize]; // Third Array is the size of the first plus the second.
	for (int i = 0; i < spliceSize; i++)
		*(thirdArraySize + i) = *(firstArray+ i); // First part of the thirdArray is the values of the first array up to the splice size.
	for (int i = 0; i < secondArraySize; i++)
		*(thirdArraySize + spliceSize + i) = *(secondArray + i); // Second part of the thirdArray is all of the secondArray.
	for (int i = spliceSize; i < firstArraySize; i++)
		*(thirdArraySize + secondArraySize+ i) = *(firstArray + i); //Last part of the thirdArray is the remainder of the firstArray after the splice.
	return thirdArraySize;
}

int main()
{
	int firstArraySize, secondArraySize, spliceSize;
	std::cout << "Enter the size of the first array(1-100): "; // Asks user for the size of first array.
	std::cin >> firstArraySize; // Assings inputed size to first array.
	if (firstArraySize > 100 || firstArraySize <= 0) // Error checking
	{
		std::cout << "Array's bigger than 100 and less then 1 are not allowed, please re-enter size of the array: ";
		std::cin >> firstArraySize;
	}
	
	std::cout << "Enter the size of the second array(1-100): ";  // Asks user for the size of second array.
	std::cin >> secondArraySize;// Assings inputed size to second array.
	if (secondArraySize > 100 || secondArraySize <= 0) // Error checking
	{
		std::cout << "Array's bigger than 100 and less than 1 are not allowed, please re-enter size of the array: ";
		std::cin >> firstArraySize;
	}
	
	std::cout << "Enter the number of elements of the first array to be copied before splice: ";// Asks user for the size of the splice.
	std::cin >> spliceSize; // Assings input to splice size
	if (spliceSize > firstArraySize) // Making sure the splice size isnt bigger then the first array.
	{
		std::cout << "The size of the splice cann't be bigger than that of the first array, please try again: ";
		std::cin >> spliceSize;
	}
	
	srand(100); // Creats the first two arrays and randomly fills them with 100 values
	int *firstArray = new int[firstArraySize];
	for (int i = 0; i < firstArraySize; i++)
		*(firstArray + i) = rand();

	int *secondArray = new int[secondArraySize];
	for (int i = 0; i < secondArraySize; i++)
		*(secondArray + i) = rand();

	
	std::cout << "The contents of the first array is: " << std::endl;
	for (int i = 0; i < firstArraySize; i++) // Outputs the contents of the first array in rows with 10 values/row.
	{
		std::cout << *(firstArray + i) << "\t";
		if ((i + 1) % 10 == 0)
			std::cout << std::endl;
	}
	
	std::cout << std::endl << "The contents of the second array is: " << std::endl;
	for (int i = 0; i < secondArraySize; i++) // Outputs the contents of the first array in rows with 10 values/row.
	{
		std::cout << *(secondArray+ i) << "\t";
		if ((i + 1) % 10 == 0)
			std::cout << std::endl;
	}


	int *thirdArray = splice(firstArray, firstArraySize, secondArray, secondArraySize, spliceSize);
	std::cout << " \nThe contents of the spliced array is: " << std::endl;
	for (int i = 0; i < firstArraySize + secondArraySize; i++) // Outputs the contents of the spliced array in rows with 10 values/row.
	{
		std::cout << *(thirdArray + i) << "\t";
		if ((i + 1) % 10 == 0)
			std::cout << std::endl;
	}

	delete[] firstArray; // Clears first array
	delete[] secondArray; // Clears second array
	delete[] thirdArray; // Clears third array

	system("pause");
	return 0;
}
You want to validate user input to be within specific parameters.

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
#include <iostream>
#include <string>

long GetNumber(const long, const long);

int main()
{
   const long min { 1 };
   const long max { 100 };

   long number { GetNumber(min, max) };

   std::cout << "The user entered " << number << '\n';
}

long GetNumber(const long min, const long max)
{
   long        number { };

   std::string input { };

   while (number < min || number > max)
   {
      std::cout << "Enter a number between " << min << " and " << max << ": ";

      std::getline(std::cin, input);
      std::cout << '\n';

      try
      {
         number = std::stol(input);
      }

      catch (...)
      {
         std::cout << "** INVALID INPUT! **\n\n";
         continue;
      }

      if (number < min || number > max)
      {
         std::cout << "** INVALID NUMBER! **\n\n";
         continue;
      }
   }

   return number;
}

Enter a number between 1 and 100: -1

** INVALID NUMBER! **

Enter a number between 1 and 100: a

** INVALID INPUT! **

Enter a number between 1 and 100: 50

The user entered 50
Here's a function to get a number between two bounds:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Prompt the user for an integer in the range [low, high) using
// "prompt".  Keep prompting until they enter a valid number
int getInt(const string &prompt,
	   int low, int high)
{
    int result;
    while (true) {
	cout << prompt << ' ';
	cin >> result;
	if (cout 		// stream is good
	    && low <= result
	    && result <= high) {
	    return result;
	}

	// If you get here then the input is invalid.

	// Clear any error and ignore input through the next end of line
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cout << "Invalid input. Please enter a number between "
	     << low << " and " << high << '\n';
    }
}


Other comments:
Line 6: consider changing thirdArraySize to thirdArray.
General comment: unless the professor forbids it, use array syntax, e.g., firstArray[i] instead of *(firstArray+i)
Lines 35 & 39: why are the items in firstArray and secondArray limited to their sizes?

For splice, I usually find it easier to keep a separate index for the destination array. It's clearer to me. This is what I mean (including the change to array syntax too):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Splice funtion to set the first two arrays, and amount of elements
// to be copied before spliced. Then points to new array.
int* splice(int *firstArray,  int firstArraySize,
            int *secondArray, int secondArraySize,
            int spliceSize)
{
    int *thirdArray = new int[firstArraySize + secondArraySize];
    int dest = 0;
    for (int i = 0; i < spliceSize; i++)
        thirdArray[dest++] = firstArray[i];
    for (int i = 0; i < secondArraySize; i++)
        thirdArray[dest++] = secondArray[i];
    for (int i = spliceSize; i < firstArraySize; i++)
        thirdArray[dest++] = firstArray[i];
    return thirdArray;
}

1
2
3
4
5
6
std::cin >> firstArraySize; // Assings inputed size to first array.
	if (firstArraySize > 100 || firstArraySize <= 0) // Error checking
	{
		std::cout << "Array's bigger than 100 and less then 1 are not allowed, please re-enter size of the array: ";
		std::cin >> firstArraySize;
	}


If the user enters a bad value the second time, it's accepted anyway.
Registered users can post here. Sign in or register to post.