繁体   English   中英

错误:我的每个函数和变量均未在此范围内声明(代码;;块,C ++)[关闭]

[英]Error: My every function & variable 'was not declared in this scope (code;;blocks, c++) [closed]

我是c ++的新手,我写了我的第一个“真实”程序(一个简单的四运算计算器)。 当我编译它时,我遇到了12个编译器错误。 我能够解决一些问题(缺少分号等),但其中有7个拒绝离开。 错误和我的代码在这里:

错误:

    15 error: 'cout' was not declared in this scope
    15 error: 'endl' was not declared in this scope
    21 error: 'GetNumber' was not declared in this scope
    24 error: 'GetOperator' was not declared in this scope
    29 error: 'nAnswer' was not declared in this scope
    33 error: 'AskContinue' was not declared in this scope
    35 error: 'bContinue' was not declared in this scope

Main.cpp

    //Four-operation calculator
    //© Olli Utriainen 2013

    #include <iostream>
    //Header with functions GetNumber(), GetOperator() and AskContinue()
    #include "calculator.h"

    using namespace std;

    int main()
    {
        //Main loop's condition
        bool bContinue = false;

        cout << "Welcome to Olli's calculator!" << endl << endl;

        //Main loop
       do
        {
            cout << "Give first number" << endl;
            int nNumber1 = GetNumber();

            cout << "Give operator (+,-,*,/)" << endl;
            char cOperator = GetOperator();

            cout << "Give second number" << endl;
            int nNumber2 = GetNumber();

            nAnswer = nNumber1 + nNumber2;

            cout << nNumber1 << " " << cOperator << " " <<
            nNumber2 << " = " << nAnswer << endl;

            bContinue = AskContinue();

        } while (bcontinue);

        return 0;
    }

标头

    //Headers for the calculator
    //© Olli Utriainen 2013

    #ifndef CALCULATOR_H_INCLUDED
    #define CALCULATOR_H_INCLUDED

   //Function for inputting numbers
   int GetNumber()

   //Function for inputting a mathematical operator (+,-,*,/)
   char GetOperator()

   //Function for asking Continue Yes/No
   bool AskContinue()

   #endif // CALCULATOR_H_INCLUDED

Functions.cpp

   //Functions for the calculator
   //© Olli Utriainen 2013

   #include <iostream>

   using namespace std;

   //Function for inputting numbers
   int GetNumber()
   {
       int nNumber

       cin >> nNumber;

       return nNumber
   }

   //Function for inputting a mathematical operator (+,-,*,/)
   int GetOperator()
   {
       char cOperator
       bool bValidOperator = false;

       //Do-while loop for checking if given operator
       //is valid (+,-,*,/). If invalid, bValidOperator
       //is changed to false ->loop will break and the
       //given operator is returned
       do
       {
           cin >> cOperator;

           if (cOperator == '+' || '-' || '*' || '/')
               {
                   bValidOperator = true;
               }
           else
               {
                   bValidOperator = false;
                   cout << "Invalid operator" << endl;
               }


       } while (!bValidOperator)

       return cOperator;

   }

   //Function for asking Continue Yes/No
   bool AskContinue()
   {
       cout << "New calculation? Y/N" << endl;

       bool bContinue = false;
       if (cin >> 'Y' || 'y')
           {
               bContinue = true;
           }
       if (cin >> 'N' || 'n')
           {
               bContinue = false;
           }
       return bcontinue;
   }

标头中的函数声明的分号? 哦,实际上很多分号都丢失了。 确保在需要的地方插入它们,然后重试。

很多错别字; 大量分号丢失;

在Calculator.cpp中,您应该#include“ calculator.h”

在bool AskContinue()中,应为:

{
cout << "New calculation? Y/N" << endl;
bool bContinue = false;
char choice;
cin >> choice;
if ((choice == 'Y')||(choice == 'y'))
{
   bContinue = true;
}
   if ((choice == 'N')||(choice == 'n'))
{
   bContinue = false;
}
   return bContinue;
}

这些是我现在可以找到的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM