a question in C++

1student

Member
May 30, 2004
42
0
0
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.
 

PottedMeat

Lifer
Apr 17, 2002
12,365
475
126
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
 

imported_obsidian

Senior member
May 4, 2004
438
0
0
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.
 

Odoacer

Senior member
Jun 30, 2001
809
0
0
Stringstream is the proper way. atoi() will work, but is not standard. Your compiler may or may not support it.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
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.
 

1student

Member
May 30, 2004
42
0
0
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!
 

TheLonelyPhoenix

Diamond Member
Feb 15, 2004
5,594
1
0
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.
 

Chebago

Senior member
Apr 10, 2004
575
0
0
marcyes.com
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...
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
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
 

1student

Member
May 30, 2004
42
0
0
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!
 

onelin

Senior member
Dec 11, 2001
874
0
0
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...
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |