PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > with atoi function for a numero program.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
with atoi function for a numero program.

Réponse
 
LinkBack Outils de la discussion
Vieux 30/05/2008, 18h18   #1
Ram
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut with atoi function for a numero program.

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 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 would be much appreciated.






  Réponse avec citation
Vieux 30/05/2008, 18h48   #2
Glenn Hutchings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with atoi function for a numero program.

Ram <sriram.n83@gmail.com> writes:

> char str[100],tmp[1];


<snip>

> for ( i=0; str[i]!='\0'; i++)
> {
>
> tmp[0]=str[i];
> c=atoi(tmp); // I need 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
  Réponse avec citation
Vieux 30/05/2008, 20h34   #3
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with atoi function for a numero program.

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>

  Réponse avec citation
Vieux 30/05/2008, 21h42   #4
Ram
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with atoi function for a numero program.

On May 30, 1:48 pm, Glenn Hutchings <zond...@googlemail.com> wrote:
> Ram <sriram....@gmail.com> writes:
> > char str[100],tmp[1];

>
> <snip>
>
> > for ( i=0; str[i]!='\0'; i++)
> > {

>
> > tmp[0]=str[i];
> > c=atoi(tmp); // I need 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.
  Réponse avec citation
Vieux 30/05/2008, 23h24   #5
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with atoi function for a numero program.

Ram <sriram.n83@gmail.com> writes:

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 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 ful 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18183 seconds with 13 queries