Help me with C++

Hi. This is my C++ homework. I don't see whats wrong with it. What is wrong with it. Thanks

Code:
(4) The following C++ program does not compile. Explain where the
	mistake is. If you correct the mistake, what would the
	output of the program be?

#include <iostream>
using namespace std;

int main&#40;&#41;
{
  cout << &#34;2 + 3 * 5 = &#34;  2+3*5 << endl;

  return 0;
}

Comments

Not sure

edit:
opening dev-c++ to see if it compiles.

edit: found the problem, add a << in between the " 2


cout << "2 + 3 * 5 = " << 2+3*5 << endl;
 
The output of the program will be:

"2 + 3 * 5 = 17"

I suppose you could technically say it would be "2 + 3 * 5 = 25", but I think I remember hearing somewhere is always does 2 + (3 * 5) unless (2 + 3) * 5 is specified. Someone correct me if I'm wrong, though :P
 
Code:
&#40;4&#41; The following C++ program does not compile. Explain where the
	mistake is. If you correct the mistake, what would the
	output of the program be?

#include <iostream>
using namespace std;

int main&#40;&#41;
{
  cout << &#34;2 + 3 * 5 = &#34; &#91;b&#93;<<&#91;/b&#93; 2+3*5 << endl;

  return 0;
}
 
Thanks guys, turns out I had already figured this out lastnight, but it was late, and I am sick so I forgot, but the answer was in my text file :P THANKS GUYS!
 
[quote name='Trippy' post='1405891' date='Sep 17 2008, 10:51 PM']should be cout << "2 + 3 * 5 = " << 2+3*5 << endl;
missing the <<[/quote]

Except I doubt that's what his homework is getting him to think about (I suspect the missing << operator is a typo); as homework is supposed to make you think for yourself as opposed to just getting the answer from a forum somewhere I'm not going to say exactly what the 'mistake' is except to give the following hint: operator precedence
 
[quote name='IceFreak2000' post='1405910' date='Sep 17 2008, 03:55 PM'][quote name='Trippy' post='1405891' date='Sep 17 2008, 10:51 PM']should be cout << "2 + 3 * 5 = " << 2+3*5 << endl;
missing the <<[/quote]

Except I doubt that's what his homework is getting him to think about (I suspect the missing << operator is a typo); as homework is supposed to make you think for yourself as opposed to just getting the answer from a forum somewhere I'm not going to say exactly what the 'mistake' is except to give the following hint: operator precedence
[/quote]

Haaay don't think I'm coming here just for answers. The reason I came here is because I thought I did do that (which I actually did, read my post a few replies up), and it still didn't compile. I know my homework is for me!
 
[quote name='science' post='1405912' date='Sep 17 2008, 10:56 PM']<< is called an operator, right?[/quote]

Yes, it's an insertion operator, but that's not what I was driving at...
 
[quote name='IceFreak2000' post='1405927' date='Sep 17 2008, 04:00 PM'][quote name='science' post='1405912' date='Sep 17 2008, 10:56 PM']<< is called an operator, right?[/quote]

Yes, it's an insertion operator, but that's not what I was driving at...
[/quote]

No, I know what you were driving at. I don't think the order of operations is what he is talking about, because he says the program does not compile, and it asks why it doesn't compile. The second part of the question tests us about the operator precedence, because it asks what the program will output
 
[quote name='science' post='1405925' date='Sep 17 2008, 11:00 PM'][quote name='IceFreak2000' post='1405910' date='Sep 17 2008, 03:55 PM'][quote name='Trippy' post='1405891' date='Sep 17 2008, 10:51 PM']should be cout << "2 + 3 * 5 = " << 2+3*5 << endl;
missing the <<[/quote]

Except I doubt that's what his homework is getting him to think about (I suspect the missing << operator is a typo); as homework is supposed to make you think for yourself as opposed to just getting the answer from a forum somewhere I'm not going to say exactly what the 'mistake' is except to give the following hint: operator precedence
[/quote]

Haaay don't think I'm coming here just for answers. The reason I came here is because I thought I did do that (which I actually did, read my post a few replies up), and it still didn't compile. I know my homework is for me!
[/quote]

My apologies; sometimes you see people posting things like this and I just wonder why the hell they're doing the course if they're just going to get someone to give them the answer without understanding what's going on. You're obviously not one of that camp!
 
[quote name='The Teej' post='1405898' date='Sep 17 2008, 03:53 PM']The output of the program will be:

"2 + 3 * 5 = 17"

I suppose you could technically say it would be "2 + 3 * 5 = 25", but I think I remember hearing somewhere is always does 2 + (3 * 5) unless (2 + 3) * 5 is specified. Someone correct me if I'm wrong, though :P[/quote]

Nah, the answer is 17, you're right. The + has a lower precedence than the multiplication. It only does left to right if the precedence is the same
 

Blog entry information

Author
science
Views
294
Comments
28
Last update

More entries in Personal Blogs

More entries from science

  • More Poems
    I've been feeling inspired... I'm not sure what it is. But I've been...
  • Warm Pizza
    Tonight, I was in Kensington picking up pizza for a poker game at my...
  • A poem I wrote
    I don't write poetry, ever. But this just came to me, and I felt like I...
  • I bought a PS3
    So I bought my friend's PStriple off of him. It has a broken blu-ray...
  • I am too old for this, but...
    Yeargh random rant about girls thread. Long, long story short, my long...
General chit-chat
Help Users
  • No one is chatting at the moment.
    Xdqwerty @ Xdqwerty: yawn