?
Solved

C casting problem

Posted on 2003-03-15
10
Medium Priority
?
193 Views
Last Modified: 2010-04-15
Here is my code #include <stdio.h>
void main(){
 int len=0,j=0,i=0;int dig[20];
int y=0;
 char expression[20];
 printf("Enter a postfix _expression");
 scanf("%s",expression);
 printf("How many digits are there in your _expression ? ");
 scanf("%d",&len);
 for(i=0;i<len;i++)

{
  if(expression[i]=='+'){
   dig[i-2] += dig[i-1];
   
  }
  else
      if(expression[i]=='-'){
        dig[i-2] -= dig[i-1];
        }
 else
      if(expression[i]=='*'){
        dig[i-2] *= dig[i-1];
     
     }
 
  else
      if(expression[i]=='/'){
        dig[i-2] /= dig[i-1];
     }

  else{
  dig[i] = (int)expression[i];

printf("%d\n",dig[i]);}
 
 }/*end for*/

 printf("Answer = %d\n",dig[0]);

}




The out put is not in integers
C:\EJL\myC>indi
Enter a postfix _expression22+
How many digits are there in your _expression ? 3
50
50
Answer = 100


and also * it is not working .I can't figure out a reason .Please help quickly.
0
Comment
Question by:purtutu
  • 3
  • 2
  • 2
  • +2
10 Comments
 

Accepted Solution

by:
plushey earned 60 total points
ID: 8143069
The reason the code is not working is because you are using the ascii value for the character representation of the digits, the coversion to an integer can't be achieved by using a cast.

Before converting the character to an integer you should really check that it is in the range '0' to '9' using isdigit();

Then the simplest way to covert your character to an integer is to code:
dig[i] = (int)expression[i] - 48;

This is not totally portable, but it should work for you
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8143076
Use this
  dig[i] = (int)expression[i]-'0';

'0' = character ZERO

0
 
LVL 20

Expert Comment

by:jmcg
ID: 8143088
I think you may have gotten lost in your cascading 'if'. This is a situation where a 'switch' statement might be a better choice.

Also, the expression

dig[i] = (int)expression[i];

does not perform a conversion from ASCII numeral to that numeral's integer value. One common way to do this, when you know you have an character value that represents a digit, is to subtract the character value '0' from it:

dig[i] = expression[i]-'0';

Your program could certainly avoid having the user lie to it, as your example shows, about how many digits are input by using the 'isdigit' predicate itself.

Since this is pretty clearly a homework problem, my colleagues and I will probably refrain from giving you a complete working program. You're welcome to submit a new revision, though, and any questions you have about what's not working.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Kocil
ID: 8143152
LOL, 3 same answers :)
0
 

Expert Comment

by:plushey
ID: 8143735
:-)
What else would you expect from 3 "experts"? :-)

Not sure that any of our answers will be accepted though, judging on past questions submitted, we will probably get a "thank you, that solved my problem" but the question will remain open :-(
0
 
LVL 1

Expert Comment

by:Hermetic
ID: 8144114
Why wouldn't you use strtol()?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8144188
strtol() works on null-terminated strings, not so well on isolated characters.
0
 

Author Comment

by:purtutu
ID: 8144731
Thanks a lot for all of you.Now my code is working properly.
0
 

Author Comment

by:purtutu
ID: 8144739
Thanks a lot
0
 

Expert Comment

by:plushey
ID: 8144989
Thank you purtutu,

Perhaps you could find some time to review some of the other questions you have asked but have not accepted an answer from an expert. I feel that many of your questions have been answered well enough to at least give one of the experts some credit for it. I've nothing to gain here, it's just worth pointing out that some people have given their time to help and an acknowledgement of that help will go some way to ensure that you continue to receive help with future questions.

Good luck.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question