d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How Can I Use Isalpha Function In This?
Prev12
Add Reply New Topic New Poll
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 7 2012 10:37pm
edit:: also found a design flaw. not sure how the function will react if you are passing a double to it that contains characters.

i think you are better off using cin with a string varaiable and then use atoi() to convert it into a integer like so

Code
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>


int check_number(string var);

int main()
{
  string test;
  cin >> test >>;

 if(!check_number(test))
 {
      negitive nuymber
  }

int number = atoi(test); //use "number" for now on to calculate
return 0;
}

int check_number(string var)
{
  int len = strlen(var);

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

if (atoi(var) < 0) return 1;

return 0;
}]


this way you can check if its a negative and a digit

This post was edited by AbDuCt on Jul 7 2012 10:47pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 7 2012 10:48pm
a better varrient

Code
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>


int check_number(string var);

int main()
{
  string test;
  cin >> test >>;

  int number = check_number(test);

 if(number < 0)
 {
      negitive nuymber
  }

//continue on

return 0;
}

int check_number(string var)
{
  int len = strlen(var);

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

return atoi(var);
}]


if you need help coding this so it works lmk i dont have a IDE open lol. im jsut doing this all freehand idk if it works

edit.. ill just open my compiler sec rofl

This post was edited by AbDuCt on Jul 7 2012 10:51pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2012 10:58pm
thanks for the help, currently pro occupied so sorry for lack of responses will check it all soon
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 7 2012 10:59pm
edit:: just check this post for code if you want to skip reading but i suggest you read it anyways lol. also the reason why you didnt have atoi and itoa is because i forgot they were located in ctypes.h (which you didn't include)

working code. just ignore the C functions such as printf/scanf they are equivelent to cin/cout

also

char *var

can be changed to

string var

and the

char in[25]

can be changed to

string in

Code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>


int check_number(char *var);



int main()
{
 char in[25];
 scanf("%s", in);

int number = check_number(in);

if(number < 0)
{
     printf("negitive");
     return 0;
 }

     printf("psoitve: %d", number);
     return 0;
}




int check_number(char *var)
{
 int len = strlen(var);

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

return atoi(var);
}


This post was edited by AbDuCt on Jul 7 2012 11:01pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 8 2012 12:19am
So I just put that into a program and I am not quite 100% sure what it does.

Seems like if you enter anything, the program stops after main function, so what is the code after that point doing?

So (again) let me see if I can comprehend this! (also an exercise!)

Program creates a string

scans a value into it

checks if the value inside is positive or negative and then ends the program?

next part does the thing where it reads everything inside of 'var' and if it sees anything that is a non-numeric it returns -1 (not sure what that flag is)

return atoi(var) -- guessing this converts var and prints it at the end or something?



Sorry I am pretty dumb to this kind of stuff.


edit: other random questions

what does the * before *var do? Like the line int check_number(char *var)?

This post was edited by Eep on Jul 8 2012 12:19am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 8 2012 01:02am
what when you call any function it passes "program flow" to the new function. so it will run code up until you call the "check" function and then the program will start executing the code starting after the function prototype (the return_type function_name(blah)).

then once the code is finished or has reached a return statement inside the function the function gives program flow back to the main() function which called it and will continue with the next line of code after the function call.

printf and scanf is just the C equivalent to cout and cin. (i dont actually know c++ >.>) all they do is output and input data to and from the screen.

Quote
next part does the thing where it reads everything inside of 'var' and if it sees anything that is a non-numeric it returns -1 (not sure what that flag is)


you can set your flags to anything you wish in reality. i could of returned -999999 if i wanted to but a simple -1 works in this case because when the function returns the "-1" back and sets it to the integer variable which is used in the if statement. since the number is negitive the true block would be called printing that it is a negative number.

Quote
return atoi(var) -- guessing this converts var and prints it at the end or something?


atoi takes a string and turns it into an integer type. (so IF THE PROGRAM DOES NOT RETURN -1 MEANING IT CONTAINS LETTERS OR SYMBOLS IN IT it returns the integer value of the string passed to the function.)

Code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>


int check_number(char *var); //char * means a character pointer in C. but in C++ just change char * to "string var"



int main()
{
char in[25];  //can change this to "string in;"
scanf("%s", in);  //can change this to "cin >> in >>"

int number = check_number(in);  //this calls the function and gives the string variable to it and what ever the function returns is stored in "number"

if(number < 0) //if number < 0 (if the function returned -1)
{
    printf("negitive");
    return 0;
}

    printf("psoitve: %d", number);
    return 0;
}




int check_number(char *var)  //again char * can be changed to "string var"
{
int len = strlen(var);   //get the length of the string (there might be a c++ equivalent here im not sure this is C syntax_

while(len-->0)  //go backwords through the string decrementing len every loop and checks to see if its grater than 0
{
     if(!isdigit(var[len]))  //if the current character in the string is not a digit
     {
             return -1;   //return -1 saying the string entered contains things other than numeric values
      }
 }

return atoi(var);  //return the string converted to an integer value to be stored in "numbers"
}


hope that helps.

the way that the function checks to see if its a negative number is that when they type say "-100" the "-" symbol would trigger the "is not digit" if statement and it would return -1 causing the ifstatement "if(number < 0)" to be true which would print negative number.

although if the entered number was "10000" the function would loop through each value (first 1, then 0, then 0, and so forth) checking to make sure each one is a digit. since they all are, the "return -1" is never called and the integer value "10000" would be stored in "number"

although if someone typoed like this "1000s99aks--" this would return -1 as well because the string has non numeric digits in it.

This post was edited by AbDuCt on Jul 8 2012 01:07am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 8 2012 01:10am
c++ version would look something like this (i didnt test it) compair the two and you will see the differences.

Code
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <string>


int check_number(string var);



int main()
{
string in;
cin >> in >>;

int number = check_number(in);

if(number < 0)
{
    cout << "negitive" <<;
    return 0;
}

    cout << "positive" <<;
    return 0;
}




int check_number(string var)
{
int len = var.length;            //found the c++ equivilent

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

return atoi(var);
}





edit: seeing how the length system works in c++ you could actually do this eliminating 1 variable.

Code
int check_number(string var)
{

for(int i = 0; i < var.length; i++)
{
     if(!isdigit(var[i]))
     {
             return -1;
      }
 }

return atoi(var);
}


both codes would do exactly the same thing

This post was edited by AbDuCt on Jul 8 2012 01:14am
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Jul 8 2012 10:09am
Actually, if you want to check if a string is a number in a type-safe way (as opposed to using printf and scanf functions family), it's as simple as that:

Code
#include <iostream>
#include <string>

using namespace std;

void checkIfNumber(string const& s)
{
try{
 int x = stoi(s);
 cout << "string '" << s << "' is an integer (" << x << ")" << endl;
 
}catch(invalid_argument const& e){
 cout << "string '" << s << "' is not an integer! (" << e.what() << ")" << endl;
}
}

int main()
{
checkIfNumber("123543");
checkIfNumber("Not an integer, obviously");
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 8 2012 11:28am
Quote (KrzaQ2 @ Jul 8 2012 12:09pm)
Actually, if you want to check if a string is a number in a type-safe way (as opposed to using printf and scanf functions family), it's as simple as that:


^better than my code +1

since i don't actually know c++ i could only help as best i can. although his example is the best it is more complicated than mine by a bit.

Code
#include <iostream>
#include <string>

using namespace std;

void checkIfNumber(string const& s)     //tje function returns nothing (void) and accepts a string address constant
{
try{         //try/catch is for error handling great addition to c++ C doesnt have it. basically it runs the code in the try block and if an exception or error occurs the catch block is ran preventing a crash
int x = stoi(s);   //trys to convert the string to an integer (if it errors the catch section is ran because the function errored)
cout << "string '" << s << "' is an integer (" << x << ")" << endl;  //cout

}catch(invalid_argument const& e){
cout << "string '" << s << "' is not an integer! (" << e.what() << ")" << endl;  //cout
}
}

int main()
{
checkIfNumber("123543"); //would print "is an integer"
checkIfNumber("Not an integer, obviously"); //would print "is not an intger
}


not sure how the function would react to negative numbers as i cant get it to compile on windows even with using the -std=c++0x flag. both ways would work. one is coded more simply (although it may seem advanced to you) in actual c++ while my varriant is made using code geared towards C.

This post was edited by AbDuCt on Jul 8 2012 11:29am
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll