Explanation:

suppose an expression -23+5-9/(5*2) is entered then i want output as

Numbers: -23 +5 +9 +5 +2
Operators: + - / * ()

Is it possible?

Recommended Answers

All 12 Replies

>>is it possible?

Yes.

>>is it possible?

Yes.

Any Advice/Idea!

Depends on if you want to build a calculator to do this or simply use c++ as the calculator and produce an answer for you. A calculator is a great project for learning. I'm currently working on a small text based (for now anyway) card game and have learned much already. Just the writing of code sometimes gets you more familiarised with a language. To start make a few variables, and then preform the mathmatics on them, and have it print the results to the screen. If you don't know how try starting here tutorial.
Is a great tutorial I learned a great deal from it. They also have great refrences on libraries.

Depends on if you want to build a calculator to do this or simply use c++ as the calculator and produce an answer for you. A calculator is a great project for learning. I'm currently working on a small text based (for now anyway) card game and have learned much already. Just the writing of code sometimes gets you more familiarised with a language. To start make a few variables, and then preform the mathmatics on them, and have it print the results to the screen. If you don't know how try starting here tutorial.
Is a great tutorial I learned a great deal from it. They also have great refrences on libraries.

Normally C++ accept only one data type at a time. I am curious is there any "work around" to input several data types once and yet getting the work done!

Of course there is. cin will accept all data types in one statement.

Maybe you need to explain in detail what you are trying to accomplish in the end. Based on your question as posted, it's a trivial procedure. Just look at one character at a time and combine them into separate strings based on operator/digits.

Any Advice/Idea!

If you expect the answer you want then you have to learn how to ask the correct question. I answered the question you asked, nothing more :)

Of course there is. cin will accept all data types in one statement.

Maybe you need to explain in detail what you are trying to accomplish in the end. Based on your question as posted, it's a trivial procedure. Just look at one character at a time and combine them into separate strings based on operator/digits.

cin will accept all data types in one statement but how it will distinguish between int and operators? It may be trivial to point out operators but I think operators can also be treated as char and carrying out its respective operations! Just my thinking, I don't know if it is even possible!

You need to get input as std::string then pars it to extract the integers and operators. Since they can be entered in any order there really is no other way to do it.

commented: exactly, didn't know this to be true with c++ but great info +3

You need to get input as std::string then pars it to extract the integers and operators

pars it to extract,How? A code might help more!:-/

Or this havn't tested it as I am still going through it myself but I assume since it was proposed it should work. string::find

in a loop check each character of the string to see if it's a digit or an operator. If its a digit then add to a vector of integers, otherwise if it's an operator add it to a std::string of operators.

Since this is probably homework, I'm not going to post the complete solution. You have to write it yourself and post what you have done.

Solved!
I finally got my desired output! Thanks anyways guys.
I will be happy to post the code, anytime!

BTW I didn't use "std::string" or "vector" or "parse" as I have no idea what these are!
And this is no homework, I just wanted and I got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.