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
293
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
  • ZeroT21 @ ZeroT21:
    a current gen i5 is plenty,next is RAM size, and then GPU depending on the kinda setting and game you play
  • ZeroT21 @ ZeroT21:
    @K3Nv2 why the G-series?
  • K3Nv2 @ K3Nv2:
    On board graphics it'll just be a ps3 emu machine
  • ZeroT21 @ ZeroT21:
    easier to troubleshoot? or gpu comes later?
  • K3Nv2 @ K3Nv2:
    Won't need a GPU with G series for emulation
    +1
  • ZeroT21 @ ZeroT21:
    I won't upgrade untill the day comes I can't play the games with my current hardware or something breaks
  • K3Nv2 @ K3Nv2:
    It's equivalent to a gt 560 which should be enough for ps3
  • K3Nv2 @ K3Nv2:
    I won't need to hack a 360 @BigOnYa :tpi:
  • ZeroT21 @ ZeroT21:
    well, if you get a gud enuf PSU, you could futureproof it incase you actually need the gpu down the line
  • K3Nv2 @ K3Nv2:
    I got a spare 650w some place
    +1
  • K3Nv2 @ K3Nv2:
    https://a.co/d/4aQazgy not bad for a $40 case
  • ZeroT21 @ ZeroT21:
    for me a 650w is base
    line
  • ZeroT21 @ ZeroT21:
    very gud case, but make sure you have the kinda PSU with very flexible cables
  • ZeroT21 @ ZeroT21:
    i know people with this exact case
  • K3Nv2 @ K3Nv2:
    Nah hog tie the cables
  • K3Nv2 @ K3Nv2:
    I'd like one with support for a couple 3.5" bays though
  • ZeroT21 @ ZeroT21:
    not that, some psu like those corsair rm series stock cables have very big knicks that make it impossible to fit in there
  • ZeroT21 @ ZeroT21:
    my friend had to order some custom cables for it
  • BigOnYa @ BigOnYa:
    Just buy another 650 watt power supply, and Raid them
  • ZeroT21 @ ZeroT21:
    well, it's not a showpiece, but it'll get the job done
  • K3Nv2 @ K3Nv2:
    Splice two 24 pins together
    +1
  • ZeroT21 @ ZeroT21:
    i bought mine with funtionality and cooling in mind for the most part, and fitting my crappy budget too ofc
  • K3Nv2 @ K3Nv2:
    We should all tell Xdqwerty when he gets old enough to build his own pc
  • ZeroT21 @ ZeroT21:
    i got 7 case fans in mine, replaced 4 when some broke cus they ran loud and wonky
    ZeroT21 @ ZeroT21: i got 7 case fans in mine, replaced 4 when some broke cus they ran loud and wonky