473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

I cant do change string to int.

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

Nov 11 '07 #1
13 1461
In article <11*********************@o80g2000hse.googlegroups. com>, emre
esirik(hacettepe computer science and engineering)
<em********@gmail.comwrote 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'.

Nov 11 '07 #2
#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????

Nov 11 '07 #3
In article <11**********************@19g2000hsx.googlegroups. com>, emre
esirik(hacettepe computer science and engineering)
<em********@gmail.comwrote 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.

Nov 11 '07 #4
santosh <sa*********@gmail.comwrites:
In article <11**********************@19g2000hsx.googlegroups. com>, emre
esirik(hacettepe computer science and engineering)
<em********@gmail.comwrote 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.
Nov 11 '07 #5
In article <87************@bsb.me.uk>, Ben Bacarisse
<be********@bsb.me.ukwrote on Sunday 11 Nov 2007 11:40 pm:
santosh <sa*********@gmail.comwrites:
>In article <11**********************@19g2000hsx.googlegroups. com>,
emre esirik(hacettepe computer science and engineering)
<em********@gmail.comwrote 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.

Nov 11 '07 #6
santosh wrote:
In article <11**********************@19g2000hsx.googlegroups. 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.

Nov 11 '07 #7
santosh wrote, On 11/11/07 18:02:
In article <11**********************@19g2000hsx.googlegroups. com>, emre
esirik(hacettepe computer science and engineering)
<em********@gmail.comwrote 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
Nov 11 '07 #8
On Sun, 11 Nov 2007 23:32:24 +0530, in comp.lang.c , santosh
<sa*********@gmail.comwrote:
>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
Nov 11 '07 #9
James Kuyper wrote:
santosh wrote:
In article <11**********************@19g2000hsx.googlegroups. 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:
He used tabs for indentation. Tabs aren't handled consistently.

Brian

Nov 11 '07 #10
Mark McIntyre wrote, On 11/11/07 23:49:
On Sun, 11 Nov 2007 23:32:24 +0530, in comp.lang.c , santosh
<sa*********@gmail.comwrote:
>>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.
It could easily be a news server in the path, since I know my client
handles tabs correctly when they reach it but I did not see them either.

To the OP, this is one of the reasons why you should not use tabs when
posting to news groups.
> }
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom[i]);
}
return 0;
}
See, tabs still there. I have also seen tabs reach me correctly on
original posts in the past, not just in quoted material.
--
Flash Gordon
Nov 12 '07 #11
santosh wrote:
In article <87************@bsb.me.uk>, Ben Bacarisse
<be********@bsb.me.ukwrote on Sunday 11 Nov 2007 11:40 pm:
....
>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.
It's defined outside of a loop, and both set and tested inside the loop.
If it's set in the first pass through the loop, the else clause should
be triggered on the following pass. I still haven't bothered looking
closely at this code, so I'm not sure what's wrong with it, but the fact
that z is first set to a non-zero after the first time it's value is
tested isn't in itself sufficient explanation.
Nov 12 '07 #12
emre esirik(hacettepe computer science and engineering) wrote:
#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;
I don't know what these variables are supposed to represent, 'z' is a
particularly meaningless name for one...
scanf("%s", poli);
bad move - you don't check the return value of scanf(), and don't force
the length to fit into 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++)
l can never be less than i.
{
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
No...
but why it doesnt do 'else' part????
It does, but never enters the body of the loop.

Your code is a mess and the wrong approach. Find a text book and look at
parsing, perhaps with a focus on Finite State machines.

Nov 12 '07 #13
On Sun, 11 Nov 2007 09:34:05 -0800, "emre esirik(hacettepe computer
science and engineering)" <em********@gmail.comwrote:
>#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]=='^')
What happens if the input does not contain 'X' followed by '^'?
> {
toplam=i+3;
p = poli[i+2] - '0';
if(z==0)
{
for(d=0 ; d<i ; d++)
{
tanpon[d]=poli[d];
What happens if the X^ is beyond the sixth character?
> }
polinom[p]=atoi(tanpon);
What happens if the '^' is followed by a digit greater than 5?
> }else {
tanpon[6]='\0';
tanpon[6] does not exist. This invokes undefined behavior.
> for(l=toplam+1; l<i ; l++)
toplam is set to i+3. This for loop is executed zero iterations.
> {
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]);
There is no guarantee that all six element of poinom have been
assigned values.
> }
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????

Remove del for email
Nov 13 '07 #14

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

Similar topics

2
by: Tiernan | last post by:
Hey everybody. I'm verry new to PHP and MYSQL and have been working on a form that when it is submitted stores the information into a mysql database. The main problem is that i'm trying to finish...
7
by: Lasse Edsvik | last post by:
Hello I made a .udl file and tested its connectionstring and it worked, when i put the connectionstring in the code it says that it is invalid? what have i done wrong? <%@ Page Language="vb"...
16
by: Mike Fellows | last post by:
when i load my windows form i populate a combobox i use the code below Dim conn As New System.Data.SqlClient.SqlConnection(strConn) Dim sql As String = "AllLenders" Dim da As New...
0
by: Seth | last post by:
For some reason my service works fine except that it will create the file in my c drive, but will not write to the file. Sorry if this is a duplicate post, i have found some that ask the same...
4
by: zack | last post by:
Any help with this would be greatly appreciated, as cannot work out how to resolve. I have a report called "3_Strikes". In its 'On open' event is command to also open a criteria form popup form...
2
by: g35rider | last post by:
Hi, I have the following code that is giving this error, I cant simplify the code, I was just testing some theory for something we are doing and was getting an issue here. Please someone point out...
3
by: arun.hallan | last post by:
Hi, I'm having problems with authentication. I have a set of users that are allowed to use a webpage. They are in domain A. My goal is to get the username of these users and then check them...
2
by: moondaddy | last post by:
I had to repost this because I had to update and change my msdn alias. I will re-ask the question and clarify a few things that were not clear before. This code is all executed on my dev...
6
by: Taras_96 | last post by:
Hi all, Jesse Liberty writes: "Note that the String class provides the operator+. The designer of the Employee class has blocked access to the operator+ being called on Employee objects by...
2
by: Mucahit ikiz | last post by:
I cant make a full dynamic query in LINQ I have 2 situation methods (only_exp_query, only_tbl_query) those are working. .... using System.Linq.Dynamic; using System.Data.Linq; .... string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.