tags:

views:

172

answers:

4

Hello,

I have a problem in writing a program with C++. I have been studying C# and Java but C++ is a way different to me, so I need your help.

My task is to make program which: reads an input from a text box than returns the nubers from that input which has two digits and their sum is 9.

For example:

Input: 12 231 81 53 522 11 63

Output: 81 63

I need it a ssimple as possible so I could understand it. Thank you in advance.

A: 

Read each character individually, one by one.

If it's a digit (>= '0' && <= '9'), add one to the "read digits" counter and convert it to a number. If not, reset the "read digits" counter and continue to parse until you hit the end.

If you reset the counter when it has exactly two read digits, check if those two numbers add up to 9 and print them out in case they do.

Since you never need to store more than two numbers, you can have a static array that can hold these two digits.

pbos
Kylotan
A: 

Read this Input/Output with files

David Allan Finch
+1  A: 

As you know C# and Java, how would you solve this problem in C# or Java? Start with that, and then you can modify that solution to fit with C++, the algorithm should be the same, and the syntax is more similar than you may think.

For instance, start with the following and implement the OutputResult function:

class Test
{
    static void OutputResult(String contentsToParse)
    {
        // TODO: Implementation here.
    }

    static void Main()
    {
        String contentsOfTextBox = "12 231 81 53 522 11 63";
        OutputResult(contentsOfTextBox);
    }
}
dalle
+1  A: 

This looks like a question about C++ operators.

  • To read the integers from a string you could use the istringstream input operator: >>
  • To test if an integer has two digits you could use the built-in greater than > and less than < operators with the boolean and && operator.
  • To separate the two digits you could use the built-in integer division / and modulus % operators.
  • To check the sum of the digits you could use the built-in addition + and equality == operators.
richj
Note: A number 'n' is two digits if the following is true: 10 <= n <= 99
Martin York