C ++ if语句超过4位数 [英] C++ if statement over 4 digits

查看:114
本文介绍了C ++ if语句超过4位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写程序将四位八进制数转换为十进制数。我必须使用charactes做这个程序。 没有字符串或字符串说我的讲师。任何人没有我怎么能这样做?这里是我的代码:

im writing a program to convert a four digit octal number to a decimal number. I have to do this programs using charactes. no char rays or strings says my lecturer. does anybody no how i can do this? here is my code:

int main() {

  char a = 0;
  char b = 0;
  char c = 0;
  char d = 0;

  cout << "Enter 4 digit octal number ";
  cin >> a >> b >> c >> d;
  if (a - '0' > 7 || b - '0' > 7 || c - '0' > 7 || d - '0' > 7 || !isdigit(a)
      || !isdigit(b) || !isdigit(c) || !isdigit(d)) {
    cout << "Bad data";
  }
  else
    cout << "Decimal form of that number: " << ((a - '0') * 512) + ((b - '0')
        * 64) + ((c - '0') * 8) + (d - '0') << endl;

  return 0;
}


推荐答案

=http://cplusplus.com/reference/iostream/istream/peek/ =nofollow> cin.peek() 检查在读取 a b c 之后的下一个未读字符 d 。如果是八进制数字,则输入错误,否则(即使失败)一切正常。

You can use cin.peek() to check the next unread character after reading a, b, c and d. If it is octal digit, then input is wrong, otherwise (even if it fails) all ok.

这篇关于C ++ if语句超过4位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆