简单计算器尝试重新学习C++.左值错误

Simple Calculator trying to relearn C++. lvalue error

本文关键字:学习 C++ 错误 计算器 新学习 简单      更新时间:2023-10-16

尝试制作一个简单的计算器。我一直在努力让我的.headers和.cpp尽可能地有条理,因为我记得以前这些都很重要。哈哈。我已经砸了大约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:usersshanedocumentsvisual studio 2015projectscalculator2calculator2calculationsfunctions.cpp  38
Severity    Code    Description Project File    Line
Error   C2106   '=': left operand must be l-value   Calculator2 c:usersshanedocumentsvisual studio 2015projectscalculator2calculator2calculationsfunctions.cpp  43
Severity    Code    Description Project File    Line
Error   C2106   '=': left operand must be l-value   Calculator2 c:usersshanedocumentsvisual studio 2015projectscalculator2calculator2calculationsfunctions.cpp  48
Severity    Code    Description Project File    Line
Error   C2106   '=': left operand must be l-value   Calculator2 c:usersshanedocumentsvisual studio 2015projectscalculator2calculator2calculationsfunctions.cpp  53
Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:UsersShaneDocumentsVisual Studio 2015ProjectsCalculator2Calculator2CalculationsFunctions.cpp  38
Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:UsersShaneDocumentsVisual Studio 2015ProjectsCalculator2Calculator2CalculationsFunctions.cpp  43
Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:UsersShaneDocumentsVisual Studio 2015ProjectsCalculator2Calculator2CalculationsFunctions.cpp  48
Severity    Code    Description Project File    Line
Error (active)      expression must be a modifiable lvalue  Calculator2 c:UsersShaneDocumentsVisual Studio 2015ProjectsCalculator2Calculator2CalculationsFunctions.cpp  53

Severity    Code    Description Project File    Line
Error   C2084   function 'int giveResults(int)' already has a body  Calculator2 c:usersshanedocumentsvisual studio 2015projectscalculator2calculator2calculationsfunctions.cpp  59
firstInteger + secondInterger = theAnswer;

这不是它的工作方式,你需要分配给左边的东西:

theAnswer = firstInteger + secondInterger;

我也不确定你认为factor;行(以及其他代码)会实现什么,比如:

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

它当然有效,但与42;一样有用(顺便说一句,它同样有效)。

firstInteger + secondInterger = theAnswer;

应该是

theAnswer = firstInteger + secondInterger;

其他作业也是如此。这是因为在C++中,形式为x + y的表达式是一个右值,也就是说,你不能给它赋值(好吧,除非你重新定义赋值运算符,因为你处理的是基本数据类型,所以这里不能这样做)。

另一个错误出现在giveResults方法中。

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

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

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

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

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