RE: Syntax errors will be the death of me!

  • From: "Ken Perry" <whistler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sat, 10 Jul 2010 23:01:36 -0400

Note you don't have to do the endl on separate lines.  Remember the << is a
pipe of sorts so you just shove the stuff from the one side to the other.
So these two do the same thing.

 

Cout <<"string';

Cout<<endl;

 

And

 

Cout <<"string"<<endl;

 

Ken

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Joseph Lee
Sent: Saturday, July 10, 2010 10:42 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Syntax errors will be the death of me!

 

Hi,

I see. You are already learning switch statement? That's an impressive
progress.

A brief hint: On the following lines, check out the display (cout) code. Do
you notice any difference:

// Jess's version:

cout << "Freshman."; << endl;

// Joseph's version:

cout << "Freshman." << endl;

Notice that there is no semicolon after the text message. If you do put
semicolon, you'll get the syntax error because the compiler couldn't find
the output (cout) operator. A slightly better variation might be:

cout << "Freshman".;

cout << endl;

Another hint (mostly an advice): In the conditional statement, you might
want to try "if" statement versus the "switch" statement. Here's how I would
do it (if I were you):

// Joseph's variation:

// Uses if statement to select a printout message.

if (code == 1)

cout << "Freshman." << endl;

else if (code == 2)

cout << "Sophomore." << endl;

else if (code == 3)

cout << "Junior." << endl;

else if (code == 4)

cout << "Senior." << endl;

else

cout << "Error: invalid code!" << endl;

I (or someone) will explain the previous code in a sec.

Cheers,

Joseph

 

 

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jes
Sent: Saturday, July 10, 2010 7:21 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Syntax errors will be the death of me!

 

Hey guys,

Here's a code for an assignment which has to be turned in tomorrow. Every
time I try to compile it, Visual Studio keeps telling me I'm missing a ;
before <<

What's wrong?

Code follows:

//Ch6ConE06.cpp

//Displays a class rank based on a code 

//entered by the user

//Created/revised by <your name> on <current date>

 

#include <iostream>

 

using std::cout;

using std::cin;

using std::endl;

 

int main()

{               

                //declare variable

                int code = 0;

 

                //enter input

                cout << "Enter the code: ";

                cin >> code;

//enter switch statements

switch(code)

{

case 1:;

cout << "Freshman. " ; << endl;

break;

case 2:

                cout <<"Sophomore"; << endl;

break;

case 3:

cout << "Junior "; << endl;

break;

case 4:

cout << "Senior" ; << endl;

break;

case 5:

cout << "Error! "; << endl;

}

//endswitch

//display output

 

    return 0;

}   //end of main function

 

Jes

 

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.441 / Virus Database: 271.1.1/2992 - Release Date: 07/10/10
19:59:00

Other related posts: