亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

vc++的cctype頭文件里有isdigit這個函數,是判斷輸入是不是數字嗎?

vc++的cctype頭文件里有isdigit這個函數,是判斷輸入是不是數字嗎?

C++
精慕HU 2023-03-18 17:13:27
#include <iostream>#include <cctype>int main(){using namespace std;int ch;double tvarps;cout << "your tvarp: " << endl;while (cin >> ch && ch > 0 ){if(ch <= 5000 && ch >0)  tvarps = 0.0;else if(ch > 5000 && ch <= 15000)tvarps = ch * 0.1;  else if(ch > 15000 && ch <=35000 )tvarps = (ch - 15000)*0.15 + 1000;else if(ch > 35000)tvarps = (ch - 35000)*0.2 + 4000;cout << "tvarps: " << tvarps << endl;}return 0;}
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

在while前加char temp[200];
把while(...)改成while(cin>>temp);
循環里第一行寫if (!isdigit(temp))break;
第二行寫ch = atoi(temp); //把字符串變成整數

以下是我做的isdigit(),僅供參考
bool isdigit(char *buf)
{
int t = 0;
if (buf[t] == '-' || buf[t] == '+')
if (buf[1] != '\0')
t ++;
for (;buf[t] != '\0'; t ++)
if (buf[t] < '0' || buf[t] > '9')
return false;
return true;
}

查看完整回答
反對 回復 2023-03-21
  • 1 回答
  • 0 關注
  • 120 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號