Another C++ program example on how to use the cin to read from standard input continuously

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Calculate the highest mark from the range of marks which read from the standard input using cin in C++ programming

To show: How to calculate the highest mark from the range of marks which read from the standard input using cin in C++ programming

 

// the stream extraction operator, another cin variation

#include <iostream>

using namespace std;

 

int main(void)

{

int mark, HighMark = -1;

 

cout<<"Enter grade(eof -Ctrl+Z- to stop): ";

while(cin>>mark)

{

if(mark>HighMark)

HighMark = mark;

cout<<"Enter grade(eof -Ctrl+Z- to stop): ";

}

cout<<"\nHighest grade is: "<<HighMark<<endl;

return 0;

}

 

Output example:

 

Enter grade(eof -Ctrl+Z- to stop): 23

Enter grade(eof -Ctrl+Z- to stop): 50

Enter grade(eof -Ctrl+Z- to stop): 45

Enter grade(eof -Ctrl+Z- to stop): 78

Enter grade(eof -Ctrl+Z- to stop): 15

Enter grade(eof -Ctrl+Z- to stop): 45

Enter grade(eof -Ctrl+Z- to stop): 90

Enter grade(eof -Ctrl+Z- to stop): ^Z

Highest grade is: 90

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index