Hey guys guys... i developed my first c program. it is simple.. please check it !!

Lich

Member
May 4, 2006
11,508
24
0
Warcraft III DOTA Map
fazaal24 said:
Wow elakiri machan :) Maath langadi indan thamai C patan gaththe :D Keep it up :)

EDIT:

Machan mama nikan Akuru dekak gahala baluwa(instead of numbers). Ethakotath uththarayak enawa neda? Eeka hadanna dannawada? just wanna learn :D XP kiwwa wage Validate karanna wei neda?
that because that Char converts to ASCII Value and prints. for that you need to Validate like X-pert said :)
 

Lich

Member
May 4, 2006
11,508
24
0
Warcraft III DOTA Map
fazaal24 said:
Thanks. Have to work on that.
ela

Here are some ASCII Values

ascii-0-127.gif
 

VIDUrUVAN123

Member
May 24, 2007
2,914
10
0
Sri lanka
x-pert said:
ElaKiri machang. Good job.

Next step should be to validate the input.

Try it out :D

Hint:
You might need to enter a masking aftr every input like
printf("\n Enter First No\n");
scanf("%d", &a);
if (a>=-9999 && a<9999) printf("Enter second number") scanf("bla bla")
else printf("invalid input... please enter a valid number again")


yeh validating should be there
 

Ranhiru

Member
Feb 2, 2007
6,438
42
0
Inside FIREFOX
Got this from a website...i didnt write the code though ill explain it :) This is for validation if the entered numbers are actually digits and not any char values :)
This is pretty much tough i guess for a beginner...ill try to explain line by line

//First take input using a string
printf("Enter a number :- ");
gets(str);

//Create variable's named flag and len

int flag,len;

//Assign the value 0 to flag
flag=0;

//Let len take the value of how much characters are there in the string
//strlen is a function defined in string.h so dont forget to include it too :)


len = strlen(str); /* str is the number in string format*/

// Now loop len times in the string (char array actually :) )

for(int i=0;i<len;i++)
{
if(!isdigit(str))
flag=1;
//isdigit is a function in ctype.h which will return whether the character is a digit or not...so you do it for all the letters in thearray :)
}

if(flag==0)
/* the string contains only digits*/
else
/* it contains other characters*/
..
..
..

isdigit is defined in "ctype.h"..

in case of any problem .. u can write it on your own as ..

int isdigit(char c)
{
if(c < '0' or c > '9')
return 0;
else
return 1;
}



This is the full code that i've come up with

Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>


void main()
{
char str[10];
int len=0,flag=0;

printf("Enter a number :- ");
gets(str);

len = strlen(str);

for(int i=0;i<len;i++)
 {
    if(!isdigit(str[i]))
         flag=1;
 }

 if (flag==0)
 printf("Only contains digits!");
 else
 printf("Contains non-digits");


}
Please ask if anything seems weird ... and if you dont understand any of this...dont worry even a bit! its your first day!!! bookmark this page and after about 2 or 3 weeks..after you have done loops and arrays just come and take a look! you will understand everything! :) hope this helps and did not confuse you

gud luck! :)
 
Last edited: