SLIDE07 - Exposure C Chapter VII Program Input and Output...

This preview shows page 1 - 13 out of 37 pages.

Exposure C++ Chapter VII Program Input and Output
Image of page 1

Subscribe to view the full document.

Program Input With cin C++ provides an input tool that looks, and behaves, in the opposite manner as cout , called cin . This C++ keyword is pronounced c-in . You will find that the special operators used for cout << are also used for cin . With program input, the “less-than chevrons” are turned around to become “greater-than chevrons.”
Image of page 2
Insertion and Extraction Operators cout puts ( inserts ) characters into the output stream with the insertion operator << cin removes ( extracts ) characters from the input stream with the extraction operator >>
Image of page 3

Subscribe to view the full document.

// PROG0701.CPP // This program demonstrates numerical keyboard input. #include <iostream.h> void main() { double Payrate; // hourly wage int HoursWorked; // hours worked per week double GrossPay; // hours worked times hourly wage cout << "ENTER YOUR HOURLY PAYRATE ===>> "; cin >> Payrate; cout << "ENTER YOUR WEEKLY HOURS ===>> "; cin >> HoursWorked; GrossPay = Payrate * HoursWorked; cout << endl; cout << "GROSSPAY: " << GrossPay << endl; } PROG0701.CPP OUTPUT ENTER YOUR HOURLY PAYRATE ===>> 8.75 ENTER YOUR WEEKLY HOURS ===>> 21 GROSSPAY: 183.75
Image of page 4
cin is used for input cin >> Number; cout is used for output cout << Number; Input/Output with cin and cout
Image of page 5

Subscribe to view the full document.

Using a "Prompt" with Program Input cin stops program execution and waits for input. There is no indication other than the execution halt that anything needs to be done. Every input statement should be preceded by a logical prompt . Prompt Example: cout << "Enter a positive integer --> "; cin >> Number;
Image of page 6
// PROG0702.CPP // This program demonstrates multiple numeric keyboard input on one line. #include <iostream.h> void main() { int Nbr1,Nbr2; cout << "ENTER INTEGER1 <Space> INTEGER2 ===>> "; cin >> Nbr1 >> Nbr2; cout << endl; cout << Nbr1 << endl; cout << Nbr2 << endl; } PROG0702.CPP OUTPUT ENTER INTEGER1 <Space> INTEGER2 ===>> 123 456 123 456
Image of page 7

Subscribe to view the full document.

// PROG0703.CPP // This program demonstrates character keyboard input. // The second input statement is ignored when more than one // character is entered. #include <iostream.h> void main() { char C1,C2,C3; cout << "Enter Character 1 ===>> "; cin >> C1; cout << "Enter Character 2 ===>> "; cin >> C2; cout << "Enter Character 3 ===>> "; cin >> C3; cout << endl; cout << C1 << C2 << C3 << endl; }
Image of page 8
PROG0703.CPP OUTPUTS PROG0703.CPP OUTPUT #1 Enter Character 1 ===>> A Enter Character 2 ===>> B Enter Character 3 ===>> C ABC PROG0703.CPP OUTPUT #2 Enter Character 1 ===>> AB Enter Character 2 ===>> Enter Character 3 ===>> CD ABC
Image of page 9

Subscribe to view the full document.

// PROG0704.CPP // This program demonstrates multiple character keyboard // input on one line. // This program works with or without inserted "white" // space. #include <iostream.h> void main() { char C1,C2,C3; cout << "Enter 3 Characters ===>> "; cin >> C1 >> C2 >> C3; cout << endl; cout << C1 << C2 << C3 << endl; } PROG0704 OUTPUT #1 Enter 3 Characters ===>> ABC PROG0704 OUTPUT #2 Enter 3 Characters ===>> A B C
Image of page 10
cin and White Space The cin function ignores any white space characters in the input stream. Only visible characters are “extracted.” Letters, numbers and symbols can be entered. Space-bar, Enter key, cursor-keys, Function-keys, etc. are characters known as white space , and will not be extracted by cin from the input stream.
Image of page 11

Subscribe to view the full document.

// PROG0705.CPP // This program demonstrates string keyboard input.
Image of page 12
Image of page 13
  • Fall '16
  • Ms. Walker
  • Decimal, Carriage return, Input/output, cout

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern