Help with atoi function for a numero program.


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.
 
Ad

Advertisements

G

Glenn Hutchings

Ram said:
char str[100],tmp[1];

for ( i=0; str!='\0'; i++)
{

tmp[0]=str;
c=atoi(tmp); // I need help here
cnt=cnt+c;
}


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.

Strings in C must be terminated by a zero ('\0') character to be usable
correctly in C library functions. Your tmp array doesn't have this. If
you define it as tmp[2], and add the line

tmp[1] = '\0';

after the tmp[0] assignment, it should work.

There are much better ways to do what you're trying to do in various places
in your program: check out the sscanf() library function. It scans the
contents of a string and places the results into one or more variables
directly.

Oh, and the // style of comments are C++, not standard C, although most
compilers these days will accept them anyway. The C style of comment is
like /* this */.

Glenn
 
S

santosh

Glenn Hutchings wrote:

Oh, and the // style of comments are C++, not standard C,

Actually they have been standardised into C with the 1999 standard.

<snip>
 
R

Ram

Ram said:
char str[100],tmp[1];

for ( i=0; str!='\0'; i++)
{

tmp[0]=str;
c=atoi(tmp); // I need help here
cnt=cnt+c;
}
<snip>



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.

Strings in C must be terminated by a zero ('\0') character to be usable
correctly in C library functions. Your tmp array doesn't have this. If
you define it as tmp[2], and add the line

tmp[1] = '\0';

after the tmp[0] assignment, it should work.

There are much better ways to do what you're trying to do in various places
in your program: check out the sscanf() library function. It scans the
contents of a string and places the results into one or more variables
directly.

Oh, and the // style of comments are C++, not standard C, although most
compilers these days will accept them anyway. The C style of comment is
like /* this */.

Glenn


Thanks glen i got it.
 
Ad

Advertisements

B

Ben Bacarisse

I'll just make a few remarks...
printf ("Enter your name (ONLY CAPITAL LETTERS) :- ");
fgets (name,BUFSIZE,stdin);

Why make the user do the work? Computers are good at this short of
thing. Either convert the letter after you read them...
ch=name;
count=((ch-'A')%9)+1;


Or do it here.
sub_sum=sub_sum+count;

I'd write: sub_sum += (toupper(name) - 'A') % 9 + 1;
for ( i=0; str!='\0'; i++)
{

tmp[0]=str;
c=atoi(tmp); // I need help here
cnt=cnt+c;
}


cnt += str - '0'; but see below...
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;
}

This is probably over complex. The pattern you have is:

while (C) {
/* some stuff */
if (C)
continue;
else {
/* a little more */
break;
}
}

If you get the loop body right, all you need is a while. No need to
repeat the test in the middle.
}
}
}
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.

}

General points: use a little more horizontal space. Breaking
the problem into helpful functions will make it much simpler.

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)

There really is not need. You can sum the digits of a number without
converting to characters and back:

int sum_digits(int num)
{
if (num < 0)
return sum_digits(-num);
else if (num == 0)
return 0;
else return num % 10 + sum_digits(num / 10);
}

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.

atoi is just the wrong function here. If you must convert to digits
using sprintf, then you convert each digit back to a number by
subtracting '0': cnt += str - '0';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top