【问题标题】:Borland C++ 5.02 cout not showing on the console windowBorland C++ 5.02 cout 未显示在控制台窗口上
【发布时间】:2016-11-06 07:22:59
【问题描述】:

对于这个新手问题,我很抱歉, 我尝试使用 borland 5.02 制作这个程序,但是由于某种原因,当我写结婚时,如果(stat)中的 cout 没有显示在控制台窗口上。我不知道出了什么问题,我已经被困了好几个小时了。请帮帮我

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

int main()
{
   int NIP, GOL, GP, TI, TA, JA, TG ;
   char NM[20], STAT[10] ;

   cout << "ID Number : " ;
   cin >> NIP ;

   cout << "Name : " ;
   cin >> NM ;

   cout << "Faction : " ;
   cin >> GOL ;

   if (GOL == 1)
   {
    GP = 1500000 ;
   }
   else if (GOL == 2)
   {
    GP = 2000000 ;
   }
   else
   {
    GP = 2500000 ;
   }

   cout << "Status  : " ;
   gets (STAT) ;

   if (STAT == "Married" || STAT == "married")
   {
      cout << "Number of children : " << endl ;
      cin >> JA ;
      TI = 0.05 * GP ;

      if (JA <= 3)
      {
        TA = 0.02 * GP * JA ;
      }
      else
      {
        TA = 0.02 * GP * 3 ;
      }
   }
   else
   {
    TI = 0 ;
    TA = 0 ;
   }

   TG = GP + TI+ TA ;

   cout << endl << "-Results-" << endl ;
   cout << "Your GP: " << GP << endl ;
   cout << "Your TI: " << TI << endl ;
   cout << "Your TA: " << TA << endl ;
   cout << "Your TG: " << TG << endl ;

   getch () ;

}

更新:我之前曾尝试将gets(STAT) ; 更改为cin &gt;&gt; STAT ;,但似乎没有任何效果。当我运行它们时,程序看起来像这样


身份证号码:0123141421

名称:维克蒙

阵营:1

状态:已婚(这是问题所在)

-结果-

您的全科医生:1500000

你的 TI:0

你的助教:0

你的TG:1500000


即使我在状态上写了结婚,cout &lt;&lt; "Number of children : " &lt;&lt; endl ; 并没有出现在控制台窗口上。好像if (STAT == "Married" || STAT == "married") 不起作用,并且“状态:已婚”算作

else
   {
    TI = 0 ;
    TA = 0 ;
   }

【问题讨论】:

  • STAT == "Married" 不是比较2个char数组的方法
  • (gets() 不推荐使用,因为它没有提供防止缓冲区溢出的方法。局部变量的 CAPS 名称是 - 不寻常的。)你试图找出发生了什么?与调试器交朋友。 cout &lt;&lt; '|' &lt;&lt; STAT &lt;&lt; '|' &lt;&lt; endl; 紧跟在 std::cin.get(STAT, sizeof STAT); 之后。试试"Married" == STAT 和"Married" == string(STAT)。
  • 修复了 main 声明:C++ 从来没有“隐式 int”。

标签: c++ console-application borland-c++


【解决方案1】:

如果STAT 是std::string,STAT == "Married" 将在标准 C++ 中工作。

但是,它是char 的数组,这意味着您正在比较两个指针。因为 C++ 不支持数组的直接比较。所以这两个数组表达式中的每一个都衰减为指向第一项的指针。

而且这些指针保证是不同的。


注 1:Borland C++ 5.02 听起来像是 1990 年代中期,在第一个 C++ 标准之前。有许多免费的现代编译器。最知名的三个是g++、clang和Visual C++(后者仅适用于PC平台)。

注 2:我记得 Borland C++ 中的 std::string 完全是拙劣的。如果仅使用 std::string 不起作用,请考虑使用 strcmp 来比较 C 字符串(就像您拥有的数组一样)。

注 3:在标准 C++ 中(自 1998 年第一个标准以来)没有 &lt;iostream.h&gt; 标头。而是包含&lt;iostream&gt;,并可能添加using namespace std;。或适当的using 指令,或cout 和endl 等名称的限定,即编写std::cout 和std::endl。


在其他新闻中:

  • 通过为宏保留 SHOUTCASE 名称,您可以显着简化代码阅读体验,并避免启动时出现意外的文本替换。更不用说遵守这方面的共同约定了。

  • 通过使用getline 而不是&gt;&gt;,程序可以读取其中包含空格的名称。但是,这仅在输入缓冲区为空时才有效(因为getline 不会跳过空格)。所以,这是需要考虑的事情,但它可能涉及一些工作。

【讨论】:

    【解决方案2】:

    我尝试以不同的方式编写代码,并且不知何故它正在工作。但是我仍然不知道以前的代码问题的根源。

    #include <stdio.h>
    #include <conio.h>
    #include <iostream.h>
    
    main()
    {
        char NM [20], STAT ;
        int NIP, GOL, GP, JA, TI, TA, TG ;
    
        cout << "ID Number: " ;
        cin >> NIP ;
    
        cout << "Name: " ;
        gets (NM) ;
    
        cout << "Faction: " ;
        cin >> GOL ;
    
        if (GOL == 1)
            GP = 1500000 ;
        else if (GOL == 2)
            GP = 2000000 ;
        else
            GP = 2500000 ;
    
        cout << "Status: " ;
        cin >> STAT ;
    
        if (STAT == 'K')
        {
            TI = 0.05 * GP ;
            cout << "Number of children: " ;
            cin >> JA ;
            if (JA <= 3)
                TA = 0.02 * GP * JA ;
            else
                TA = 0.02 * GP * 3 ;
        }
        else
            TI = 0, TA = 0 ;
    
        TG = GP + TI + TA ;
    
        cout << endl << "-Results-" << endl ;
        cout << "Your GP: " << GP << endl ;
        cout << "Your TI: " << TI << endl ;
        cout << "Your TA: " << TA << endl ;
        cout << "Your TG: " << TG << endl ;
    
        getch () ;
    
    }
    

    【讨论】:

      【解决方案3】:

      我已经修改了你的代码!

      #include <stdio.h>
      #include <conio.h>
      #include <iostream.h>
      
      int main()
      {
         int NIP, GOL, GP, TI, TA, JA, TG ;
         char NM[20];
         string STAT; // FIX 1 --------------------------------------------------------
      
         cout << "ID Number : " ;
         cin >> NIP ;
      
         cout << "Name : " ;
         gets(NM);
      
         cout << "Faction : " ;
         cin >> GOL ;
      
         if (GOL == 1)
         {
          GP = 1500000 ;
         }
         else if (GOL == 2)
         {
          GP = 2000000 ;
         }
         else
         {
          GP = 2500000 ;
         }
      
         cout << "Status  : " ;
         cin>>STAT;
      
         if ((STAT == "Married") || (STAT == "married"))
          {
            cout << "Number of children : " ; // FIX #2 -------------------------------------------------
            cin >> JA ;
            TI = 0.05 * GP ;
      
            if (JA <= 3)
            {
              TA = 0.02 * GP * JA ;
            }
            else
            {
              TA = 0.02 * GP * 3 ;
            }
         }
         else
         {
          TI = 0 ;
          TA = 0 ;
         }
      
         TG = GP + TI+ TA ;
      
         cout << endl << "-Results-" << endl ;
         cout << "Your GP: " << GP << endl ;
         cout << "Your TI: " << TI << endl ;
         cout << "Your TA: " << TA << endl ;
         cout << "Your TG: " << TG << endl ;
      
         getch () ;
      
      }
      

      问题是你不能只在 C++ 中使用 if 语句比较数组 您可以使用 char,然后只使用单个字符,例如 'M' 或者你可以改用字符串,但可能会引起一些警告。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 2010-10-20
        • 1970-01-01
        相关资源
        最近更新 更多