A+B Problem

I attempted a A+B problem from an online judge and my solution works but the judgement I got was wrong answer. Can anyone explain why?

1
2
3
4
5
6
7
8
9
10
Description
For this problem you must calculate A + B, numbers given in the input.
Input specification
The only line of input contain two space separated integers A, B (0 <= A, B <= 10).
Output specification
The only line of output should contain one integer: the sum of A and B.
Sample input
1 2
Sample output
3


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <sstream>

int getint();

int main()
{
	int sum;
	sum = getint();
	std::cout << '\n' << "The Sum Is: " << sum << std::endl;

	return 0;
}

int getint()
{
	int a, b; std::string str;
	std::cout << "Please Enter Two Numbers Separted By Space" << std::endl;
	std::cout << "A, B (0 <= A, B <= 10)" << '\n' << std::endl;
	std::getline(std::cin, str);
	std::stringstream stream(str);
	stream >> a;
	stream >> b;
	if (a <= 0 || b > 10)
	{
		return getint();
	}
	else
	{
		return a + b;
	}
}
Last edited on
Er, you are not following the instructions.

Sample output
3


Your output:
The Sum Is: 3


Critique to Help You Out

The point of the test was to see if you have very basic comprehension and can write a trivial C++ program — a basic litmus, if you will, to weed out people who are lying on their résumés.

While it is clear that you can write a program, you have, sadly, missed the point.

The first mistake was trying to be clever. Don’t do that. It adds complexity and makes it too easy to confuse things.

For example, you have introduced a function named “getint”.

But the function does not get an int. It prints stuff to the console, gets two (or more) ints, possibly performs recursion, and returns the sum of the last two ints received at input.

Everything about that violates the concept behind “get an int”.

Beyond that, you have introduced a function which seeks to do all the stuff that the program is expected to do. In fact, the only thing it doesn’t do is print the sum. Whenever you find yourself writing main() under another name, stop and reconsider your structure.


You also do not test A and B properly, but you should not be doing that anyway — the instructions do not ask you to do that.


Breakdown

To help follow the instructions, let’s break them down.

Description
This is the problem description.

For this problem you must calculate A + B, numbers given in the input.
This is a general synopsis about what the program will do:
  • input will provide two numbers (A and B)
  • you will calculate their sum (A + B)

Notice that we are lacking a lot of specifics. The following will clarify the important points: how input is shaped, and how output is to be shaped.

Input specification
The only line of input contain two space separated integers A, B (0 <= A, B <= 10).
This is fairly clear, and you have understood everything but the constraints, which are a fairly standard mathematical notation to say that both A and B will fall in the range 0..10 inclusive.

Importantly, it does not ask you to verify this fact. You can take it as given that the input will be composed of two integers, separated by one or more spaces, and that the integers will be in the correct range of values.

Output specification
The only line of output should contain one integer: the sum of A and B.
You should produce:
  • exactly one line of output (meaning, terminated with a newline)
  • the output must have exactly one item: an integer
  • that integer must be the numerical sum of the two input integers

Sample input
1 2
Sample output
3
Common in program specifications are example inputs and outputs, meant to clarify the description with concrete examples. (And often, to hint at corner cases you should be aware of).

Here the problem designer has given you very exacting examples to clarify the instructions.
  • As “Sample input” we see two integers, separated by a space, both in the range 0..10 inclusive.
  • As “Sample output” we see a single integer. Applying a little mental math we see that it is, in fact, the sum of 1 and 2.

This is a very normal approach to specifying problems. It is typically —as in this case— very exacting. You should learn to recognize the structure and understand its meaning. You will see it again and again.


So, to finish off, a correctly performing program would look something like this:

1
2
3
4
5
6
7
#include <iostream>
int main()
{
  int A, B;
  std::cin >> A >> B;
  std::cout << (A + B) << "\n";
}

This program would demonstrate:
  • I can actually write a simple C++ program, which compiles and functions correctly
  • I have understood the program requirements and properly translated the
    “written problem” to working code.

It also demonstrates:
  • I don’t spend time with unnecessary convolutions.

That last point is important to an employer. It demonstrates an employee who can see a simple, straight-forward solution, and not spend time (== money) on bells and whistles or not get confused in the middle of an algorithm. It also demonstrates an employee who can plan an algorithm before jumping in to writing code.


Don’t be dismayed.

This is all part of the learning curve. The lessons here are:

  • be exact

  • don’t waste time trying to do extra or be fancy in any way
     this problem has not asked you to use functions, for example

  • recognize that written language follows specific patterns

I hope this helps. May future competitive programming efforts bring you fun and joy!
Thanks for replying Duthomhas, I feel really retarded because I didn't think the solution was so simple. I was sent this question by a classmate and I immediately assumed that it was complex. It seems I overestimated the problem due to the lack of proper comprehension skills. I also read the constraints as (A >= 0) && (B<= 10). I made the function complex because I didn't think the questions was at such a beginner level and that there was some hidden criteria behind the question. Thanks for the solution. It was correct.
Again, don’t be dismayed. We all have had to learn at some point.

Mine was rather belated. For a University assignment I had to implement an interpreter for a simple language... which I didn’t like. So I extended it in ways that I found interesting... and failed to make it do something that was part of the assignment.

The teacher had to tell me, in pretty simple terms, that I had wasted my time doing stuff that wasn’t asked.
Topic archived. No new replies allowed.