1、輸入
#include <iostream>#include <Windows.h>int main( void ){char girlType;int salary;float height;std::cout << " 請輸入您的理想類型 :\n A: 賢惠型 \n B: 潑辣新 \n C: 文藝型 \n D: 運(yùn)動型 " << std::endl;std::cin >> girlType;std::cout << " 請輸入您的月收入 :" << std::endl;std::cin >> salary;std::cout << " 請輸入您的身高 :[ 單位 - 米 ]" << std::endl;std::cin >> height;std::cout << " 您的理想類型是 : " << girlType << std::endl;std::cout << " 您的月收入是 : " << salary << " 元 " << std::endl;std::cout << " 您的身高是 : " << height << " 米 " << std::endl;system( "pause" );return 0;}
對于 char, int, float 等基本數(shù)據(jù)類型, 直接使用 std::cin >> 輸入即可?
輸出使用 std::cout <<輸入使用 std::cin >>
2、常量
long long 類型字面常量: 100000000000 LL (一千億,建議用大寫字母 LL )

程序的可讀性變差。代碼的可維護(hù)性差。
3、常見錯誤總結(jié)
int char; // 編譯失敗
int system; // 會導(dǎo)致后面不能使用 system 函數(shù)
#include <iostream>#include <Windows.h>using namespace std;int main( void ){int age;int num;num = age * 360;cout << " 請輸入您的年齡 : " ;cin >> age;cout << " 這是您在地球的 " << num << " 天 " << endl;system( "pause" );return 0;}
num = age * 360;?
?4. 數(shù)據(jù)輸入時,數(shù)據(jù)的類型不匹配
#include <iostream>#include <Windows.h>using namespace std;int main( void ){int age;int num;cout << " 請輸入您的年齡 : " ;cin >> age;num = age * 360;cout << " 這是您在地球的 " << num << " 天 " << endl;system( "pause" );return 0;}
運(yùn)行結(jié)果:
?為什么??
#include <iostream>#include <Windows.h>#include <string>int main( void ) {int a;int b;int c;std::cin >> a >> b >> c;std::cout << "a=" << a << std::endl;std::cout << "b=" << b << std::endl;std::cout << "c=" << c << std::endl;system( "pause" );return 0;}
?解決方案:
#include <iostream>
#include <Windows.h>#include <string>using namespace std;int main( void ) {int a;int b;int c;//std::cin >> a >> b >> c;std::cout << " 請輸入 a: " ;std::cin >> a;if (cin.fail()) { // 檢查輸入時是否發(fā)生了錯誤cout << " 輸入錯誤,應(yīng)該輸入一個整數(shù) " << endl;// 清除錯誤標(biāo)記,使得后續(xù)輸入可以正常進(jìn)行// 但是已經(jīng)輸入的數(shù)據(jù)還在輸入緩沖區(qū)cin.clear();cin.sync(); // 清空輸入緩沖區(qū)}std::cout << " 請輸入 b: " ;std::cin >> b;if (cin.fail()) {cout << " 輸入錯誤,應(yīng)該輸入一個整數(shù) " << endl;cin.clear(); // 清除錯誤標(biāo)記,使得后續(xù)輸入可以正常進(jìn)行cin.sync(); // 清空輸入緩沖區(qū)}std::cout << " 請輸入 c: " ;std::cin >> c;if (cin.fail()) {cout << " 輸入錯誤,應(yīng)該輸入一個整數(shù) " << endl;cin.clear(); // 清除錯誤標(biāo)記,使得后續(xù)輸入可以正常進(jìn)行}std::cout << "a=" << a << std::endl;std::cout << "b=" << b << std::endl;std::cout << "c=" << c << std::endl;system( "pause" );return 0;}
4、計(jì)算機(jī)英語加油站
單詞 | 說明 |
char
|
character
字符
|
int
|
integer
整數(shù)
|
short
|
短的
|
long
|
長的
|
unsinged
|
無符號的
|
double
|
雙倍的
|
float
|
浮動, 浮點(diǎn)數(shù)
|
name
|
名稱,名字
|
password
|
密碼
常簡寫為
pwd
|
precision
|
精度
應(yīng)用: cout.precision(4)? ?
默認(rèn)是有效數(shù)字
|
flags
|
flag
的復(fù)數(shù)
flag:
標(biāo)記
應(yīng)用:
cout.flags(xxx);
|
fixed
|
固定的
應(yīng)用:cout.flags(cout.fixed);
設(shè)置精度為保留小數(shù)后幾位
|
unset
|
復(fù)原
應(yīng)用:cout.unsetf(cout.fixed);
取消設(shè)置精度為保留小數(shù)后
幾位
|
const
|
常量
constant
不變的
|
5、職場修煉:怎樣安全度過試用期
#include <iostream>#include <Windows.h>using namespace std;int main( void ) {unsigned boyAge;unsigned girlAge;unsigned diff;cout << " 美女,多大了 ?" << endl;cin >> girlAge; // 輸入 25cout << " 帥哥,多大了 ?" << endl;cin >> boyAge; // 輸入 22diff = girlAge - boyAge;cout << " 美女比帥哥大 " << diff << " 歲 " << endl;diff = boyAge - girlAge;cout << " 帥哥比美女大 " << diff << " 歲 " << endl;system( "pause" );return 0;}
結(jié)果:
?文章來源:http://www.zghlxwxcb.cn/news/detail-437797.html
unsigned short boyAge2 = boyAge;unsigned short girlAge2 = girlAge;unsigned short diff2 = boyAge2 - girlAge2;cout << " 帥哥比美女大 " << diff2 << " 歲 " << endl; // 輸出 65533
即:該負(fù)數(shù) + “模值”-3 + 65536 = 65533
?文章來源地址http://www.zghlxwxcb.cn/news/detail-437797.html
到了這里,關(guān)于C++學(xué)習(xí)day--06 向計(jì)算機(jī)輸入數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!