检查我的变量在&#34中是否不是整数;如果" c ++中的语句

时间:2014-01-21 18:57:20

标签: c++ if-statement

我编写了c ++代码,这是其中的一部分:

int flag8=1,
     tmp,
     part;
      .
      .
      .
 if(part > 7 || !(int)tmp || tmp < 0){
      cout << "ERROR !!!\n";
      flag8=0;
      break;
    }

如何检查我的tmp变量是否为整数?

如果part>7 or tmp is not integer or tmp<0,我想说if语句为真。 非常感谢你。

编辑: 这是我的全部代码:

#include <iostream>
using namespace std;


int base2toten(int nums);
int base8toten(int num);

////////////////////////////////////////////
/*   FUNCTION: CONVERTING BASE 2 TO 10   */
////////////////////////////////////////////
int base2to10(int nums){
int base2,
    digits,
    base10=0,
    parts,
  powers=1,
  flag2=1;

  cout << "Enter num base 2: ";
  cin >> base2;
  //if(!(int)base2 && base2 < 0 || !(int)base2 && base2 > 1){
  // cout << "OUT OF RANGE"    
  //}

  digits = base2;

  while(digits){ 
    parts = digits % 10;

    if(parts > 1 || !(int)base2 || base2 < 0){
      cout << "The number you have entered is not base 2 !!!\n";
      flag2=0;
      break;
    }

    digits /= 10;
    base10 += powers * parts;
    powers *= 2;    
}

  if(flag2){
    cout << " \"base2\":\t" << base2  << "\n" 
     << "\"base10\":\t" << base10 << endl;
  }
}


///////////////////////////////////////////
/*   FUNCTION: CONVERTING BASE 8 TO 10   */
///////////////////////////////////////////
int base8to10(int num){
  int base8,
    digit,
    base10=0,
    part,
    power=1,
    flag8=1;

  cout << "Enter num base 8: ";
  cin >> base8;

  digit = base8;

  while(digit){
    part = digit % 10;
    if(part > 7 || !(int)(base8) || base8 < 0){
      cout << "The number you have entered is not base 8 !!!\n";
      flag8=0;
      break;
    }

    digit /= 10;
    base10 += power * part;
    power *= 8;
  }
  if(flag8){
    cout << "\"base8\":\t"   << base8  << "\n" 
     << "\"base10\":\t"  << base10 << endl;
  }
}


//////////////////////////////////////////
/*           FUNCTION: MAIN             */
//////////////////////////////////////////
int main(){
  int num,nums,a ,b,ans;

  //  cout << "What base you want to convert?\n"
  cout << "*******************************************************\n"
       << "*            BASE TWO:                2               *\n"
       << "*            BASE EIGHT:              8               *\n"
       << "*            BOTH BASE TWO AND EIGHT: 28              *\n"
       << "*            BOTH BASE EIGHT AND TWO: 82              *\n"
       << "*******************************************************\n";

  cout << "What base you want to convert? ";
  cin >> ans;

  if(ans != 2 && ans != 8 && ans != 28 && ans !=82){
    cout << "Your answer in not acceptable!!!\n";
  }

  else if(ans == 2){
    cout << "**********************************\n"
         << "*     YOU'VE CHOSEN BASE 2       *\n"
         << "**********************************\n";
    base2to10(a);  
  }

  else if(ans == 8){
    cout << "**********************************\n"
     << "*     YOU'VE CHOSEN BASE 8       *\n"
     << "**********************************\n";
    base8to10(b);
  }

  else if(ans == 28){
    cout << "*********************************************\n"
     << "*     YOU'VE CHOSEN BOTH BASE 2 AND 8       *\n"
     << "*********************************************\n";
    base2to10(a);
    base8to10(b);
  }

  else if(ans == 82){
    cout << "*********************************************\n"
     << "*     YOU'VE CHOSEN BOTH BASE 8 AND 2       *\n"
     << "*********************************************\n";
    base8to10(b);
    base2to10(a);
  }

  return 0;
}

2 个答案:

答案 0 :(得分:1)

你的问题很可能要求XY-Problem。由于C ++是一种类型化语言,因此用于声明变量的类型将始终保持不变。

您需要检查输入流的状态,以测试输入是否以正确的格式给出:

int tmp;

std::cin >> tmp;
if(!cin)
{
    // user input was not parsable as integer value ...
}

如果要指定特定的数字输入格式,可以使用流操作符(需要#include <iomanip>):

std::cin >> std::oct >> tmp; // Will parse integer from base 8 input
std::cin >> std::hex >> tmp; // Will parse integer from base 16 input

答案 1 :(得分:0)

这是你想要的一个例子......希望这会有所帮助......

这只是一个示例,在真实应用中使用之前,您可能需要彻底检查并进行修改。

#include<stdio.h>
int main(){
    int n=0;
    char c;
    do{
        c=getchar();
        if(c>='0' && c<='9'){
            n=n*10;
            n+=(c-48);
        }
        else if(c=='\n'){
            break;
        }
        else{
            printf("SOME THING  WRONG");
            break;
        }
    }while(1);
    printf("%d ",n);
    return 0;
}

因为您说的是base8(从0到7),所以使用if(c>='0' && c<='7')

修改if语句