我必须编写一个使用以下功能的程序,但是我不确定如何从此开始。本质上,我希望getValue使用主函数(在这种情况下为cin和cout)中引用的inputstream和outputstream,但是我遇到了很多错误。如果需要,我可以提供头文件。
#include "lab6.h"
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
using namespace std;
double getValue(istream inputStream, ostream outputStream, string promptUser) {
double number;
outputStream << promptUser;
inputStream >> number;
return number;
}
void convertRectToPolar(istream& inputStream, ostream& outputStream) {
double x, y;
string promptUser;
promptUser = "Input the x value: ";
x = getValue(inputStream, outputStream, promptUser);
outputStream << "Input the y value: ";
y = getValue(inputStream, outputStream, promptUser);
if (inputStream.fail()) {
outputStream << "Error: inputStream has failed.";
}
}
int main() {
convertRectToPolar(cin, cout);
}
最佳答案
istream inputStream
和ostream outputStream
无法使用。流不可复制。也做那些引用。
double getValue(istream& inputStream, ostream& outputStream, string promptUser) {
double number;
outputStream << promptUser;
inputStream >> number;
return number;
}
附带说明:在调用getValue
之前,无需将提示字符串分配给变量。只需将提示放入通话中,它将转换为std::string
:void convertRectToPolar(istream& inputStream, ostream& outputStream) {
double x = getValue(inputStream, outputStream, "Input the x value: ");
double y = getValue(inputStream, outputStream, "Input the y value: ");
if (inputStream.fail()) {
outputStream << "Error: inputStream has failed.";
}
}