C++ 博兰C&x2B的scanf+;建设者

C++ 博兰C&x2B的scanf+;建设者,c++,scanf,c++builder,builder,borland-c++,C++,Scanf,C++builder,Builder,Borland C++,我试图用scanf函数检查变量的类型。它适用于devc++(我的输入是int),但不使用Borland。以下是我尝试过的: AnsiString as = Edit1->Text; string b = as.c_str(); int testb = atoi(b.c_str()); if(scanf("%i", &testb)==1){ do sth; } 有什么想法吗 [edit1]从Spektre的评论中删除 我还有一个问题。我的输入值应该看起来像xx xx xxxx,

我试图用scanf函数检查变量的类型。它适用于devc++(我的输入是int),但不使用Borland。以下是我尝试过的:

AnsiString as = Edit1->Text;
string b = as.c_str();

int testb = atoi(b.c_str());

if(scanf("%i", &testb)==1){
do sth;
}
有什么想法吗

[edit1]从Spektre的评论中删除

我还有一个问题。我的输入值应该看起来像
xx xx xxxx
,所以它是一个日期。
我必须检查日、月和年是否为整数。
我试着这样做:

AnsiString a = Edit1->Text;
date = a.c_str();
 if (a==AnsiString().sprintf("%i",atoi(a.SubString(0,2).c_str()))
  && a==AnsiString().sprintf("%i",atoi(a.SubString(3,2).c_str()))
  && a==AnsiString().sprintf("%i",atoi(a.SubString(6,4).c_str())) )
 { 
 //do sth
 }
  • 但它只检查了一天。有人知道为什么吗J.B.20小时前
    • 我是这样做的

      AnsiString s=Edit1->Text; // copy to real AnsiString ... the AnsiStrings inside visual components are not the same ... some functions/operations does not work properly for them
      int e,i,l=s.Length();
      
      for(e=0,i=1;i<=l;) 
       {
       e=1; // assume it is integer
       if (s[i]=='-') i++; // skip first minus sign
        for (;i<=l;i++) // scan all the rest 
         if ((s[i]<'0')||(s[i]>'9')) // if not a digit
          { 
          e=0; // then not an integer
          break; // stop
          }
       break;
       }
      // here e holds true/false if s is valid integer
      
      now you can use safely
      if (e) i=s.ToInt(); else i=0;
      

      你把事情弄得比它应该的复杂得多

      scanf()
      读取STDIN,但GUI进程不使用STDIN进行输入,这就是它不适合您的原因。改用
      sscanf()

      int testb;
      if (sscanf(AnsiString(Edit1->Text).c_str(), "%d", &testb) == 1)
      {
          // do sth ...
      }
      
      int testb;
      if (TryStrToInt(Edit1->Text, testb))
      {
          // do sth ...
      }
      
      或者,使用RTL的
      TryStrToInt()

      int testb;
      if (sscanf(AnsiString(Edit1->Text).c_str(), "%d", &testb) == 1)
      {
          // do sth ...
      }
      
      int testb;
      if (TryStrToInt(Edit1->Text, testb))
      {
          // do sth ...
      }
      
      对于检查日期字符串,您可以使用
      sscanf()
      进行检查:

      int day, month, year;
      if (sscanf(AnsiString(Edit1->Text).c_str(), "%2d-%2d-%4d", &day, &month, &year) == 3)
      {
          // do sth ...
      }
      
      或者使用RTL的
      TryStrToDate()


      当你说它不起作用时,你是什么意思?运行显示的代码时会发生什么?您是否尝试过在调试器中运行并逐行查看发生了什么?为什么要分配给
      testb
      ,然后用
      scanf`调用直接覆盖
      值?您能解释一下这里是如何检查类型的吗?您计算
      testb
      ,然后在scanf中丢弃它的值。我需要检查输入值是数字还是字符,因为我需要一个整数作为输入类型。要转换字符串并验证字符串确实是整数,请使用例如(或者如果您没有)。最好将Edit1输入类型设置为numeric,并使用
      Edit1->Text->ToInt()
      感谢您的解决方案。这在一定程度上起了作用。我还有一个问题。我的输入值应该看起来像xx xx xxxx,所以它是一个日期。我必须检查日、月和年是否为整数。我试着这样做:ansistringa=Edit1->Text;date=a.c_str()如果(a==AnsiString().sprintf(“%i”,atoi(a.SubString(0,2.c_str())&&a==AnsiString().sprintf(“%i”),atoi(a.SubString(3,2.c_str())&&a==AnsiString().sprintf(“%i”,atoi(a.SubString(6,4.c_str())){但它只检查了一天。有人知道为什么吗?@J.B.有两个原因。1.您应该为内部条件添加括号来分隔操作数(&&O)(有时编译器会对multile condition if's if's if's if's not bracked)2。AnsiString在旧的VCL(包括bds2006)上有一个小错误,如果调用
      ..=a.sprintf(…)
      ..=a.SubString(…)
      它也会更改a变量(例如在BCB5中这是正常的,但在BCB6和bds2006中…它有错误)要么将子字符串存储到单独的变量中,然后将它们剪切到后面,要么使用我答案中的第一个选项(在这里,您可以使用
      -
      跳过来硬编码…它看起来也会非常有用)nicer@J.B.同时检查断点
      a.SubString(6,4)的值
      因为AnsiString索引是从1开始的,而不是从0开始的…它不应该是
      a.SubString(7,4)
      (现在不确定,懒得检查它)?