C++/Console/cout
Версия от 14:21, 25 мая 2010; (обсуждение)
Содержание
Call function in cout
#include <iostream>
using namespace std;
inline int max(int a, int b)
{
return a>b ? a : b;
}
int main()
{
cout << max(10, 20);
cout << " " << max(99, 88);
return 0;
}
Unformattwd Input/Output: cin.get and getline
#include <iostream>
#include <string>
using namespace std;
string header ="Unformatted Input";
int main()
{
string word, rest;
cout << header << "Press <return> to go on" << endl;
cin.get(); // Read the new line without saving.
cout << "\nEnter a sentence! End with <!> and <return>." << endl;
cin >> word; // Read the first word
getline( cin, rest, "!"); // read a line up to the character !
cout << "The first word: " << word << endl
<< "Remaining text: " << rest << endl;
return 0;
}
Use function in cout
#include <iostream>
using namespace std;
int main()
{
cout << (10>20 ? 10 : 20);
cout << " " << (99>88 ? 99 : 88);
return 0;
}
Using the cout Object with Numeric Arrays
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int testScore[MAX];
for (int i = 0; i < MAX; i++)
{
cout << "Enter test score #" << i + 1 << ": ";
cin >> testScore[i];
}
cout << "The test scores are: " << testScore;
return 0;
}