0
I have a data entry where the user will enter a certain amount of char
, this quantity comes from the first entries of the size of the vectors according to the user’s choice...
How the input of characters will work?
- The user will type in a single line all n characters requested, for example:
ABCDEFGHJILMN
- After user input each char will be in one
index
of*ponteiro
What is going on?
I can’t control data entry so the user doesn’t enter blank characters, for example:
Should he enter
A B C D
, whitespace will be stored in each index of the vector
What I’m trying to do?
According to the above written situation, I decided to get the idea to check which
index
contains the blank space, and after this delete (de-locate) the memory and leaving the same vector with a new amount of data and only with thechar
nonwhite...
NOTE [IMPORTANT]:
Default functions of languages cannot be used for vector handling (search, pertinence, insertion, deletion, sorting, etc.).
How could I do this in an elegant way and using good practice?
Just follow my algorithm:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <limits>
using namespace std;
void cleanScreen() {
system("CLS");
}
int validateDataEntry(istream& strm, int sze, char phrase[]){
char inputTester;
strm.get(inputTester);
if(sze < 0) {
cout << "============== PROGRAMA FINALIZADO! ==============" << endl;
exit(0);
} else {
if(strm.fail() || sze < 0 || inputTester == '.') {
while(true) {
char inputTesterTwo;
cleanScreen();
cout << "VALOR DIGITADO INVALIDO.. UTILIZE APENAS NUMEROS POSITIVOS & INTEIROS!" << endl;
cout << phrase << endl;
strm.clear();
strm.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
strm >> sze;
strm.get(inputTesterTwo);
if(!strm.fail() && sze > 0 && inputTesterTwo != '.') {
cleanScreen();
return sze;
}
if(sze < 0) {
cout << "============== PROGRAMA FINALIZADO! ==============" << endl;
exit(0);
}
}
} else {
return sze;
}
}
}
int main()
{
char *vetorA, *vetorB, *vetorC;
int sizeN = 0, sizeM = 0, sizeK = 0;
// ENTRADA DO VETOR A
cout << "DIGITE A QUANTIDADE DO [VETOR A]:" << endl;
cin >> sizeN;
sizeN = validateDataEntry(std::cin, sizeN, "DIGITE A QUANTIDADE DO [VETOR A]:");
// ENTRADA DO VETOR B
cout << "DIGITE A QUANTIDADE DO [VETOR B]:" << endl;
cin >> sizeM;
sizeM = validateDataEntry(std::cin, sizeM, "DIGITE A QUANTIDADE DO [VETOR B]:");
// ENTRADA DOS CARACTERES DO VETOR A
vetorA = new char[sizeN];
cout << "DIGITE [" << sizeN << "]" << " CARACTERES PARA O [VETOR A].." << endl;
cin.get(vetorA, sizeN + 1);
for(int i = 0; i < sizeN; i++) {
if (isspace(vetorA[i])) {
delete[] vetorA;
}
}
return 0;
}
If it’s c++ I think you’re complicating in several ways, not just by not using
std::string
how to force the non-use of language functions. What you want to do is toremove_if(str.begin(), str.end(), isspace);
assuming thatstr
is astd::string
.– Isac
generally speaking " my algorithm" is not used, unless you have created a new algorithm in some area of computer science...in which case the most correct is to say "my program"
– zentrunix