2

I'm pretty new to C++ and I'm taking an intro course in it right now at school. One of the assignments for this week is to solve a fairly lengthy equation. So what I've done is break it down into little chunks. I've been getting weird output when I try to use the sin function, so I started messing around a bit and isolated it to this...

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double input1, input2, a;
    cout << "Enter first input." << endl;
    cin >> input1;
    cout << "Enter second input." <<endl;
    cin >> input2;

    a = input2 - input1*2;
    cout << a << endl; // This doesn't give expected output
    cout << input2 - 2*input1 <<endl; //This gives the expected result
    return 0;
}

And as a return value I'm getting an insanely small number: 6.95323e-310. The obvious question is, why?

If it helps at all I'm doing this on a Mac OS using g++ 4.2.

My input values are 5 and 2...so I'd expect a -8.

Thanks.

Matt
  • 3,508
  • 6
  • 38
  • 66
  • 3
    Whit what values of input1 and input2 do you get that result? – Christian Tapia Oct 25 '13 at 07:06
  • Oh, forgot that. See edit. – Matt Oct 25 '13 at 07:06
  • 3
    [Works as expected on ideone](http://ideone.com/oUIutO). Are you sure you are not running a stale binary or something (meaning you are not actually running the code you think you are)? I don't think this is a compiler issue, because this would be a very serious bug in g++ if that was the case. – Björn Pollex Oct 25 '13 at 07:08
  • I ran your program on my machine, input `5` and `2`, get `-8` as expected. – Yu Hao Oct 25 '13 at 07:08
  • It works fine on my compiler too. – Zero Fiber Oct 25 '13 at 07:09
  • I get - 8 on vc++ (windows). – David Oct 25 '13 at 07:09
  • 2
    Works at Visual Studio '10. Even tho I would initialize the variables to `0`. – Blacktempel Oct 25 '13 at 07:09
  • 1
    Hmmm....all of a sudden it's working. I swear it was giving me weird output the second before I posted that. Not that it matters, but in the interest of curiosity can anyone think why it *might* have done that? **Nevermind it's still freaking out** – Matt Oct 25 '13 at 07:11
  • Mmm, are you sure your input were those 2 numbers? – Christian Tapia Oct 25 '13 at 07:12
  • Yes. Absolutely positive. I did it numerous times before I posted. – Matt Oct 25 '13 at 07:13
  • @Matt Try to find out what your original problem is and post a new question. – Sarien Oct 25 '13 at 07:15
  • the most likely explanation is a stale binary, as mentioned above – codeling Oct 25 '13 at 07:15
  • Problem still stands @Sarien, sorry. – Matt Oct 25 '13 at 07:16
  • @nyarlathotep I'll research that. Thanks. – Matt Oct 25 '13 at 07:16
  • 1
    @Matt Try printing the values of input1 and input2. The only thing I can imagine is something weird with your input and nothing getting assigned. – Sarien Oct 25 '13 at 07:17
  • Change something to the text and recompile. If you re-run, do you see the change? Just to make sure you're actually running the right executable. – thelamb Oct 25 '13 at 07:17
  • Printing out the values individually works as expected. If I do the math in the `cout` line it returns `-8`, however if I do the math and assign the output to a variable it returns the problem number. – Matt Oct 25 '13 at 07:19
  • Try declare a as 'int'. – Marikkani Chelladurai Oct 25 '13 at 07:21
  • It works if I declare it as int. But for my assignment I'll need doubles. Otherwise I wouldn't care. Thanks for the suggestion. – Matt Oct 25 '13 at 07:22
  • One non-question-related comment: I think you are starting C++ programming in the right way (No C headers, no `printf`, no unnecesary pointers, etc), so I wan't to help you and improve your style a bit more: [**Avoid `using namespace std`**](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Manu343726 Oct 25 '13 at 07:25
  • @matt take a look at my answer below:) – GMasucci Oct 25 '13 at 08:55

1 Answers1

1

the code you posted works fine for me under a variety of compilers, however I would suggest a couple of improvements:

cout << input2 - 2*input1 <<endl; I would change this to cout << input2 - (2*input1) <<endl;

I would also ensure that cin is totally clear before taking an input by using: the following modified code:

cout << "Enter first input." << endl;
cin.clear();
cin >> input1;
cout << "Enter second input." <<endl;
cin.clear();
cin >> input2;

I only once managed to replicate your error by happenstance and it was occurring because the inputs were not set correctly.

After that I stepped through the program a few times and checked outputs: when the variables are not set correctly (i.e. they contain whatever value happens to be at the memory location utilised by the variable) I consistently get answers similar to your posted one.

So the simple answer is that the variables are un-initialised because the cin buffer is not cleared.

Hope this helps, and let me know if you need a better/more in-depth explanation:)

Addendum:

A few more points I just thought of on reading the other comments:

using namespace std; - this is fine for small projects you control totally, ie homework, small assignments etc. however, when you are using diverse code (other libraries, APIs etc) it is more advisable to control namespaces directly: they exist to allow you to target specific functionality within a given namespace, and avoid name contention/ambiguity in the the code. For example you might call cin at present, later though you might include another API in you code, lets call it TEST namespace, so you add using namespace TEST; TEST contains its own implementation of cin, which handles cin clearing automatically (handy because that may solve you problem) however your code cant compile now because you have std::cin and TEST::cin both available, and because you use the using namespace for both, when you type cin it could be either.

There was also a suggestion that you use float and not double, couple of points on this:

  • using float or double will allow decimal values= TRUE
  • using float rather than double will fix you problem= very very rarely True, there are limited instances where the bit-size of a float will fix a problem that the bit-size of a double caused.
  • general programming convention: for memory efficiency, utilise the smallest possible type which can contain all your use-case values (a double is nominally twice the size of a float in memory, so 0.3f is half the memory usage of 0.3d

I know this is for homework, as you mentioned that's why I have added the extra information,: it is always good to get more information, whether you agree with it or not having it will allow you to research more in-depth and either agree/prove or disagree/disprove the initial information you had. Welcome to programming, btw, it only gets worse!

Better! I meant better!

GMasucci
  • 2,834
  • 22
  • 42
  • That worked. Actually, a few hours after posting it stopped acting up. Which was nice, but also strange because it's a computer, it isn't a finicky teenager. So I figured it was something that was beyond my knowledge/ability. But you're right, that fixed it. And a huge thank you for the extra info. Should I be clearing the `cin` buffers after every input? – Matt Oct 25 '13 at 17:23
  • 1
    I tend to if I am making a console only application, the expense in processing (minimal) has been worth it for me, as it guarantees that the buffers only contain the input the user entered, mainly I do it as there are small differences between OSes and clearing the buffer ensures the same behaviour across Windows, Ubuntu and MacOS for me. Sorry for the late reply, my home computer is experiencing teenage finnickyness :( and refusing to boot even after several wipe and install cycles. – GMasucci Oct 28 '13 at 08:13
  • 1
    I know computers are meant to be able to carry out the same operation repeatedly with a consistent output, however, after year of arguing with them, sorry, programming them, they have proven to be exactly as bad as a teenager:) – GMasucci Oct 28 '13 at 08:15