Post: Logic Erorr [C++]
03-08-2011, 02:37 PM #1
(adsbygoogle = window.adsbygoogle || []).push({});
    #include <iostream>
#include <conio.h>

#define isNum(x) (x>=48 && x<=57) ? 1 : 0
#define isChar(x) ( (x>=65 && x<=90) || (x>=97 && x<=122) ) ? 1 : 0
#define isAction(x) (x == 13 || x == Cool Man (aka Tustin) ? 1 : 0
#define isSpace(x) (x == 32) ? 1 : 0

std::string inputstream(bool filterText,bool filterNum,bool filterSpace,unsigned char maxLen)
{
//if filter text and filter number, nothing can be entered, return null;
//if getting num input, but allowed spaces, number will fail
if((filterText && filterNum) || (!filterNum && !filterSpace))
{
return NULL;
}

//creating an input line
struct input {
std::string line;
unsigned char length;
unsigned char maxLength;
unsigned char kb_hit;
bool textOnly;
bool numOnly;
bool spaceAllow;
};

//declaring our stream
input stream;

//initializing our stream
stream.line = "";
stream.length = 0;
stream.maxLength = maxLen;
stream.textOnly = !filterText;
stream.numOnly = !filterNum;
stream.spaceAllow = !filterSpace;

//input loop (monitor)
for(;Winky Winky
{
_sleep(50);
//if keyboard not hit, wait 50 miliseconds
if(kbhit())
{
//record hit key
stream.kb_hit=getch();
//if keyboard input not valid reguardless of what type of input, this proccess is void
if(!isNum(stream.kb_hit) && !isChar(stream.kb_hit) && !isAction(stream.kb_hit) && !isSpace(stream.kb_hit))
{
std::cout<<"INVALID";
//clear any double ascii key events
while(kbhit())
{

stream.kb_hit = getch();
}
//skip asigning to stream line
continue;
}
//if user backspace or enter
if(isAction(stream.kb_hit))
{
std::cout<<"0";
switch(stream.kb_hit)
{
case 8: if(stream.length!=0)
{
std::cout<<"\b \b";
stream.line.erase(stream.line.end()-1,stream.line.end());
}
break;
case 13: std::cout<<std::endl;
return stream.line;
break;
}
continue;
}
//add keyboard event to streamline depending on filter type
if(stream.spaceAllow && stream.kb_hit == 32)
{
std::cout<<stream.kb_hit;
stream.line += stream.kb_hit;
stream.length++;
continue;
}
if(stream.textOnly && isChar(stream.kb_hit))
{
std::cout<<stream.kb_hit;
stream.line += stream.kb_hit;
stream.length++;
continue;
}
if(stream.numOnly && isNum(stream.kb_hit))
{
std::cout<<stream.kb_hit;
stream.line += stream.kb_hit;
stream.length++;
continue;
}
}
}
}

int stringToNum(std::string param)
{
int temp;

for(unsigned char index = 0;index<param.length();index++)
{
temp+= (param[index] - 4Cool Man (aka Tustin);
temp *= 10;
}
temp /= 10;

return temp;
}

int main()
{
using namespace std;

string name = "";
int age;

cout<<"Enter your name (12 letters max): ";
name = inputstream(false,true,false,12);

cout<<"Enter your age (2 digit Max): ";
age = stringToNum(inputstream(true,false,true,2));

system("CLS");

cout<<"Your name is: "<<name<<endl;
cout<<"Your age is: "<<age<<endl;
system("PAUSE");
return 0;
}


During this call

if(!isNum(stream.kb_hit) && !isChar(stream.kb_hit) && !isAction(stream.kb_hit) && !isSpace(stream.kb_hit))

it should return false is a character, space, number, or action has been pressed. but it returns true some how :(.

---------- Post added at 09:37 AM ---------- Previous post was at 09:27 AM ----------

Finished

    //input.h
//TheUberFail

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

#define isNum(x) (x>=48 && x<=57) ? 1 : 0
#define isChar(x) ( (x>=65 && x<=90) || (x>=97 && x<=122) ) ? 1 : 0
#define isAction(x) (x == 13 || x == Cool Man (aka Tustin) ? 1 : 0
#define isSpace(x) (x == 32) ? 1 : 0

std::string inputstream(bool filterText,bool filterNum,bool filterSpace,unsigned char maxLen)
{
//if filter text and filter number, nothing can be entered, return null;
//if getting num input, but allowed spaces, number will fail
if((filterText && filterNum) || (!filterNum && !filterSpace))
{
return NULL;
}

//creating an input line
struct input {
std::string line;
unsigned char length;
unsigned char maxLength;
unsigned char kb_hit;
bool textOnly;
bool numOnly;
bool spaceAllow;
};

//declaring our stream
input stream;

//initializing our stream
stream.line = "";
stream.length = 0;
stream.maxLength = maxLen;
stream.textOnly = !filterText;
stream.numOnly = !filterNum;
stream.spaceAllow = !filterSpace;

//input loop (monitor)
for(;Winky Winky
{
//if keyboard not hit, wait 50 miliseconds
_sleep(50);
if(kbhit())
{
//record hit key
stream.kb_hit=getch();
//if keyboard input not valid reguardless of what type of input, this proccess is void
if(!(isNum(stream.kb_hit)) && !(isChar(stream.kb_hit)) && !(isAction(stream.kb_hit)) && !(isSpace(stream.kb_hit)))
{
//clear any double ascii key events
while(kbhit())
{

stream.kb_hit = getch();
}
//skip asigning to stream line
continue;
}
//if user backspace or enter
if(isAction(stream.kb_hit))
{
switch(stream.kb_hit)
{
case 8: if(stream.length!=0)
{
std::cout<<"\b \b";
stream.length--;
stream.line.erase(stream.line.end()-1,stream.line.end());
}
break;
case 13: std::cout<<std::endl;
return stream.line;
break;
}
continue;
}
//add keyboard event to streamline depending on filter type
if(stream.length < stream.maxLength)
{
if(stream.spaceAllow && stream.kb_hit == 32)
{
std::cout<<stream.kb_hit;
stream.line += stream.kb_hit;
stream.length++;
continue;
}
if(stream.textOnly && isChar(stream.kb_hit))
{
std::cout<<stream.kb_hit;
stream.line += stream.kb_hit;
stream.length++;
continue;
}
if(stream.numOnly && isNum(stream.kb_hit))
{
std::cout<<stream.kb_hit;
stream.line += stream.kb_hit;
stream.length++;
continue;
}
}
}
}
}

int stringToNum(std::string param)
{
int temp = 0;

for(unsigned char index = 0;index<param.length();index++)
{
temp += (param[index] - 4Cool Man (aka Tustin);
temp *= 10;
}
temp /= 10;

return temp;
}


Forgot they are not functions, they are preprocessor logic
if((!isNum(stream.kb_hit)) && !(isChar(stream.kb_hit)) && !(isAction(stream.kb_hit)) && !(isSpace(stream.kb_hit))) Works fine

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo