C++問題~~~~~到底幾時的數字要' ' 幾時不要

2008-10-26 9:23 am
到底幾時的數字要' ' 幾時不要

回答 (2)

2008-10-26 8:30 pm
✔ 最佳答案
' '表示字元
'1'表示字元1
而1表示數目1
'H'表示字元H
而H表示數目H <--呢個有問題,因為H唔係數目
例如
#include <iostream>
using namespace std;

int main(){
char inp; //建立字元變數
cout << "請輸入一些英文" <<endl;
cin >> inp;
// if(inp == 1){ //錯誤!!!1是字元,所以呢個有問題
if(inp == '1'){ //呢個sin至arm
cout << "hohoho";
}
system("pause"); //暫停命令
}
如果你想要嘅變數係儲存數字哥隻:
#include <iostream>
using namespace std;

int main(){
int inp; //建立儲存數字嘅變數
cout << "請輸入一些數字" <<endl;
cin >> inp;
// if(inp == '1'){ //錯誤!!!1是數字,所以呢個有問題
if(inp == 1){ //呢個sin至arm
cout << "hohoho";
}
system("pause"); //暫停命令
}
參考: 電腦高手(本人)
2008-10-26 10:50 am
2 is an integer expression, you can do arithmetic with it. For example:
printf("%d\n",2+6); // will print 8

'2' is a character. You can do character arithmetic if you know the ASCII table of character representations. For example, '2' has an ASCII value of 50, 'a' is 97, and 'A' is 65. You can use this fact to convert from character digits to numbers, or upper to lower case conversions. Try:
printf("%d\n",'2'-'0'); // will print 2 as an integer
printf("%c\n",'A'+32); // will print the lowercase character a

If you need more details, post your question, or PM me..


收錄日期: 2021-04-13 16:11:35
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081026000051KK00159

檢視 Wayback Machine 備份