我的计算器程序C ++出错了

时间:2015-07-14 19:11:42

标签: c++ syntax-error calculator

我试图用C ++为我的第一个项目制作一个计算器,但是我收到了一个错误。

这是我的代码:

#include <iostream>
#include <cmath>

using namespace std;

int a;
int b;
int sum;
int opp;
int yn;


int main()
{

    cout << "Enter your first value" << endl;
    cin >> a;

    cout << "Type the number that corrosponds to your opperation \n 1.add \n 2.subtract \n 3.mutipliy \n 4.divide" << endl;
    cin >> opp;

    cout << "Choose your last value" << endl;
    cin >> b;

    return 0;

}

int action()
{

    switch(opp)
    {
    case 1:
        sum = a + b;
        cout << "Your answer is " << sum << endl;
        break;

    case 2:
        sum = a - b;
        cout << "Your answer is " << sum << endl;
        break;

    case 3:
        sum = a * b;
        cout << "Your answer is " << sum << endl;
        break;

    case 4:
        sum = a / b;
        cout << "Your answer is " << sum << endl;
        break;

    case >> 5:
        cout << "Type the number that corrosponds to your opperation \n 1.add \n 2.subtract \n 3.mutipliy \n 4.divide"
    }

    cout << "Would you like to use another oppperation? \n Type the number that relates to the answer you want to choose \n 1.Yes \n 2.No" << endl;
    cin >> yn;

    if (yn = 1)
    {

    cout << "what would you like your nxt opperation to be? \n 1.add \n 2.subtract \n 3.mutiply \n 4.divide" << endl;
        cin >> opp;

        return 0;
    }
}

我知道这不是最好的书面代码,但我更专注于让它发挥作用。

如果它有助于我得到的错误代码是:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>  calculator.cpp
1>c:\users\"my acount"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(12): error C2365: 'yn' : redefinition; previous definition was 'function'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\math.h(1002) : see declaration of 'yn'
1>c:\users\"my account"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(56): error C2059: syntax error : '>>'
1>c:\users\"my account"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(61): error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double (__cdecl *)(int,double)' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(485): could be 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_streambuf<char,std::char_traits<char>> *)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(466): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(void *&)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(448): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long double &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(430): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(double &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(411): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(float &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(392): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned __int64 &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(373): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(__int64 &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(353): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned long &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(335): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(317): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned int &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(291): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(int &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(272): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned short &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(237): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(short &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(218): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::_Bool &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(211): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(204): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_ios<char,std::char_traits<char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(198): or       'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_istream<char,std::char_traits<char>> &(__cdecl *)(std::basic_istream<char,std::char_traits<char>> &))'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1103): or       'std::basic_istream<char,std::char_traits<char>> &std::operator >><char,std::char_traits<char>,double(int,double)>(std::basic_istream<char,std::char_traits<char>> &&,_Ty (__cdecl &))'
1>          with
1>          [
1>              _Ty=double (int,double)
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1093): or       'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsigned char &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1086): or       'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsigned char *)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1079): or       'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed char &)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1072): or       'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed char *)'
1>          while trying to match the argument list '(std::istream, double (__cdecl *)(int,double))'
1>c:\users\"my account"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(63): error C2659: '=' : function as left operand
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果您可以尝试解释答案,那么我可以从中学习,因为这是我的第一个项目。

3 个答案:

答案 0 :(得分:10)

  1. int yn;
  2. 此处您重新声明了yn,因为math.h已经有一个名为yn的变量:

    extern double yn _PARAMS((int, double));
    

    重命名yn

    1. case >> 5:错误,请将其更改为case 5:

    2. if (yn = 1)将始终为true,因为它是一个赋值操作,而不是比较运算符(==)。将其重写为if (yn == 1)

答案 1 :(得分:4)

根据错误消息,您的<cmath>定义(或包含定义的内容)名为yn的内容。

由于您将所有内容从std::内部移动到全局命名空间(使用using namespace std;),因此ynyn内的std::发生冲突}。

解决方案:停止使用using namespace std;

答案 2 :(得分:2)

好吧,让我们只读取编译器的输出。

  

\ calculator.cpp(12):错误C2365:'yn':重新定义;      以前的定义是'功能'          \ include \ math.h(1002):见'yn'的声明

第一行告诉我们已经有'yn'的定义 正在使用。那第二行 告诉我们在哪里:在您包含的math.h文件中。要解决此问题,请选择 你的main.cpp文件中'yn'的另一个名字(或者你可以删除“using namespace std”行,并在每次调用cout的开头附加“std ::”等等,但我不认为你知道在这个阶段真的会发生什么吗?

  

\ calculator.cpp(56):错误C2059:语法错误:'&gt;&gt;'

这只是说语法不正确,理所当然。拿那些&lt;&lt;超出你的最后一个“案例”,并让它看起来像你之前做的每一个案例,例如“案例5:”这只是一个简单的错字。

  

\ calculator.cpp(61):错误C2679:二进制'&gt;&gt;' :没有找到哪个运算符采用'double(__ cdecl *)(int,double)'类型的右手操作数(或者没有可接受的转换)

我认为这也是重新定义“yn”的结果,它将“yn”视为你不想要它的东西,而且对于&gt;&gt;它没有意义。要在其上使用的运算符。例如,您可能会在字符串上使用它,您可能知道。

之后看起来它列出了你可以使用它的所有方式,这对你来说并不是很重要。

  

\ calculator.cpp(63):错误C2659:'=':作为左操作数的函数

这里你误认为比较运算符(==)的赋值运算符(=)。前者按照您的方式分配值,后者生成一个“true”或“false”的布尔值,用于if语句。你想要后者,所以用“==”替换“=”。

学习阅读编译器的输出。它实际上说有用的东西,你只需要学会使用它。