EvilZone
Programming and Scripting => C - C++ => : rasenove March 27, 2013, 06:53:41 AM
-
Hi folks, I came across a quiz that said,
Write a program that reads two numbers from the user, adds them together, and then outputs the answer. The program should use two functions: A function named ReadNumber() should be used to get an integer from the user, and a function named “WriteAnswer” should be used to output the answer. You do not need to write a function to do the adding.
and the solution they gave was,
#include <iostream>
int ReadNumber()
{
using namespace std;
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void WriteAnswer(int x)
{
using namespace std;
cout << "The answer is " << x << endl;
}
int main()
{
int x = ReadNumber();
int y = ReadNumber();
WriteAnswer(x+y);
return 0;
}
I didnt look at that before i solved it by myself.
But my solution is different,
#include <iostream>
using namespace std;
//X & Y are two variables for storing nums
int X, Y;
int get_num()
{
cout<< "enter a number: ";
// X stores the 1st number
cin>> X;
cout<< "enter another number: ";
// Y stores the 1st number
cin>> Y;
}
int main()
{
get_num();
cout<< X << "+" << Y << "=" << X + Y <<endl;
return 0;
}
So which one is better? does my code have any mistake? it compiles okay and displays the result i wanted to achieve.
-
The second one obviously doesn't meet the specifications, so the first one is better.
-
If your talking about the "code should have two functions" than i dont get it, main() is a function too. So why didnt they count it?
-
@rasenove
The code you wrote was same that came to my mind after reading the question, but I think that the previous one is better.
-
If your talking about the "code should have two functions" than i dont get it, main() is a function too. So why didnt they count it?
When you have a specification, you do it by that specification.
And in your case it says:
<...> The program should use two functions: A function named ReadNumber() should be used to get an integer from the user, and a function named “WriteAnswer” should be used to output the answer. <...>
-
Kulverstukas is correct. You need to have 2 functions in addition to main(). The first function should be called 'ReadNumber' and the second function should be called 'WriteAnswer'. Also, try not to use global variables. The point of this exercise is to pass variables between functions. Your 'get_num' function does not actually return a value even though it is declared as having an integer return type.
-
If your talking about the "code should have two functions" than i dont get it, main() is a function too. So why didnt they count it?
while main() is a function by definition, it is also an entry point (EP), so it doesn't count as a "function" because it must be there for the program to compile.
-
i think the first code is better although even if i was the one i would write a code similar to the 2nd one
-
I would go for the first one.
When I code I do my very best to keep things seperated apply catogories and structure.
That way you can easily reuse certain pieces of code.
For example when I need to write stuff to files I make a dynamic module that can handle any kind of write operation.
I used to find myself rewriting very similar blocks.
Than again Im no expert.