R
Ram
Hi All,
Firstly i am a newbie and trying to learn C.
The background of the problem is
Program:
Presently I am working on a program of numerology and the I/P will be
the name and output will be a digit for which there are known
characteristics which i will print.
Assume Input: ram spartan
output: a single digit number by adding up the corresponding number to
these characters till u get <9 or 11 or 22 and corresponding to that
digit there are a list of known characteristics which is to be
printed. Refer the link for the table which contains the number
equivalent of the character.
http://www.astrology-numerology.com/num-expression.html
So i need to add r,a,m corresponding digits (e.g. 9+1+4=14) Now again
add 14 as 1+4=5 so i have got the numeral sub_res as 5 for ram.
similarly i need to do for spartan sum it up and get it to a single
digit which is either < 9 or == 11 or == 22. (assume spartan's final
sum is 8). The res will be sum of ram and spartans value 5+8= 13. This
again needs to be added up till the result is either res < 9 or res ==
11 or res == 22
so that i can print the list of characteristics from 1 - 9 and for 11
and for 22 which is available to me. i can use switch case for this.
Code.
i am pasting the code i have written for this.
// C code for numerology
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define BUFSIZE 100
int main ()
{
int i,c,cnt=0,count=0;
int sub_sum=0,res=0;
char ch,chr;
char str[100],tmp[1];
char name[BUFSIZE] = {'\0'}; // setting char array to all null
characters
printf ("Enter your name (ONLY CAPITAL LETTERS) :- ");
fgets (name,BUFSIZE,stdin);
for (i=0;name!='\0';i++) // loop to check for end of string
{
if(name != ' ') // condition for space
{
ch=name;
count=((ch-'A')%9)+1;
sub_sum=sub_sum+count;
// printf("%d\n",sub_sum);
}
else
{
while ((sub_sum > 9) && (sub_sum != 11) &&(sub_sum != 22))
{
sprintf( str, "%d" , sub_sum ); //Convert the number to
string
for ( i=0; str!='\0'; i++)
{
tmp[0]=str;
c=atoi(tmp); // I need help here
cnt=cnt+c;
}
printf ("Count is the value of sum of the two integers %d\n",cnt);
if ((cnt > 9) && (cnt != 11) && (cnt != 22))
{
continue;
}
else
{
res += cnt;
sub_sum=0;
break;
}
}
}
}
printf("The sum of the words are %d\n",res);
// The code is incomplete i have to again do a sum of res if that
turns to be a double digit and then check if // the final result will
be <9 or == 11 or == 22 and if this is fine then pass it to switch
case to get me the corresponding characteristics of the name.
}
The question to the group is on the atoi function.
I am converting the integer to string and so that
-> i can parse the string,
-> convert it back to integer and
-> sum it up.
( For e.g. in the above code i have sub_sum as 14)
1. i am checking for the condition is true then i am converting the
integer to string using sprintf. This works fine. The string is
declared as an character array. In this program char str[100];
2. Then using for loop to check for end of line
now str[0] = 1 and later str[1] = 4
now i use atoi to convert the string to integer so that i can
perform the sum.
Can someone please explain me what i am doing wrong. atoi needs the
string and i am passing a character so i tried passing the charecter
to a tmp[] and passing that to atoi function but doesnt solve my
purpose.
Any help would be much appreciated.
Firstly i am a newbie and trying to learn C.
The background of the problem is
Program:
Presently I am working on a program of numerology and the I/P will be
the name and output will be a digit for which there are known
characteristics which i will print.
Assume Input: ram spartan
output: a single digit number by adding up the corresponding number to
these characters till u get <9 or 11 or 22 and corresponding to that
digit there are a list of known characteristics which is to be
printed. Refer the link for the table which contains the number
equivalent of the character.
http://www.astrology-numerology.com/num-expression.html
So i need to add r,a,m corresponding digits (e.g. 9+1+4=14) Now again
add 14 as 1+4=5 so i have got the numeral sub_res as 5 for ram.
similarly i need to do for spartan sum it up and get it to a single
digit which is either < 9 or == 11 or == 22. (assume spartan's final
sum is 8). The res will be sum of ram and spartans value 5+8= 13. This
again needs to be added up till the result is either res < 9 or res ==
11 or res == 22
so that i can print the list of characteristics from 1 - 9 and for 11
and for 22 which is available to me. i can use switch case for this.
Code.
i am pasting the code i have written for this.
// C code for numerology
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define BUFSIZE 100
int main ()
{
int i,c,cnt=0,count=0;
int sub_sum=0,res=0;
char ch,chr;
char str[100],tmp[1];
char name[BUFSIZE] = {'\0'}; // setting char array to all null
characters
printf ("Enter your name (ONLY CAPITAL LETTERS) :- ");
fgets (name,BUFSIZE,stdin);
for (i=0;name!='\0';i++) // loop to check for end of string
{
if(name != ' ') // condition for space
{
ch=name;
count=((ch-'A')%9)+1;
sub_sum=sub_sum+count;
// printf("%d\n",sub_sum);
}
else
{
while ((sub_sum > 9) && (sub_sum != 11) &&(sub_sum != 22))
{
sprintf( str, "%d" , sub_sum ); //Convert the number to
string
for ( i=0; str!='\0'; i++)
{
tmp[0]=str;
c=atoi(tmp); // I need help here
cnt=cnt+c;
}
printf ("Count is the value of sum of the two integers %d\n",cnt);
if ((cnt > 9) && (cnt != 11) && (cnt != 22))
{
continue;
}
else
{
res += cnt;
sub_sum=0;
break;
}
}
}
}
printf("The sum of the words are %d\n",res);
// The code is incomplete i have to again do a sum of res if that
turns to be a double digit and then check if // the final result will
be <9 or == 11 or == 22 and if this is fine then pass it to switch
case to get me the corresponding characteristics of the name.
}
The question to the group is on the atoi function.
I am converting the integer to string and so that
-> i can parse the string,
-> convert it back to integer and
-> sum it up.
( For e.g. in the above code i have sub_sum as 14)
1. i am checking for the condition is true then i am converting the
integer to string using sprintf. This works fine. The string is
declared as an character array. In this program char str[100];
2. Then using for loop to check for end of line
now str[0] = 1 and later str[1] = 4
now i use atoi to convert the string to integer so that i can
perform the sum.
Can someone please explain me what i am doing wrong. atoi needs the
string and i am passing a character so i tried passing the charecter
to a tmp[] and passing that to atoi function but doesnt solve my
purpose.
Any help would be much appreciated.