Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -

Similar presentations


Presentation on theme: "Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -"— Presentation transcript:

1 Dayu Zhang 9/10/2014 Lab03

2 Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise - Calculate Area of a Square

3 Steps of Hello.cpp Create Command: touch Hello.cpp Edit Command: vi Hello.cpp Compile Command: g++ Hello.cpp If there is compile error, file a.out will not be generated Run Command:./a.out

4 Distinguish In or Not In vi Editor All commands in previous slide should be used outside of vi Editor When in vi Editor Insert Mode: edit code Command Mode: save code and quit vi editor

5 Understand endl and \n In C++, they both mean new line. endl --- it is used outside of “ ” \n --- it is used inside of “ ” \ --- it has special meaning If you want to print \, you cannot use cout << “\”; Instead, you will use cout << “\\”;

6 First, know the Difference int main() { cout << "hi"; return 0; } int main() { cout << "hi" << endl; return 0; }

7 Q1:What is the output? int main() { cout << "hi" << endl << endl << "hello" << endl; return 0; } A. B. hi hello hi hello

8 Q2: Are the outputs the same? int main() { cout << "hi" << endl; return 0; } int main() { cout << "hi\n"; return 0; } A. Yes B. No

9 Q3: What is the output? int main() { cout << "today\ntomorrow" << endl; return 0; } A. B. today\ntomorrowtoday tomorrow

10 Q4: What is the output? int main() { cout << "\\" << endl; return 0; } A. \ B. \\

11 Q5: What is the output? int main() { cout << "\n" << endl; return 0; } A. B. \n (Choice A means zero or more new lines)

12 Q6: What is the output? int main() { cout << "endl" << endl; return 0; } A. B. endl

13 Q7: What is the output? int main() { cout << "\\n" << endl; return 0; } A. \\n B. \n

14 Q8: What is the output? int main() { cout << "\\\\" << endl; return 0; } A. \\ B. \\\\

15 Q9: What is the output? int main() { cout << \n << endl; return 0; } A. \n B. Program could not be compiled, thus no output

16 Understand Comment // Current line is comment. You should always put your name in the first line of programming assignment // Name: LastName, FristName /* */ All lines in between are comments.

17 Exercise: Calculate Area of a Square Write a program that asks user to input the length of the side of a square (type: int) and then output the area of the square.

18 Answers to Output Questions Q1 B Q2 A Q3 B Q4 A Q5 A Q6 B Q7 B Q8 A Q9 B


Download ppt "Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -"

Similar presentations


Ads by Google