標題:
problem of cin.get()和cin
發問:
呢個我係用黎玩下,我想limit users to enter only one characterchar getOneChar() { char c; while(true) { cin >> c; if (c>cin.get() ) return c; cout << "Too many characters! " "Please input again.\n"; cin.ignore(200, '\n'); } } int main(){... 顯示更多 呢個我係用黎玩下,我想limit users to enter only one character char getOneChar() { char c; while(true) { cin >> c; if (c>cin.get() ) return c; cout << "Too many characters! " "Please input again.\n"; cin.ignore(200, '\n'); } } int main(){ cout<cin.get(), 當我type 'ppp' or 'pqr' or 'p' or 'abc'呢兩句different conditions cannot fulfil my aim. 我想知當我打上面個幾個可能性既時候, cin.get()同cin分別變左D咩野? 希望有人可以幫我LIST出黎,諗左好耐都唔識....
最佳解答:
1. cin >> c; 呢個statement唔係解只係入一個字完咩? 即係如果 c 只係字元, 就算你打 "ppp" 呢種字串,佢都係睇第一個字元 i.e. "ppp" c會變成 'p' "pqr" c會變成 'p' "abc" c會變成 'a' 2. 仲有,字串唔會用 ' '....係用 " " !!!! 3. cin.ignore好似要 c 係字串先用到,即係你用完等於無用. 你第一個數就係200,就算係字串,會唔會去到200咁多. 4. c>cin.get() 我唔明你呢個想點, cin.get()呢個function有好多用法,但係唔係俾你咁用. 你可以: c = cin.get(); or cin.get(c) 我唔明你 cin.get(); 想點, 你咁樣即係要求輸入,不過無話input到邊到. 5. 你一係咁做,最簡單 char getOneChar() { char c; cin >> c; return c; } 如果你想一定要同一講 "Too many characters! "同"Please input again.\n"; 一係就咁 char getOneChar() { char c[20]; int t; while(1){ cin >> c; if (c[1]=='\0'){ return c[0]; }else { cout << "Too many characters! Please input again.\n" ; /*重洗 c */ for(t=0;t<20;t++) c[t] == '/0'; } } }
problem of cin.get()和cin
發問:
呢個我係用黎玩下,我想limit users to enter only one characterchar getOneChar() { char c; while(true) { cin >> c; if (c>cin.get() ) return c; cout << "Too many characters! " "Please input again.\n"; cin.ignore(200, '\n'); } } int main(){... 顯示更多 呢個我係用黎玩下,我想limit users to enter only one character char getOneChar() { char c; while(true) { cin >> c; if (c>cin.get() ) return c; cout << "Too many characters! " "Please input again.\n"; cin.ignore(200, '\n'); } } int main(){ cout<
最佳解答:
1. cin >> c; 呢個statement唔係解只係入一個字完咩? 即係如果 c 只係字元, 就算你打 "ppp" 呢種字串,佢都係睇第一個字元 i.e. "ppp" c會變成 'p' "pqr" c會變成 'p' "abc" c會變成 'a' 2. 仲有,字串唔會用 ' '....係用 " " !!!! 3. cin.ignore好似要 c 係字串先用到,即係你用完等於無用. 你第一個數就係200,就算係字串,會唔會去到200咁多. 4. c>cin.get() 我唔明你呢個想點, cin.get()呢個function有好多用法,但係唔係俾你咁用. 你可以: c = cin.get(); or cin.get(c) 我唔明你 cin.get(); 想點, 你咁樣即係要求輸入,不過無話input到邊到. 5. 你一係咁做,最簡單 char getOneChar() { char c; cin >> c; return c; } 如果你想一定要同一講 "Too many characters! "同"Please input again.\n"; 一係就咁 char getOneChar() { char c[20]; int t; while(1){ cin >> c; if (c[1]=='\0'){ return c[0]; }else { cout << "Too many characters! Please input again.\n" ; /*重洗 c */ for(t=0;t<20;t++) c[t] == '/0'; } } }
此文章來自奇摩知識+如有不便請留言告知
其他解答:文章標籤
全站熱搜
留言列表