The following C++ program does not compile

  • Context: C/C++ 
  • Thread starter Thread starter soul5
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary
SUMMARY

The forum discussion centers on a C++ compilation issue related to outputting expressions using the stream insertion operator (<<). The incorrect syntax presented was count << "2 + 3 * 5 = " 2+3*5 << endl;, which fails to compile due to improper use of the operator. The correct approach is count << "2 + 3 * 5 = " << (2+3*5) << endl;, where the expression is properly enclosed in parentheses and separated by the insertion operator. This highlights the necessity of correctly formatting output statements in C++.

PREREQUISITES
  • Understanding of C++ syntax and operators
  • Familiarity with the stream insertion operator (<<)
  • Basic knowledge of arithmetic expressions in C++
  • Experience with compiling C++ programs
NEXT STEPS
  • Review C++ operator precedence and associativity
  • Learn about C++ output streams and formatting
  • Explore common C++ compilation errors and debugging techniques
  • Practice writing and compiling simple C++ programs
USEFUL FOR

C++ developers, programming students, and anyone looking to improve their understanding of output syntax and compilation in C++.

soul5
Messages
63
Reaction score
0
count << "2 + 3 * 5 = " 2+3*5 << endl;


do I just need to put it as

count << "2 + 3 * 5=" << (2+3*5) << endl;
 
Technology news on Phys.org
soul5 said:
count << "2 + 3 * 5 = " 2+3*5 << endl;do I just need to put it as

count << "2 + 3 * 5=" << (2+3*5) << endl;

First one is bad, second one looks good.

Why don't you simply compile the second, run and test it? ooO, does the second one also throws error?
 
I'm pretty sure you can't mix string output together with variables. You must separate them into with <<.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
10
Views
2K
Replies
12
Views
3K
Replies
12
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
7
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K