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;
}
02-26-2011, 09:46 PM #2
Girby2K11
☮ ☯ ☢ ✔ ➝
ive always wondered what does system("CLS") do?
02-26-2011, 09:50 PM #3
Originally posted by Girby2K11 View Post
ive always wondered what does system("CLS") do?


Same as the CMD "cls" command. it stands for clear screen.
02-26-2011, 09:56 PM #4
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by TheUberFail View Post
Same as the CMD "cls" command. it stands for clear screen.


OH. thats quite useful. like for a tic tac toe game?
03-01-2011, 08:47 PM #5
kiwimoosical
Bounty hunter
Originally posted by Girby2K11 View Post
OH. thats quite useful. like for a tic tac toe game?


It's a bad programming habit, system calls are terrible in general. I don't know why he teaches if he uses them, he should know that.
03-02-2011, 05:29 PM #6
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by kiwimoosical View Post
It's a bad programming habit, system calls are terrible in general. I don't know why he teaches if he uses them, he should know that.


does it use alot of the memory? system calls
03-02-2011, 08:12 PM #7
kiwimoosical
Bounty hunter
Originally posted by Girby2K11 View Post
does it use alot of the memory? system calls


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
03-02-2011, 10:42 PM #8
Girby2K11
☮ ☯ ☢ ✔ ➝
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


is there another way to remove all text on the screen without system calls?
03-03-2011, 02:34 AM #9
kiwimoosical
Bounty hunter
Originally posted by Girby2K11 View Post
is there another way to remove all text on the screen without system calls?


    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"<<std::endl;
03-03-2011, 03:27 PM #10
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by kiwimoosical View Post
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"<<std::endl;


doesnt seem neat though lol.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo