简单计算器尝试重新学习C++.左值错误
Simple Calculator trying to relearn C++. lvalue error
尝试制作一个简单的计算器。我一直在努力让我的.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
只是在行的末尾打印一个新行字符。
相关文章:
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 神经网络不学习.卡在50%
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何将经过训练的机器学习模型保存在python中并将其加载到C++中进行预测?
- 学习 c++,编译错误"no matching function for call"
- 学习数据结构和算法的简单方法
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- 学习嵌套循环C++与示例混淆
- 从书本中学习C++无法使该程序与类一起工作
- 刚开始学习C++
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- 学习时空复杂性时语句XYZ的含义是什么?
- Q学习扫雷行为
- 我可以在没有C++的情况下学习 Win32 API(仅使用 C)吗?
- 学习C,我的编译器已经知道bool(Visual Studio 2017)
- 我正在学习C++,我不能使用指针访问参考吗?(举个例子)
- 试图学习指针,为什么要使用它们而不是仅仅使用&?
- Microsoft CNTK加强学习C 示例
- 与机器学习和小数据库进行分类