c++操作1运算符、操作2的未知值
c++ Unknown values for operand1 operatr, operand2
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void getName ( string first1_name, string last1_name, string first2_name, string last2_name );
string getLectureSection ( );
void getLabSection ( string lab_section1, string lab_section2 );
void printIdInfo ( ostream&out, string first1_name, string last1_name, string first2_name,
string last2_name, string CLASS, string lecture_section,
string lab_section1, string lab_section2, string DUE_DATE );
void readExpression ( ifstream&, double operand1, char operatr, double operand2 );
void echoExpression ( ostream&out, double operand1, char operatr, double operand2 );
void evaluateExpression ( ostream&out, double operand1, char operatr, double operand2 );
void writeFileLocation ( );
int main()
{
const string CLASS = "C.S.1428",
DUE_DATE = "11/15/17";
string first1_name,
last1_name,
first2_name,
last2_name,
lecture_section,
lab_section1,
lab_section2;
double operand1,
operand2;
char operatr;
ifstream fin;
fin.open("prog5_002in.txt");
if(!fin)
{
cout << "Program Terminated." << endl
<< "Input file failed to open!" << endl;
return 1;
}
ofstream fout;
fout.open("prog5_002out.txt");
if(!fout)
{
cout << "Program Terminated." << endl
<< "Output file failed to open!" << endl;
return 2;
}
getName ( first1_name, last1_name, first2_name, last2_name );
lecture_section = getLectureSection ( );
getLabSection ( lab_section1, lab_section2 );
printIdInfo ( fout, first1_name, last1_name, first2_name, last2_name,
CLASS, lecture_section,
lab_section1, lab_section2, DUE_DATE );
readExpression ( fin, operand1, operatr, operand2 );
while ( operatr != '?' )
{
echoExpression ( fout, operand1, operatr, operand2 );
evaluateExpression ( fout, operand1, operatr, operand2 );
readExpression ( fin, operand1, operatr, operand2 );
}
cout << endl << endl;
printIdInfo ( cout, first1_name, last1_name, first2_name, last2_name,
CLASS, lecture_section, lab_section1, lab_section2, DUE_DATE );
writeFileLocation ( );
fin.close();
fout.close();
system("PAUSE>NUL");
return 0;
}
void getName(string first1_name, string last1_name, string first2_name, string last2_name)
{
cout << "Enter your first name: ";
cin >> first1_name;
cout << endl << "Enter your last name: ";
cin >> last1_name;
cout << endl << endl;
cout << "Enter your first name: ";
cin >> first2_name;
cout << endl << "Enter your last name: ";
cin >> last2_name;
cout << endl << endl;
}
string getLectureSection()
{
string lecture_section;
cout << "Enter your three digit lecture section: ";
cin >> lecture_section;
cout << endl << endl;
return lecture_section;
}
void getLabSection(string lab_section1, string lab_section2)
{
cout << "Enter your two digit lab section number: ";
cin >> lab_section1;
cout << endl;
cout << "Enter your two digit lab section number: ";
cin >> lab_section2;
}
void printIdInfo ( ostream&out, string first1_name, string last1_name, string first2_name,
string last2_name, string CLASS,string lecture_section,string lab_section1,
string lab_section2,string DUE_DATE )
{
out << first1_name << " " << last1_name << " & "
<< first2_name << " " << last2_name << endl
<< CLASS << lecture_section << endl
<< "Lab Section: " << "L" << lab_section1
<< "L" << lab_section2 << endl
<< DUE_DATE << endl << endl;
}
void readExpression (ifstream &fin, double operand1, char operatr, double operand2)
{
fin >> operand1, operatr, operand2;
}
void echoExpression(ostream&fout, double operand1,char operatr, double operand2)
{
fout << operand1 << operatr << operand2;
}
void evaluateExpression(ostream&fout, double operand1, char operatr, double operand2)
{
double expression;
switch (operatr)
{
case '+' :
expression = (operand1 + operand2);
fout << " = " << expression;
break;
case '-' :
expression = (operand1 - operand2);
fout << " = " << expression;
break;
case '*' :
expression = (operand1 * operand2);
fout << " = " << expression;
break;
case '/' :
if (operand2 == 0)
{
fout << endl << "Division by zero produces an "
<< "undefined result." << endl;
}
else
{
expression = (operand1 / operand2);
fout << " = " << setprecision(1) << fixed << expression;
}
break;
default :
fout << operand1 << " " << operatr << " " << operand2;
fout << endl << "Encountered unknown operator.";
}
}
void writeFileLocation()
{
cout << "Program results have been written to Prog5_002out.txt";
}
一般来说,我对编码很陌生,尤其是c++,这是我在课堂上为一项额外的学分作业而编写的程序。在大多数情况下,我已经运行了它,但我不知道如何显示printIdInfo函数中的语句,因为我不确定如何让它保持开放状态,一次调用fout,然后一次调用cout。所以我还没能让cout呼叫正常工作,但它对fout起到了半作用。
我也不认为我对readExpression的函数调用做了任何远程正确的事情,我似乎没有正确地从输入文件中找到operation1、operator和operation2。目前,输出文件循环(无限期?)只是显示遇到的未知运算符,我希望它在$符号的情况下这样做。但我真的不确定我错在了什么地方。
这不是真正的"到期",但我可以在周五之前交上它以获得奖励积分。我在某个地方被难住了,我想这只是因为我冲过去了,但任何建议都将不胜感激。非常感谢。
(Input File)
123.5 + 59.3
198.7 / -26
125 $ 28
89.3 * 2.5
178.9 - 326.8
198.7 / 0
34.0 ? 10
您的readExpression
函数可以正确地从fin
读取,但不会对结果执行任何操作。您将readExpression
的签名定义为:
void readExpression(std::ifstream &, double, char, double);
通过传递对该参数的引用(使用&
),您对std::ifstream
做了正确的操作。但是,您将按值传递最后三个参数,这本质上意味着将创建每个参数的临时副本,以便在函数中使用。因此,您正在从fin
中读取结果,并将结果放入临时变量中,当函数退出时,这些临时变量将被删除。如果您希望函数修改最后三个参数的内容(类似于它修改std::ifstream
的方式),请通过引用传递它们。将签名更改为:
void readExpression(std::ifstream &, double &, char &, double &);
然后,在调用readExpression(fin, operand1, operatr, operand2)
之后,您应该会发现operand1
、operatr
和operand2
包含您希望它们包含的内容。
有关参考文献的更多信息,请参阅维基百科的这篇文章。
附言,尽可能避免using namespace std
。
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 继承期间显示未知行为的子类
- 重载操作程序时出错>>用于类中的字符串 memebr
- 输入中的字符串数未知(以字母表示)
- 对字符串进行位操作
- 具有未知值时的时间复杂性
- 我可以在 C++ 中的函数体之外进行操作吗?
- MPI突然停止了对多个核心的操作
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 对字符数组中的元素执行逐位操作
- 如何在directx/c++中进行平移/缩放操作
- 链表中写入访问冲突的未知原因
- 正在生成未知类实例
- c++操作1运算符、操作2的未知值
- 在测试网上部署智能合约时合约错误中的未知操作 hi
- 指针操作的使用未知
- 取模操作是否更快,功率为 2,即使在编译时该值未知
- GCC 内联 ASM,未知的 SSE 操作码
- g++ 4.6.1 编译器错误:错误:未知伪操作:".cfi_personality"
- C++DLL与C#的互操作:公开未知大小的多维数组和内存管理