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 - Calculate Area of a Square
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
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
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 << “\\”;
First, know the Difference int main() { cout << "hi"; return 0; } int main() { cout << "hi" << endl; return 0; }
Q1:What is the output? int main() { cout << "hi" << endl << endl << "hello" << endl; return 0; } A. B. hi hello hi hello
Q2: Are the outputs the same? int main() { cout << "hi" << endl; return 0; } int main() { cout << "hi\n"; return 0; } A. Yes B. No
Q3: What is the output? int main() { cout << "today\ntomorrow" << endl; return 0; } A. B. today\ntomorrowtoday tomorrow
Q4: What is the output? int main() { cout << "\\" << endl; return 0; } A. \ B. \\
Q5: What is the output? int main() { cout << "\n" << endl; return 0; } A. B. \n (Choice A means zero or more new lines)
Q6: What is the output? int main() { cout << "endl" << endl; return 0; } A. B. endl
Q7: What is the output? int main() { cout << "\\n" << endl; return 0; } A. \\n B. \n
Q8: What is the output? int main() { cout << "\\\\" << endl; return 0; } A. \\ B. \\\\
Q9: What is the output? int main() { cout << \n << endl; return 0; } A. \n B. Program could not be compiled, thus no output
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.
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.
Answers to Output Questions Q1 B Q2 A Q3 B Q4 A Q5 A Q6 B Q7 B Q8 A Q9 B