View previous topic :: View next topic |
Author |
Message |
NoMercy Master Cheater Reputation: 1
Joined: 09 Feb 2009 Posts: 289
|
Posted: Sat Apr 17, 2010 1:51 am Post subject: char with +,- to int |
|
|
Heey
Ive beent trying almost everything i can think off
but if uve got:
how can i make a int x = y? I know how to do the numbers, but with the + i keep failing.
I want to know the answer of x, but since u can do + in a char, ive to convert to int, but it goes wrong
tried:
atoi
strstream
using lexical_cast:
|
|
Back to top |
|
|
Flyte Peanuts!!!! Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
Back to top |
|
|
Slugsnack Grandmaster Cheater Supreme Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Apr 17, 2010 5:31 am Post subject: |
|
|
can you give an example of what you're trying to do ?
are you trying to convert the int on the right to a string and then assigning it to the char array ?
|
|
Back to top |
|
|
tombana Master Cheater Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Sat Apr 17, 2010 5:36 am Post subject: |
|
|
Slugsnack wrote: | can you give an example of what you're trying to do ?
are you trying to convert the int on the right to a string and then assigning it to the char array ? |
I think he wants to parse a string like "2 + 5 * 2" and get the resulting number in an int.
|
|
Back to top |
|
|
Slugsnack Grandmaster Cheater Supreme Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Apr 17, 2010 7:24 am Post subject: |
|
|
can use regex or pattern matching i guess
|
|
Back to top |
|
|
NoMercy Master Cheater Reputation: 1
Joined: 09 Feb 2009 Posts: 289
|
Posted: Sat Apr 17, 2010 9:55 am Post subject: |
|
|
Im trying to make it that ( in a calculator) people can typ, 2+2+2+(2 * (3/4)) and there is a answer,
but for the EDIT box where ppl can typ the numbers in i use CHAR, so if some1 typs 2+3*5, ive to convert it to int
|
|
Back to top |
|
|
Flyte Peanuts!!!! Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sat Apr 17, 2010 2:28 pm Post subject: |
|
|
NoMercy wrote: | Im trying to make it that ( in a calculator) people can typ, 2+2+2+(2 * (3/4)) and there is a answer,
but for the EDIT box where ppl can typ the numbers in i use CHAR, so if some1 typs 2+3*5, ive to convert it to int |
If it wasn't obvious from my first post, you have to use the shunting yard algorithm to parse the string into reverse polish notation onto a stack, and then use a reverse polish notation calculator to solve it.
This is the best way. You can't just 'cast to an int' and have it magically solve the equation for you (well, you could, but you'd have to use operator overloading and what I said in the first part ).
|
|
Back to top |
|
|
NoMercy Master Cheater Reputation: 1
Joined: 09 Feb 2009 Posts: 289
|
Posted: Mon Apr 19, 2010 12:33 am Post subject: |
|
|
thanks flyte,
It took me a few hours to write all those functions, but i think ive got it working now
|
|
Back to top |
|
|
|