简单计算器试图重新学习C ++。左值误差

时间:2015-11-18 04:58:29

标签: c++

尝试制作一个简单的计算器。我一直试图尽可能地用我的.headers和.cpps组织起来,因为我记得那些重要的日子。哈哈。我已经破解了大约10个键盘试图让它工作。

CALCULATOR2.CPP
#include "stdafx.h"
#include <iostream>
#include "CalculatorDeclarations.h"
int firstInteger;
int secondInteger;
int theAnswer;
char factor;
int main()
{
    getFirstInteger(firstInteger);
    getSecondInteger(secondInteger);
    getFactor(factor);
    giveResults(theAnswer);
    return 0;
}

CalculatorDeclarations.h
#ifndef ADD_H
#define ADD_H
#include "stdafx.h"
#include <iostream>
int getFirstInteger(int firstInteger);
int getSecondInteger(int secondInterger);
void getFactor(char factor);
int doCalculations(int getFirstInteger, int secondInterger, char getFactor);
int giveResults(int theAnswer);
#endif

CALCULATORSFUNCTIONS.CPP
#include "stdafx.h"
#include <iostream>
#include "CalculatorDeclarations.h";

int giveResults(int theAnswer)
{
    std::cout << theAnswer;
}
int getFirstInteger(int firstInteger)
{
    firstInteger;
    std::cout << "Please enter the first integer you would like to use." <<     std::endl;
    std::cin >> firstInteger;
    return firstInteger;
}

int getSecondInteger(int secondInteger)
{
    secondInteger;
    std::cout << "Please enter the second integer you would like to use." <<     std::endl;
    std::cin >> secondInteger;
    return secondInteger;
}

void getFactor(char factor)                                                                            
{
    factor;
    std::cout << "Select what you would like to use: (+, -, *, /)" <<    std::endl;
    std::cin >> factor;
}

int doCalculations(int firstInteger, int secondInterger, int getFactor)
{
    int theAnswer;
    if (getFactor == '+')
    {
        firstInteger + secondInterger = theAnswer;
        return theAnswer;
    }
    if (getFactor == '-')
    {
        firstInteger - secondInterger = theAnswer;
        return theAnswer;
    }
    if (getFactor == '*')
    {
        firstInteger * secondInterger = theAnswer;
        return theAnswer;
    }
    if (getFactor == '/')
    {
        firstInteger / secondInterger = theAnswer;
        return theAnswer;
    }
}

int giveResults(int theAnswer)
{
    std::cout << "Your answer is:" + theAnswer;
}

我得到的错误:

Severity    Code    Description Project File    Line
Error   C2106   '=': left operand must be l-value   Calculator2 c:\users\shane\documents\visual studio 2015\projects\calculator2\calculator2\calculationsfunctions.cpp  38

Severity    Code    Description Project File    Line
Error   C2106   '=': left operand must be l-value   Calculator2 c:\users\shane\documents\visual studio 2015\projects\calculator2\calculator2\calculationsfunctions.cpp  43

Severity    Code    Description Project File    Line
Error   C2106   '=': left operand must be l-value   Calculator2 c:\users\shane\documents\visual studio 2015\projects\calculator2\calculator2\calculationsfunctions.cpp  48

Severity    Code    Description Project File    Line
Error   C2106   '=': left operand must be l-value   Calculator2 c:\users\shane\documents\visual studio 2015\projects\calculator2\calculator2\calculationsfunctions.cpp  53

Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:\Users\Shane\Documents\Visual Studio 2015\Projects\Calculator2\Calculator2\CalculationsFunctions.cpp  38

Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:\Users\Shane\Documents\Visual Studio 2015\Projects\Calculator2\Calculator2\CalculationsFunctions.cpp  43

Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:\Users\Shane\Documents\Visual Studio 2015\Projects\Calculator2\Calculator2\CalculationsFunctions.cpp  48

Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:\Users\Shane\Documents\Visual Studio 2015\Projects\Calculator2\Calculator2\CalculationsFunctions.cpp  53


Severity    Code    Description Project File    Line
Error   C2084   function 'int giveResults(int)' already has a body  Calculator2 c:\users\shane\documents\visual studio 2015\projects\calculator2\calculator2\calculationsfunctions.cpp  59

3 个答案:

答案 0 :(得分:2)

firstInteger + secondInterger = theAnswer;

这不是它的工作原理,你需要分配到左边的东西:

theAnswer = firstInteger + secondInterger;

我也不确定您认为您将使用factor;行(以及其他代码)在代码中实现的目标:

void getFactor(char factor)
{
    factor;
    // blah blah blah
}

它肯定是有效的,但与42;一样有用(顺便说一句,这同样有效)。

答案 1 :(得分:1)

firstInteger + secondInterger = theAnswer;

应该是

theAnswer = firstInteger + secondInterger;

与其他作业类似。那是因为在C ++中,x + y形式的表达式是一个右值,即你不能赋值给它(好吧,除非你重新定义赋值运算符,这在你处理基本数据类型时不能在这里完成)。 / p>

答案 2 :(得分:0)

另一个错误在于您的giveResults方法。

这是您当前的方法签名和内容:

int giveResults(int theAnswer)
{
    std::cout << "Your answer is:" + theAnswer;
}

其返回类型为int但是,在这种情况下,您不会返回任何内容。您只是尝试将其打印到控制台,返回类型应为void。另外cout不允许使用+连接打印项目,您需要将cout调用更改为:

std::cout << "Your answer is: " << theAnswer << endl;

使用多个<<与使用其他语言中的+打印到控制台相同,而endl只是在结尾处打印一个新行字符。线。