0

I'm trying to do the following assignment in a c++ book.

After running this:

#include <iostream>

using namespace std;

int main()
{
double first_arg;
double second_arg;

cout << "Enter first argument: ";
cin >> first_arg;
cout << "Enter second argument: ";
cin >> second_arg;

cout << first_arg << " * " << second_arg <<  " = "
     << cout << first_arg * second_arg << "\n";

cout << first_arg << " + " << second_arg << " = "
     << cout << first_arg + second_arg << "\n";

cout << first_arg << " / " << second_arg << " = "
     << cout << first_arg / second_arg << "\n";

cout << first_arg << " - " << second_arg << " = "
     << cout << first_arg - second_arg << "\n";

I get some unexpected results. Like this result copied straight from the windows cli:

Enter first argument: 7
Enter second argument: 9
7 * 9 = 0x6fcc43c463
7 + 9 = 0x6fcc43c416
7 / 9 = 0x6fcc43c40
7 - 9 = 0x6fcc43c4-2

I'm using the latest version of codeblocks with the default compiler settings. Thanks.

bad
  • 939
  • 6
  • 18

1 Answers1

3
cout << first_arg << " * " << second_arg <<  " = "
     << cout << first_arg * second_arg << "\n";

You have two cout in one line since there is no semicolon on line 1

To fix this either get rid of the second cout or add a semicolon at the end of the first line on each cout statement.

If you look at the last 2 digits of each answer you will see the answer you wish to get so its still printing out the answer you want its just after the pointer to cout.

TheQAGuy
  • 498
  • 7
  • 17
  • Interesting. I never made `cout << cout;` and never expected that it could print such result. – A.S.H Oct 04 '15 at 23:52
  • Haha! Thanks a lot, man! I don't know how I didn't see that :s – bad Oct 04 '15 at 23:55
  • 1
    explanation of wierd hex number can be found at http://stackoverflow.com/questions/10987156/does-stdcout-have-a-return-value – Ritesh Oct 04 '15 at 23:56
  • `cout` doesn't point to anything, it just used to have an unfortunate conversion to `void*` in older versions of C++. – chris Oct 05 '15 at 00:19
  • @chris Yeah i know i was misinformed. I figured deleting my answer wouldnt be a good choice however since it did fix his problem so i just edited my answer to get rid of that. Ritesh provided a good link to read if someone wants to know why it outputs this – TheQAGuy Oct 05 '15 at 00:27