Post: (C++ Header) Input blocking
02-26-2011, 09:36 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Quick post sorry,

save as "getInput.h"

this will block anything other than numbers when getting a number, and filter a string upon input.

getInputString(bool filterNum,bool filterChar,bool filterSym,bool filterSpace,bool rejectString,string defaultStr);

Explained below.

getInputString(Filter numbers true:false, Filter characters true:false,
Filter symbols true:false, Filter spaces true:false, if bad input replace string with default true:false, default string to replace with "STRING");

    //getInput.h, made by TheUberFail

#include <iostream>
#include <string.h>
#include <windows.h>
#include <conio.h>

using namespace std;

string getInputString(bool filterNum,bool filterChar,bool filterSym,bool filterSpace,bool rejectString,string defaultStr)
{
string Oinput ="";
string Ainput ="";

short counter = 0;

getline(cin,Oinput);

for(short index = 0; index < Oinput.length(); index++)
{
if(filterNum)
{
if(Oinput[index]<='9'&&Oinput[index]>='1'Winky Winky
{
if(rejectString)
{
return defaultStr;
}
continue;
}
}
if(filterSym)
{
if(!(Oinput[index]<='9'&&Oinput[index]>='1'Winky Winky&&!(Oinput[index]<='Z'&&Oinput[index]>='A'Winky Winky&&!(Oinput[index]<='z'&&Oinput[index]>='a'Winky Winky&&Oinput[index]!=' 'Winky Winky
{
if(rejectString)
{
return defaultStr;
}
continue;
}
}
if(filterSym)
{
if((Oinput[index]<='Z'&&Oinput[index]>='A'Winky Winky||(Oinput[index]<='z'&&Oinput[index]>='a'Winky Winky)
{
if(rejectString)
{
return defaultStr;
}
continue;
}
}
if(filterSpace)
{
if(Oinput[index]==' 'Winky Winky
{
if(rejectString)
{
return defaultStr;
}
continue;
}
}
Ainput += Oinput [index];
continue;
}
return Ainput;
}

int getNumber()
{
string ILine = "";
char input_stream;
short charCounter = 0;
int number = 0;
int power = 0;

for(;Winky Winky
{
if(kbhit())
{
input_stream = getch();
if(charCounter!=0)
{
if(input_stream == '0'Winky Winky
{
ILine += input_stream;
charCounter++;
cout<<input_stream;
}
}
if(input_stream >= '1' && input_stream <= '9'Winky Winky
{
ILine += input_stream;
charCounter++;
cout<<input_stream;
}
if(input_stream == Cool Man (aka Tustin)
{
if(charCounter!=0)
{
cout<<input_stream<<" "<<input_stream;
ILine.erase(ILine.end()-1,ILine.end());
charCounter--;
}
}
if(input_stream==13)
{
break;
}
}
Sleep(50);
}

for(short count = 0; count < ILine.length(); count++)
{
power =1;
//if(count != 0)
{
for(short sCount = 0; sCount <= count; sCount++)
{
power *= 10;
}
}
number += ((((int)ILine[ILine.length()-(count)-1])-4Cool Man (aka Tustin)*power);
}
number /= 10;
return number;
}

void doStringExample()
{
system("CLS");

string tempStr;

cout<<"\n\tEnter a number: ";
// Dont filter number Filter alphabet Filter symbols Filter spaces Reject bad input If rejected, this is default
tempStr = getInputString(0, 1, 1, 1, 0, "");

cout<<"\n\tYou entered: "<<tempStr;

cout<<"\n\tEnter a sentance with no numbers\n\n\tSentance: ";

// filter number Dont Filter alphabet Dont Filter symbols Dont Filter spaces Reject bad input If rejected, this is default
tempStr = getInputString(1, 0, 0, 0, 1, "You entered numbers!");

cout<<"\n\tYou entered: "<<tempStr;

system("PAUSE");
system("CLS");
return;
}
03-03-2011, 07:01 PM #11
Originally posted by kiwimoosical View Post
Not much, but just read.

It is bad for several reasons:
-It isn't portable.
-It only works if the OS has the function you call in it. (System refers to OS hypothetically).
This is what system does:

~Halt your program.
~Make a sys call to the OS.
~Relaunch the OS in a sub process.
~Find the command in the process.
~Allocate the memory to execute the command
~Execute the command
~Deallocate the memory
~exit the OS sub process.
~resume your program

Whereas there is usually a better way of doing nearly everything without a system call. Smile
I hope you learned from your mini lesson in programming by Kiwimoosical Happy lulz


nothing wrong with using a system call here, its not like where developing an application :P
03-03-2011, 08:42 PM #12
kiwimoosical
Bounty hunter
Originally posted by Girby2K11 View Post
doesnt seem neat though lol.


    #include <conio.h>

clrscr();


---------- Post added at 03:42 PM ---------- Previous post was at 03:34 PM ----------

Originally posted by TheUberFail View Post
nothing wrong with using a system call here, its not like where developing an application :P


Bad programming habit.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo