Answer the question
In order to leave comments, you need to log in
How to disable character input in this code?
Hello. Noob, that is, I, slows down a lot. In the program, you need to prohibit the input of letters. Forbid in the designation of the length of the array forbade, but violated the code. Further, the size is not taken into account and goes to infinity. How to fix? Thanks in advance.
#include <iostream>
#include <windows.h>
#include<string>
#include<conio.h>
#include<cstdlib>
using namespace std;
bool test(char ch,const char *keys,unsigned int size)
{
for(int i=0;i<size ;i++ )
if(keys[i] == ch) return true;
return false;
}
int f(int *array, int size)
{
int newSize = size;
for (int i = 1; i < newSize; i++)
{
if (array[0] == array[i])
{
for (int j = i; j < newSize - 1; j++)
{
array[j] = array[j+1];
}
newSize--;
i--;
}
}
return newSize;
}
void printArray(int *array, int size)
{
std::cout << "Длина массива = " << size << " : ";
for (int i = 0; i < size; ++i)
{
std::cout << array[i] << " ";
}
std::cout << std::endl;
}
int main()
{
setlocale (LC_ALL, "RUS");
int arraySize;
int *array;
std::cout << "Размер массива = ";
{char numbers[10] = {'0','1','2','3','4','5','6','7','8','9'};
string arraySize;
char ch;
while((ch=getch()) != '\r')//enter
if(test(ch,numbers,10))
{
arraySize += ch;
cout<<ch;
}
}
std::cout << std::endl;
array = new int[arraySize];
for (int i = 0; i < arraySize; ++i)
{
std::cout << "Число[" << i <<"] = ";
std::cin >> array[i];
}
printArray(array, arraySize);
arraySize = f(array, arraySize);
printArray(array, arraySize);
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question