Adding Forever?

Dec 13, 2013 at 4:13am
I am taking a c++ tutorial on learncpp.com and was asked to write a program that reads two numbers from the user and adds them together using one function to get the input and one function to print the answer and doing the rest in main(). What I started with was different from their solution, but I thought it would work. It printed 2130567168. I've now literally copy pasted their solution into my ide and it still prints that number. And yes I remembered to rebuild before running again.

tl;dr: why it print the big numbers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #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;
}
Dec 13, 2013 at 7:59am
First: line 4 goes after line 1

Second: delete line 13 EDIT: originally had 14 here instead

Third: I'm not sure what's causing the issue... usually 21blahblah implies an uninitialized number being outputted. Try fixing the 2 things I mentioned, see if anything different happens. Your program looks right...

Last edited on Dec 13, 2013 at 8:29am
Dec 13, 2013 at 8:04am
I think you can safely ignore sargon94's post, since none of it make sense.

The program works correctly for me. Is your input something that would put cin into an error state?

http://ideone.com/nZrqZn
Dec 13, 2013 at 8:19am
Pointing out that moving using namespace std; to the global scope would be optimal in this case makes no sense?

Pointing out that getting ~(2^32)/2 as an output value is a common sign of an uninitialized value made no sense?

Pointing out that the program should work as is (same as your post) made no sense?

Why the confusion?
Dec 13, 2013 at 8:32am
sargon94 wrote:
Pointing out that moving using namespace std; to the global scope would be optimal in this case makes no sense?

Optimal in that fewer characters would be typed or optimal in that everything in the std:: namespace would be exposed in a larger scope? The former is a poor measuring stick. The latter is not exactly what I would call optimal.

So no, it makes no sense.


sargon94 wrote:
Second: delete line 14

It makes no sense to delete the line which does the output.


sargon94 wrote:
Third: I'm not sure what's causing the issue... usually 21blahblah implies an uninitialized number being outputted. Try fixing the 2 things I mentioned, see if anything different happens.

Random changes to a program in the hopes they change the outcome do not make sense, no. Unless you subscribe to the monkeys-with-typewriters school of debugging.



Dec 13, 2013 at 9:12am
Well I suppose you're right. Although I did amend the line 14 thing.

But anyway as far the OP is concerned the program should work as is.
Dec 13, 2013 at 1:25pm
I think your program is True
Last edited on Dec 13, 2013 at 1:26pm
Topic archived. No new replies allowed.