1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
void test1() { int myInt; long myLong;
char buf[128] = { 0 };
cin >> myInt; cin >> myLong; cin >> buf;
cout << "myInt: " << myInt << endl; cout << "myLong: " << myLong << endl; cout << "buf: " << buf << endl; }
void test2() { char ch;
while ((ch = cin.get() )!= EOF) { cout << ch << endl; } }
void test3() { char a, b, c;
char buf[10] = { 0 };
cout << "从输入缓冲区去读取数据,如果缓冲区中没有数据,就阻塞" << endl;
cin.get(buf, 10, ' ');
cout << buf << endl; }
void test4() { char buf[128] = { 0 }; cout << "请输入一个字符串 aa bb cc dd" << endl; cin.getline(buf, 128);
cout << "buf:" <<buf << endl; }
void test5() { char buf1[128]; char buf2[128];
cout << "请输入一个字符串 aa bb cc dd" << endl; cin >> buf1; cin.ignore(2); cin.getline(buf2, 128);
cout << "buf1:" << buf1 << endl; cout << "buf2:" << buf2 << endl;
}
void test6() { cout << "请输入一个数字或者字符串" << endl; char ch; ch = cin.get(); if ((ch >= '0') && ch <= '9') { cout << "输入的是一个数字" << endl; int num; cin.putback(ch); cin >> num;
cout << "num =" << num << endl; } else { cout << "输入的是一个字符串" << endl; char buf[128] = { 0 }; cin.getline(buf, 128);
cout << "buf:" << buf << endl; } }
int main(void) { test6(); return 0; }
|