d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How Can I Use Isalpha Function In This?
12Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2012 05:13pm
seemed like it would be a small fix maybe


Code
//block of code from something I had finished earlier
while (cin >> x) {
       if (x<0)
           cout << "Negative number detected: Ignored." << endl;
       else {
           total += x;
           total *= 1+rate;
       }
   }




I was wondering if I did a isalpha check in the if statement [ if (x < 0 || isalpha (stuff)) ] if it would work the way I want it to. Problem is I went to the cplusplus reference site and couldn't quite figure out how to use it properly.

'x' is declared as a double in this instance. I am wanting to make it so that if something other than an integer value is detected (in this case, an upper or lower case letter) that the program returns that cout (which I can change to reflect more than just negative numbers)
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 7 2012 06:27pm
this can be fairly tricky to a new person but essentially what you want to do is either use

sprintf or itoa

to assign your double to a cstring (array of characters) and then use the isdigit function to check it its a numeric number

Code
int check_number(int var)
{
     char cstring[25];
     
     sprintf(cstring, "%d", var)
     
    for(int i = 0; i < 25; i++)
    {
         if(!isdigit(cstring[i])) return 1;
     }
return 0;
}


i would personally do it this way because 1) it is coded in such a way to prevent buffer overflows 2) i like it better (although some may argue the first code segment is better because it doesnt use non standard functions and doesnt need to include other libraries such as stdlib and strings)

Code
int check(int var)
{
char cstring[25];

itoa(var, cstring, 25);

int len = strlen(cstring);

while(len-->0)
{
   if(!isdigit(cstring[len]) return 1;
}

return 0;
}



or something like that

usage would be something like

if(x < 0 || check(x))

This post was edited by AbDuCt on Jul 7 2012 06:29pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2012 06:37pm
ic, will try it later tonight if I can.

ty for post


some questions if you can answer:

is the int check(int var) just the first function you start off with, kind of like the main function?

Essentially creating some new block of code you deemed 'check' which includes an integer named 'var'?




if I can try to piece this together.....

you create a char named cstring which can hold up to 25 values (like an array, thank you google!)

then you convert the int var into cstring?? or something with itoa

----edit: I think I see what you do here. You take whatever input was put into some variable 'x' (an integer) and then covert that over into cstring. Right?

then you create the variable named len which checks the length of cstring??

and while the length of cstring --> 0 (not sure what that syntax means, approaches zero or something? :o) it says that if the isdigit function returns true on cstring[len], then it is changed to false by the '!' operator and will return 0. If it returns false on cstring, then the program returns an error (1)

This post was edited by Eep on Jul 7 2012 06:52pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 7 2012 07:45pm
Quote (Eep @ Jul 7 2012 08:37pm)
ic, will try it later tonight if I can.

ty for post


some questions if you can answer:

is the int check(int var) just the first function you start off with, kind of like the main function?

Essentially creating some new block of code you deemed 'check' which includes an integer named 'var'?




if I can try to piece this together.....

you create a char named cstring which can hold up to 25 values (like an array, thank you google!)

then you convert the int var into cstring?? or something with itoa

----edit: I think I see what you do here. You take whatever input was put into some variable 'x' (an integer) and then covert that over into cstring. Right?

then you create the variable named len which checks the length of cstring??

and while the length of cstring --> 0 (not sure what that syntax means, approaches zero or something? :o) it says that if the isdigit function returns true on cstring[len], then it is changed to false by the '!' operator and will return 0. If it returns false on cstring, then the program returns an error (1)


pretty much my 1 and 0's might be reversed i didnt test the code

while(len-->0) is just my shorthand for len-- > 0

it deincrements len then checks to see if its still greater than 0

itoa is a function that converts a decimal into a string (or in this case a array of characters which is all a string is)

then it jsut loops through and checks character by character if its a digit. if it does is the funciton returns true saying its not a digit, if not it returns false at the end

and yea that int check(int var) just means you are declaring a new function that accepts the variable (var) passed to it
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2012 08:34pm
very cool! thanks for the post

I like learning small things like this

This post was edited by Eep on Jul 7 2012 08:35pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2012 08:47pm
uhhh, quick question:

the compiler my school uses apparently doesn't include that cool itoa function. Is there any alternative or was that the only one :[

edit: forgot about your first example.

This post was edited by Eep on Jul 7 2012 08:47pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2012 09:42pm
Code
int main() {

   int var;
   char cstring[25];

   cout << "Enter something" << endl;

   cin >> var;

   sprintf(cstring, "%d", var);

   for (int i = 0; i < 25; i++) {
       if (!isdigit(cstring[i])) cout << "Try again~" << endl;
       else cout << "Works?" <<endl;
   }



return 0;
}



tried doing something like this (just to mess around) but I have problems.

Like, if I enter in '1' for the cin, the thing gives me back 25 responses. 3 of them work, apparently (returns that cout "works")

Is it supposed to do that?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 7 2012 09:56pm
you have to include <stdlib.h> and <strings.h> for the itoa to work

try remove the NOT (!) and see it works.

and do this

Code
for (int i = 0; i < 25; i++) {
      if (!isdigit(cstring[i])) cout << "Try again~" << endl;
      else
{
cout << "Works?" <<endl;
break
}
  }


test byinputting

1
a
1a
a1

should be

works
try again
something
try again

This post was edited by AbDuCt on Jul 7 2012 10:05pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2012 10:00pm
Quote (AbDuCt @ Jul 7 2012 10:56pm)
you have to include <stdlib.h> and <strings.h> for the itoa to work

try remove the NOT (!) and see it works.

and do this

Code
for (int i = 0; i < 25; i++) {
      if (!isdigit(cstring[i])) cout << "Try again~" << endl;
      else
{
cout << "Works?" <<endl;
break
}
  }


yeah apparently the compiler my school uses does not recognize itoa, no matter what I include.

I'll double check the 2nd one.


edit: seems to work! thanks. Even did your edit and got those values. Only issues is that I don't do anything after the 'if' is flagged, so it continues to run until it reaches the return 0, thus giving me both works! and try again :p




I have another question I have been wanting to know about if you can answer:


Is there any easy way to like, create a function or something that I can call on for future programs?

Like instead of adding 8 lines of code or whatever to my original post project, if I could just make something called like number_check which just does the above stated function for a specific thing?

Seems like it would be pretty complicated....

This post was edited by Eep on Jul 7 2012 10:08pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 7 2012 10:29pm
thats what my first post did

theyre called functions and what they do is what you want.

Code
#include <stdio.h>

void function( int x)

int main()
{
//main code

function(x);

return 0;
}

void function( int x)
{
//do stuff with the x variable that was passed to the function
}


Code
#include <stdio.h>

return_type function_name(variable_type variable_name)  <--function prototype (you need to declare this above your main entry point else you cant use the function and youll get errors)

int main()
{
//main code

function(x);

return 0;
}

return_type function_name(variable_type variable_name)
{
//do stuff with the x variable that was passed to the function
}


the functions return type is what type of data it returns. in this example the return type is void meaning nothing. if i changed it to INT it would return integer values. if i changed it to char it would return character values.
the variable type and name is the kind of variables you want to pass to the function for the function to use.

functions allow you to export code and to organize it more clearly.

you can also create your own libraries by exporting your functions to their own .c file and then create a .h file with the same name as the .c and include the function prototypes in the .h file then in your main() file you can do #include <xxxx.h> and use the functions.


Code
int check_number(int var)
{
    char cstring[25];
   
    sprintf(cstring, "%d", var)
   
   for(int i = 0; i < 25; i++)
   {
        if(!isdigit(cstring[i])) return 1;
    }
return 0;
}


this code works like this

1) put the integer into the cstring
2) loop through the string and see if the current character is a digit. if it is return with "yes"
3) if the entire string does not contain a alpha character return with "no"

the code you were testing on was flawed in the way of the test sample "1a" would return "no" if you used my break method.

edit:: and in any function when you issue a "return" the rest of the code after it will not run. example: if you tried to pass 11a to it it would only loop 3 times instead of 25 and will return "yes" or w/e

This post was edited by AbDuCt on Jul 7 2012 10:35pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll