tdde18 726g77
play

TDDE18 & 726G77 Course Introductjon Christofger Holm - PowerPoint PPT Presentation

TDDE18 & 726G77 Course Introductjon Christofger Holm Department of Computer and informatjon science 1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files 2 / 76 Course Informatjon


  1. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Some text!\nMore!\n

  2. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; }

  3. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; }

  4. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } The

  5. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } The

  6. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; The cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; }

  7. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; The cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; }

  8. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; The cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } End!\n

  9. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; The cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } End!\n

  10. 26 / 76 IO Printjng program.cc #include <iostream> using namespace std; Text! int main() Some text! { More! cout << "Text!" << endl; The End! cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; }

  11. 27 / 76 IO Printjng ‚ cout prints to a bufger ‚ the bufger is printed when it is flushed ‚ usually the bufger is fmushed when a newline ( \n ) is printed (not guaranteed though) ‚ however to guarantee a fmush we can use endl instead (which also inserts a newline) ‚ to fmush without adding a newline we can use flush

  12. 28 / 76 IO What about reading? program

  13. 29 / 76 IO Storing things ‚ When reading things from the terminal we must be able to store these things in our program ‚ Everything entered into a terminal is text. ‚ But computers work with numbers, how do we read numbers? ‚ We can specify how the computer should interpret the things read by specifying a so called data type

  14. 1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

  15. 31 / 76 Variables Basics int main() { int x{3}; double y{3.14}; char z{'c'}; }

  16. 31 / 76 Variables Basics int main() int main() { { int x{3}; int x{3}; double y{3.14}; cout << "x = " char z{'c'}; << x << endl; } }

  17. 32 / 76 Variables Basics ‚ Variables are used to store and access data ‚ Have difgerent types; integers, decimal numbers, characters etc. ‚ Types determines what kind of values can be stored inside the variables. ‚ A variable can never change type ‚ Most variables can be printed to cout

  18. 33 / 76 Variables string #include <iostream> #include <string> using namespace std; int main() { string str {"hello"}; cout << str << endl << str.size() << endl << str.front() << endl; }

  19. 33 / 76 Variables string #include <iostream> #include <string> $ ./a.out using namespace std; hello int main() { 5 string str {"hello"}; cout << str << endl h << str.size() << endl << str.front() << endl; }

  20. 34 / 76 Variables string ‚ string is defjned in #include <string> ‚ can either be accessed with using namespace std; or by calling it std::string instead of just string ‚ Represents text (a sequence of characters). ‚ Has alot of builtjn functjonality that other types do not.

  21. 35 / 76 Variables const int x{5}; x = 7; int const y{7}; y = 9; // will not compile const int z{9};

  22. 36 / 76 Variables const ‚ Variables can be marked as read‐only by adding the keyword const ‚ This means that the value of the variable can never change. ‚ You can place the const before or afuer the data type. ‚ I recommend that you place it afuer the type, why will become apparent in later lectures.

  23. 1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

  24. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  25. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  26. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  27. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> programming 10 using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  28. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> programming 10 ê using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  29. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> programming 10 ê using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  30. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } Programming 10\n

  31. 38 / 76 More IO word = "" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } Programming 10\n

  32. 38 / 76 More IO word = "programming" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } 10\n

  33. 38 / 76 More IO word = "programming" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } 10\n

  34. 38 / 76 More IO word = "programming" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } 10\n

  35. 38 / 76 More IO word = "programming" number = 0 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } 10\n

  36. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } \n

  37. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } \n

  38. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } \n

  39. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  40. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  41. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> a using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  42. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> a ê using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  43. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> a ê using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

  44. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> a ê using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } a\n

  45. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } a\n

  46. 38 / 76 More IO word = "programming" number = 10 Reading letter = '\0' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } a\n

  47. 38 / 76 More IO word = "programming" number = 10 Reading letter = 'a' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } \n

  48. 38 / 76 More IO word = "programming" number = 10 Reading letter = 'a' program.cc #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; } \n

  49. 39 / 76 More IO Reading ‚ Reading values from the terminal into variables is done using cin . ‚ There is a bufger which the reading will be done from in fjrst‐hand. ‚ If the bufger is empty then, and only then will a prompt appear in the terminal. ‚ cin will read from this bufger untjl it is empty again. ‚ Most data types can be read from cin .

  50. 40 / 76 More IO Reading ‚ Whitespace characters are such characters as space, newline and other characters that doesn’t have a glyph. ‚ While reading from the bufger, cin will ignore all whitespaces untjl it reaches a non‐whitespace character. ‚ If cin fjnds a whitespace character (or any character that is nonsensical for the data type) while reading a value, the reading is done.

  51. 41 / 76 More IO getline line = "" string line; getline(cin, line); cin.ignore(1000, '\n'); This is a line\nAnother line\n

Recommend


More recommend