• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

a question in C++

1student

Member
Hi,

I have 2 urgent question:

ex.: I have the chars: '1' '+' '2'.

1. How can I convert the chars ('1', '2') into int (1, 2)?

2. How can I convert the char ('+', '-' etx.. in another case) into operator?

Lot's of 10xs.
 
uh
i think for
1. take the character and subtract hex30 from it to convert to a number
2. use if/else or switches or case when looking at the string like "2+3"
or seperate the numbers out into different variables
 
Yea, this is correct. Subtract decimal 48 or hex 30 from the integer chars to convert to an integer. This is because chars are really integers representing an ascii code, and ascii 48 is '0'.

For the operators you will want to use a select case statement most likely since there are only 4 possibilities.
 
Stringstream is the proper way. atoi() will work, but is not standard. Your compiler may or may not support it.
 
Originally posted by: Odoacer
Stringstream is the proper way. atoi() will work, but is not standard. Your compiler may or may not support it.

It's standard. It's part of C89, and C++ guarantees to have everything from C89's standard library. C89 also has atol(), but afaik that's it.
 
First, thank u all!

second-
Subtract decimal 48 or hex 30 from the integer chars to convert to an integer
I think it's the most comfortable way to convert the chars (and that the way I did it in my HW), but it works just on numbers in the range 0-9.. is there a similiar way for numbers out that range?

Or just use atoi() to convert to integers.
I tried it & had a problem when I compiled the program: Call to undefined function 'atoi' in function main()
What can be the cause?
Oh, now I see -- Stringstream is the proper way. atoi() will work, but is not standard. Your compiler may or may not support it.

Originally posted by: BingBongWongFooey
Or the "real" c++ way: streams.

int one;
std::stringstream("1") >> one;
sorry, I didn't understand it..

For the operators you will want to use a select case statement most likely since there are only 4 possibilities.
That's what I'll do, I think. Thanks!
 
I'm rather fond of the "Subtract 48" cop-out. 🙂

But proper style in C++ would favor stringstreams, as they are a much more powerful method of handling this situation.
 
atoi() requires that you #define<stdlib.h> in order to use it I think, if you are in linux you can 'man atoi' to find out, also 'man ascii' if you wanna know why you need to subtract that specific amount, as for the string stream, I had this program that would work for about the first 200 iterations and then it would mess up, really irritated me and I never did figure it out, I think I was trying to convert an int to a string for a filename though...
 
instead of #define, don't you mean #include?

also, the OP also asked about numbers other than 0-9. You can subtract 48 from each character and assemble them into one whole number by multiplying by 10 before adding the next digit.

or -- use stringstream 🙂
 
I'm in windows.

I tried what u said (#define<stdlib.h> ), but the program doesn't understand it...
The message I get:
Define directive needs an identifier

Originally posted by: oog
instead of #define, don't you mean #include?
It sounds better.😉
and now the program I work on doesn't complain about the define..

also, the OP also asked about numbers other than 0-9. You can subtract 48 from each character and assemble them into one whole number by multiplying by 10 before adding the next digit
That's an idea, I'll try it!
 
aww c'mon, no one was lazy enough to subtract the character 0? 🙂 '9' - '0' results in 9. 🙂 but I agree on stringstream, especially if it will do the math for you on multiple digit stuff...
 
Back
Top