In C, how do I use scanf to read an equation in the format Ax+By=C?
-
I want the program to extract A, B and C from the equation ONLY if the input is in this format. Also, spaces are allowed but not between the variables to the coefficients: 5x + 5y= 3 is fine 5 x + 5 y = 3 should return an error. I tried scanf("%dx+%dy=%d",&A,&B,&C) where A, B, C are predefined integers. It did some of the input checking but not all of it: for instance, it accepts the first digit whether it's followed by an "x" or not. Also the spaces are not handled properly. Thanks for your answers
-
Answer:
You cannot do that with scanf! It's much easier to do it with regular expressions. If you don't know whats a regular expression look here http://en.wikipedia.org/wiki/Regular_expression After learning that now you can use sscanf function (yes with double s) Example: int returned; returned = sscanf(string, "your reg ex", A, B, C); if you didn't understand it try searching for sscanf.
giladbr at Yahoo! Answers Visit the source
Related Q & A:
- How should I use Youtube API?Best solution by Stack Overflow
- How do I use "John et. al. 1990 " citation for more than two authors?Best solution by tex.stackexchange.com
- How Can I use .net dll in C program?Best solution by Stack Overflow
- Where do I find my Yahoo briefcase and how do I use it?Best solution by Yahoo! Answers
- How do I use chat when I am in a room?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.