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