C++/Console/cin — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:27, 25 мая 2010
Содержание
- 1 A Closer Look at the I/O Operators
- 2 cin and cout work with char array
- 3 cin Object"s getline Function
- 4 cin to read
- 5 cin to read float in C++
- 6 Filling an Array with a Maximum Number of Characters
- 7 Unformattwd Input/Output
- 8 Use the getline function to read the user"s input and assign that input to the character array
- 9 Using the cin and cout Objects with Arrays
A Closer Look at the I/O Operators
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
float f; char str[80]; double d; cout << "Enter two floating point numbers: "; cin >> f >> d; cout << "Enter a string: "; cin >> str; cout << f << " " << d << " " << str; return 0;
}
</source>
cin and cout work with char array
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
char name[ 80] = {"J", "e", "f", "f", "/0" }; cout << "Enter your name: "; cin >> name; cout << "Your name is " << name; return 0;
}
</source>
cin Object"s getline Function
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
char name[ 80] = {"J", "e", "f", "f", "/0" }; cout << "Enter your name: "; cin >> name; cout << "Your name is " << name; return 0;
}
</source>
cin to read
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
int testScore1, testScore2, testScore3; cout << "Enter test score #1: "; cin >> testScore1; cout << "Enter test score #2: "; cin >> testScore2; cout << "Enter test score #3: "; cin >> testScore3; cout << "Test score #1: " << testScore1 << endl; cout << "Test score #2: " << testScore2 << endl; cout << "Test score #3: " << testScore3 << endl; return 0;
}
</source>
cin to read float in C++
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
double x, y; cout << "Enter two floating-point values: "; cin >> x ; cin >> y; cout << "The average of the two numbers is: " << (x + y)/2.0 << endl; return 0;
}
</source>
Filling an Array with a Maximum Number of Characters
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
char buffer[80] = {"\0"}; cout << "Enter the string: "; cin.get(buffer, 79); // get up to 79 or newline cout << "Here"s the buffer: " << buffer << endl; return 0;
}
</source>
Unformattwd Input/Output
<source lang="cpp">
- 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;
}
</source>
Use the getline function to read the user"s input and assign that input to the character array
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
char name[80] = {"J", "e", "f", "f", "/0" }; cout << "Enter your name: "; cin.getline(name, 80); cout << "Your name is " << name; return 0;
}
</source>
Using the cin and cout Objects with Arrays
<source lang="cpp">
- include <iostream>
using namespace std; const int MAX = 3; int main () {
char grades[MAX]; for (int i = 0; i < MAX; i++) { cout << "Enter grade for test #" << i + 1 << ":"; cin >> grades[i]; } return 0;
}
</source>