Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I cant do change string to int.

Reply
Thread Tools

I cant do change string to int.

 
 
emre esirik(hacettepe computer science and engineering)
Guest
Posts: n/a
 
      11-11-2007
char poli[50];
int p;
scanf("%s", poli);
if(poli[i]=='X' && poli[i+1]=='^')
{
toplam=i+3;
p=poli[i+2];

p is a ASCII code of integer but I need integer
for example if poli[i+2]='2' it puts p 50 (ascii code of '2')
but I need p=2 integer form, how can I do, please help

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-11-2007
In article <(E-Mail Removed). com>, emre
esirik(hacettepe computer science and engineering)
<(E-Mail Removed)> wrote on Sunday 11 Nov 2007 9:16 pm:

> char poli[50];
> int p;
> scanf("%s", poli);
> if(poli[i]=='X' && poli[i+1]=='^')
> {
> toplam=i+3;
> p=poli[i+2];
>
> p is a ASCII code of integer but I need integer
> for example if poli[i+2]='2' it puts p 50 (ascii code of '2')
> but I need p=2 integer form, how can I do, please help


Do:

p = poli[i+2] - '0';

This only works for '0'... '9'.

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      11-11-2007
emre esirik(hacettepe computer science and engineering) wrote:

> char poli[50];
> int p;
> scanf("%s", poli);
> if(poli[i]=='X' && poli[i+1]=='^')
> {
> toplam=i+3;
> p=poli[i+2];


/Please/ don't offer us random fragments of code; offer us
complete (but small) compilable (unless of course your problem
is a compilation problem) programs. For example, the fragment
above doesn't have a declaration for `i` in it: what /else/
has been missed out?

Are you /sure/ you want to be using `scanf` here, rather than
`fgets`?

> p is a ASCII code of integer but I need integer
> for example if poli[i+2]='2' it puts p 50 (ascii code of '2')
> but I need p=2 integer form, how can I do, please help


To convert a digit character into the corresponding value, you
simply subtract `'0'`, eg

int seven = '7' - '0';

C specifies that the digit characters are in order without gaps
(whereas it does /not/ do this for letters).

--
Non-Digital Hedgehog
"The path to the web becomes deeper and wider" - October Project

 
Reply With Quote
 
emre esirik(hacettepe computer science and engineering)
Guest
Posts: n/a
 
      11-11-2007
#include <stdio.h>
#include<stdlib.h>
int main()
{
char poli[50],tanpon[6]={'\0'};
int i,d,l,toplam,polinom[6];
int p;
int z=0;
scanf("%s", poli);
for(i=0 ; i<50 ; i++)
{
if(poli[i]=='X' && poli[i+1]=='^')
{
toplam=i+3;
p = poli[i+2] - '0';
if(z==0)
{
for(d=0 ; d<i ; d++)
{
tanpon[d]=poli[d];
}
polinom[p]=atoi(tanpon);
}else {
tanpon[6]='\0';
for(l=toplam+1; l<i ; l++)
{
printf("toplam%d,l---->%d",toplam,l);
tanpon[l]=poli[l];
}
polinom[p]=atoi(tanpon);
}
z=1;
}
}
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom[i]);
}
return 0;
}

this is my code, I want to enter like this 3X^1+32X^4+12X^0 but its
only do if part which if z==0 but why it doesnt do 'else' part????

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-11-2007
In article <(E-Mail Removed). com>, emre
esirik(hacettepe computer science and engineering)
<(E-Mail Removed)> wrote on Sunday 11 Nov 2007 11:04 pm:

Please quote the relevant portions of the article to which you are
replying.

> #include <stdio.h>
> #include<stdlib.h>
> int main()
> {
> char poli[50],tanpon[6]={'\0'};
> int i,d,l,toplam,polinom[6];


Good indentation and judicious use of whitespace can enhance the
readability of your code for others.

> int p;
> int z=0;
> scanf("%s", poli);


scanf() with the 's' specifier behaves much like gets() and is a
dangerous way to get input. If the input consists of over 50
characters, scanf() exceed the bounds of 'poli' and overwrite memory it
does not own, invoking undefined behaviour.

To read a line of input use fgets().

fgets(poli, sizeof poli, stdin);

Note that 'sizeof poli' only works as expected within the function
where 'poli' is defined. Anywhere else you must explicitly pass the
size of 'poli' to code that needs it.

> for(i=0 ; i<50 ; i++)
> {
> if(poli[i]=='X' && poli[i+1]=='^')
> {
> toplam=i+3;
> p = poli[i+2] - '0';
> if(z==0)
> {
> for(d=0 ; d<i ; d++)
> {
> tanpon[d]=poli[d];
> }
> polinom[p]=atoi(tanpon);
> }else {
> tanpon[6]='\0';
> for(l=toplam+1; l<i ; l++)
> {
> printf("toplam%d,l---->%d",toplam,l);
> tanpon[l]=poli[l];
> }
> polinom[p]=atoi(tanpon);
> }
> z=1;
> }
> }
> for(i=0 ; i<6 ; i++) {
> printf(" %d ", polinom[i]);
> }
> return 0;
> }


Without indentation your code is illegible. I'm sure there must be a
much simpler way for what you are trying to do, if you can spell it out
clearly.

> this is my code, I want to enter like this 3X^1+32X^4+12X^0 but its
> only do if part which if z==0 but why it doesnt do 'else' part????


Because 'z' is always zero. You initialise it to zero but no other code
changes it, so the if part of the if/else statement is always executed.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-11-2007
santosh <(E-Mail Removed)> writes:

> In article <(E-Mail Removed). com>, emre
> esirik(hacettepe computer science and engineering)
> <(E-Mail Removed)> wrote on Sunday 11 Nov 2007 11:04 pm:
>> int z=0;

<snip>
>> z=1;

<snip>
>> this is my code, I want to enter like this 3X^1+32X^4+12X^0 but its
>> only do if part which if z==0 but why it doesnt do 'else' part????

>
> Because 'z' is always zero. You initialise it to zero but no other code
> changes it, so the if part of the if/else statement is always
> executed.


there is a 'z=1;' in there. Proof, if it were needed, that good names
and good layout are important. The code is uninviting so I have not
had a look at it.

--
Ben.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-11-2007
In article <(E-Mail Removed)>, Ben Bacarisse
<(E-Mail Removed)> wrote on Sunday 11 Nov 2007 11:40 pm:

> santosh <(E-Mail Removed)> writes:
>
>> In article <(E-Mail Removed). com>,
>> emre esirik(hacettepe computer science and engineering)
>> <(E-Mail Removed)> wrote on Sunday 11 Nov 2007 11:04 pm:
>>> int z=0;

> <snip>
>>> z=1;

> <snip>
>>> this is my code, I want to enter like this 3X^1+32X^4+12X^0 but
>>> its
>>> only do if part which if z==0 but why it doesnt do 'else' part????

>>
>> Because 'z' is always zero. You initialise it to zero but no other
>> code changes it, so the if part of the if/else statement is always
>> executed.

>
> there is a 'z=1;' in there. Proof, if it were needed, that good names
> and good layout are important. The code is uninviting so I have not
> had a look at it.


Ah yes. Good spot. However it comes _after_ the if/else construct so
it's of no consequence to it.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-11-2007
santosh wrote:
> In article <(E-Mail Removed). com>, emre
> esirik(hacettepe computer science and engineering)

....
[Source code, free of indentation]

> Without indentation your code is illegible. I'm sure there must be a
> much simpler way for what you are trying to do, if you can spell it out
> clearly.


His code displays fully indented in my newsreader (Firefox). I'll insert
a copy of it here, in the hope that this copy displays better in your
newsreader than the original did:

> #include <stdio.h>
> #include<stdlib.h>
> int main()
> {
> char poli[50],tanpon[6]={'\0'};
> int i,d,l,toplam,polinom[6];
> int p;
> int z=0;
> scanf("%s", poli);
> for(i=0 ; i<50 ; i++)
> {
> if(poli[i]=='X' && poli[i+1]=='^')
> {
> toplam=i+3;
> p = poli[i+2] - '0';
> if(z==0)
> {
> for(d=0 ; d<i ; d++)
> {
> tanpon[d]=poli[d];
> }
> polinom[p]=atoi(tanpon);
> }else {
> tanpon[6]='\0';
> for(l=toplam+1; l<i ; l++)
> {
> printf("toplam%d,l---->%d",toplam,l);
> tanpon[l]=poli[l];
> }
> polinom[p]=atoi(tanpon);
> }


Here's where 'z' gets set:
> z=1;
> }
> }
> for(i=0 ; i<6 ; i++) {
> printf(" %d ", polinom[i]);
> }
> return 0;
> }


>> this is my code, I want to enter like this 3X^1+32X^4+12X^0 but its
>> only do if part which if z==0 but why it doesnt do 'else' part????

>
> Because 'z' is always zero. You initialise it to zero but no other code
> changes it, so the if part of the if/else statement is always executed.


I see a z=1; statement. Whatever the problem is with his code, that
isn't it.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-11-2007
santosh wrote, On 11/11/07 18:02:
> In article <(E-Mail Removed). com>, emre
> esirik(hacettepe computer science and engineering)
> <(E-Mail Removed)> wrote on Sunday 11 Nov 2007 11:04 pm:
>
> Please quote the relevant portions of the article to which you are
> replying.
>
>> #include <stdio.h>
>> #include<stdlib.h>
>> int main()
>> {
>> char poli[50],tanpon[6]={'\0'};
>> int i,d,l,toplam,polinom[6];

>
> Good indentation and judicious use of whitespace can enhance the
> readability of your code for others.
>
>> int p;
>> int z=0;
>> scanf("%s", poli);

>
> scanf() with the 's' specifier behaves much like gets() and is a
> dangerous way to get input. If the input consists of over 50
> characters, scanf() exceed the bounds of 'poli' and overwrite memory it
> does not own, invoking undefined behaviour.


You mean over 49 characters, don't forget that scanf will write a /0 to
terminate the string.

> To read a line of input use fgets().
>
> fgets(poli, sizeof poli, stdin);
>
> Note that 'sizeof poli' only works as expected within the function
> where 'poli' is defined. Anywhere else you must explicitly pass the
> size of 'poli' to code that needs it.


<snip>

Note also that scanf and fgets return values which should be checked,
otherwise you won't know if you actually received *any* input. Also you
have to deal with the newline and how you want to handle over-long inputs.
--
Flash Gordon
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-11-2007
On Sun, 11 Nov 2007 23:32:24 +0530, in comp.lang.c , santosh
<(E-Mail Removed)> wrote:

>> for(i=0 ; i<6 ; i++) {
>> printf(" %d ", polinom[i]);
>> }
>> return 0;
>> }

>
>Without indentation your code is illegible.


FYI, the code is indented just fine when I see it in Agent. Suspect
your news server or client is mangling the tabs.

> }
> for(i=0 ; i<6 ; i++) {
> printf(" %d ", polinom[i]);
> }
> return 0;
>}

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WIERD: Cant change webconfig in 2.0? VB Programmer ASP .Net 3 01-29-2008 06:42 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
man i cant belive i cant get help please unclejesse01 DVD Video 2 04-14-2005 03:15 PM
Re cant change background color using external style sheet Simon HTML 2 02-25-2005 06:30 PM



Advertisments