1 / 16

White Spaces

White Spaces. Space Tab Next line …. Input Operator >>. cout << "What is your input: "; cin >> theInput; // Operator >> will ignore white spaces // before reading // Operator >> will stop at white spaces // after have read any value // Input: -50.5 50

Download Presentation

White Spaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. White Spaces Space Tab Next line …

  2. Input Operator >> cout << "What is your input: "; cin >> theInput; // Operator >> will ignore white spaces // before reading // Operator >> will stop at white spaces // after have read any value // Input: -50.5 50 // What is the value of theInput? // Depending on the type of theInput // But the spaces before “-50.0” will be ignored.

  3. Type char char theInput; cout << "What is your input: "; cin >> theInput; // Input: -50.5 50 // Q: What is the value of theInput? // A: _____ // ‘-’ // Not space before ‘-’

  4. Type integer int theInput; cout << "What is your input: "; cin >> theInput; // Input: -50.5 50 // Q: What is the value of theInput? // A: _____ // -50 // stop at ‘.’, which cannot be part of int

  5. Type float float theInput; cout << "What is your input: "; cin >> theInput; // Input: -50.5 50 // Q: What is the value of theInput? // A: _____ // -50.5

  6. Type string string theInput; cout << "What is your input: "; cin >> theInput; // Input: -50.5 50 // Q: What is the value of theInput? // A: _____ // -50.5

  7. Tracing aChar C S 1 4 3 X // Input // CS 143 // X char aChar; int count = 0; cin >> aChar; // Prime read before loop while (aChar != ‘X’) { count ++; cout << aChar; cin >> aChar; } cout << aChar; cout << “Count: “ << count;

  8. Function cin.get() The function will read one char at a time Including white spaces!

  9. Tracing aChar C S - 1 4 3 \n X // Input // CS 143 // X char aChar; int count = 0; cin.get(aChar); // Prime read before loop while (aChar != ‘X’) { count ++; cout << aChar; cin.get(aChar); } cout << aChar; cout << “Count: “ << count;

  10. Function cin.eof() The function returns true or false eof(): end of file end of input From keyboard [CTRL-D] From File Not True after reading the last item in the file True when trying to read after reading the last item in the file

  11. Tracing aChar cin.eof() C False S False - False 1 False 4 False 3 False \n False X False ? True // Input // CS 143 // X char aChar; int count = 0; cin.get(aChar); // Prime read before loop while (!cin.eof()) { count ++; cout << aChar; cin.get(aChar); } cout << aChar; cout << “Count: “ << count;

  12. int theInput; int count = 0; cout << "What is your input: "; cin >> theInput; while (!cin.eof()) { count ++; cout << "What is your input: "; cin >> theInput; } cout << theInput; cout << “Count: “ << count; // Input: 55 50.5 60 // What is the value of theInput? // Trouble when reading ‘.’!

  13. Formatting Output cout << endl << "My output."; cout << "\nMy output."; float average; int count; // Compute count and average cout << endl << count; cout << endl << average; // How many digits displayed for average?

  14. #include <iomanip> cout << fixed << showpoint << setprecision(2); // Always display two decimal digits. // No need to remember for quiz/test float value; value = 1 / 3.0; cout << "\nFloat value 1/3.0: " << value; // 0.33 value = 36 / 3.0; cout << "\nFloat value 36/3.0: " << value; // 12.00 value = 1 / 5.0; cout << "\nFloat value 1/5.0: " << value; // 0.20

  15. #include <iomanip> cout << fixed << showpoint << setprecision(2); // The setting is applied to all values displayed // until the setting changes. float value; value = 1 / 3.0; cout << "\nFloat value 1/3.0: " << setw(8) << value; // □ □ □ □ 0.33 value = 36 / 3.0; cout << "\nFloat value 36/3.0: " << setw(8) << value; // □ □ □ 12.00 value = 1 / 5.0; cout << "\nFloat value 1/5.0: " << value; // 0.20 (no extra spaces) // setw() is only applied to the next value // It has to be used for every value need the format.

  16. Schedule • Prog1 Can still fix style • Prog2: Loops Start Early! Two points Quiz Monday! • Lab 2 Demo by 4pm • Quiz3-4 Due 5 pm Monday • Test 1: Next Friday Note01 – Note10 Prog2:Loops! • Quiz3-5 Now!

More Related