473,473 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with atoi function for a numero program.

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[i]!='\0';i++) // loop to check for end of string
{
if(name[i] != ' ') // condition for space
{
ch=name[i];
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[i]!='\0'; i++)
{

tmp[0]=str[i];
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.


Jun 27 '08 #1
4 2491
Ram <sr********@gmail.comwrites:
char str[100],tmp[1];
<snip>
for ( i=0; str[i]!='\0'; i++)
{

tmp[0]=str[i];
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
Jun 27 '08 #2
Glenn Hutchings wrote:

<snip>
Oh, and the // style of comments are C++, not standard C,
Actually they have been standardised into C with the 1999 standard.

<snip>

Jun 27 '08 #3
Ram
On May 30, 1:48 pm, Glenn Hutchings <zond...@googlemail.comwrote:
Ram <sriram....@gmail.comwrites:
char str[100],tmp[1];

<snip>
for ( i=0; str[i]!='\0'; i++)
{
tmp[0]=str[i];
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.
Jun 27 '08 #4
Ram <sr********@gmail.comwrites:

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[i];
count=((ch-'A')%9)+1;
Or do it here.
sub_sum=sub_sum+count;
I'd write: sub_sum += (toupper(name[i]) - 'A') % 9 + 1;
for ( i=0; str[i]!='\0'; i++)
{

tmp[0]=str[i];
c=atoi(tmp); // I need help here
cnt=cnt+c;
}
cnt += str[i] - '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.

<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)
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);
}

<snip>
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[i] - '0';

--
Ben.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
15
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
15
by: Anonymousgoogledeja | last post by:
Hi all, since the function atof, atoi, _atoi64, atol returned value are Return Values Each function returns the double, int, __int64 or long value produced by interpreting the input...
3
by: lemat | last post by:
Hi, I use this function in my website, it works perfectly in Firefox. Whan I click on the image, it changes the border and selects the button asociated (it's a radiobutton). the color of the...
7
by: khajeddin | last post by:
(in C programing) who knows the name of some function which can be used for printing tables or for printing some colorful outputs? i want to print some data in red color and also want to print and...
2
by: useasdf4444 | last post by:
I have the following program: #include <stdio.h> #include <string.h> #include <stdlib.h> void main () { char command; char *com,*pos,*stack1,*stack2; int boxes,boxtable,i,j,k,st1,st2; ...
13
by: ptq2238 | last post by:
Hi, I have written this code to help me learn C but I'm not sure why gcc - Wall is giving me an error when I compile Basically I want to read in a character then a number and then manipulate...
1
by: martinmontielr | last post by:
How can i do a program that makes the numeric center.. this is.... 1+2+3+4+5=15 ............ 6= numeric center.... ........7+8= 15..... of this way 1,2,3,4,5,(6),7,8... the next is 35...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.