C++ 无法在C++;
现在,在这个问题被标记为重复之前。我已经看过了C++的大部分问题和它们的相关答案。这些是我尝试过的链接,但没有一个适合我。可能是因为他们使用了C++的旧版本,我有C++的最新版本。以下是我尝试过的链接:C++ 无法在C++;,c++,visual-c++,C++,Visual C++,现在,在这个问题被标记为重复之前。我已经看过了C++的大部分问题和它们的相关答案。这些是我尝试过的链接,但没有一个适合我。可能是因为他们使用了C++的旧版本,我有C++的最新版本。以下是我尝试过的链接: 现在,把复制品放在一边。我正在做一个表达式计算器。因此,例如,如果用户输入是:2+2*6*9/9,那么输出应该是14 我怀疑问题所在的代码是: #include <iostream> #include <vector> using std::cout; us
我怀疑问题所在的代码是:
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::string;
using std::vector;
void clear();
void error(string message);
int main() {
cout << "Enter an expression: ";
double l_Value = 0, r_Value = 0, result = 0, count = 0, previous_number;
char op;
while (cin >> l_Value) { // 1+2*3+6-4/2+3
if (!cin) {
error("Invalid operand entered!");
}
else {
bool is_Error = 0; // false
vector<double> numbers;
numbers.push_back(l_Value);
previous_number = l_Value;
while (cin >> op) {
if (op == '\0') {
break;
}
cin >> r_Value;
switch (op)
{
case '+':
numbers.push_back(r_Value);
previous_number = r_Value;
break;
case '-':
numbers.push_back((-1 * r_Value));
previous_number = (-1 * r_Value);
break;
case '*':
numbers.pop_back(); // take out the number
r_Value *= previous_number;
numbers.push_back(r_Value);
previous_number = r_Value;
break;
case '/':
if (r_Value == 0) {
error("Sorry, division by zero has occured. Please re-evaluate your expression!\n");
is_Error = 1; // true
break;
}
else {
numbers.pop_back(); // take out the number
previous_number /= r_Value;
numbers.push_back(previous_number);
break;
}
}
}
if (!is_Error) {
for (int i = 0; i < numbers.size(); i++) {
result += numbers[i];
}
cout << result << '\n';
}
numbers.clear();
result = 0;
l_Value = 0;
r_Value = 0;
}
cout << "Enter an expression: ";
}
clear();
return 0;
}
#包括
#包括
使用std::cout;
使用std::cin;
使用std::string;
使用std::vector;
无效清除();
无效错误(字符串消息);
int main(){
cout>l_值){//1+2*3+6-4/2+3
如果(!cin){
错误(“输入的操作数无效!”);
}
否则{
bool is_Error=0;//false
向量数;
数字。推回(l_值);
上一个_编号=l_值;
而(cin>>op){
如果(op=='\0'){
打破
}
cin>>r_值;
开关(op)
{
格“+”:
数字。推回(r_值);
上一个_数=r_值;
打破
案例'-':
数字。推回((-1*r_值));
前一个_值=(-1*r_值);
打破
案例“*”:
number.pop_back();//取出数字
r_值*=先前的_编号;
数字。推回(r_值);
上一个_数=r_值;
打破
案例“/”:
如果(r_值==0){
错误(“对不起,发生了零除。请重新计算表达式!\n”);
is_Error=1;//true
打破
}
否则{
number.pop_back();//取出数字
前一个_数/=r_值;
数字。推回(上一个数字);
打破
}
}
}
如果(!是_错误){
对于(int i=0;i
这个论点是没有意义的。你可以从cin
读什么,你也可以从std::string
读什么,没有任何区别。你只需要添加一个步骤:
#include <iostream>
#include <string>
#include <sstream>
int main( ){
std::string x;
std::cin >> x;
if (x == "") {
std::cout << "user pressed enter (and nothing else)";
} else {
double y;
std::stringstream ss{x};
ss >> y;
std::cout << y;
}
}
#包括
#包括
#包括
int main(){
std::字符串x;
标准:cin>>x;
如果(x==“”){
std::cout>y;
标准::cout
我避免使用字符串,以避免通过文本进行解析
这个论点是没有意义的。你可以从cin
读什么,你也可以从std::string
读什么,没有任何区别。你只需要添加一个步骤:
#include <iostream>
#include <string>
#include <sstream>
int main( ){
std::string x;
std::cin >> x;
if (x == "") {
std::cout << "user pressed enter (and nothing else)";
} else {
double y;
std::stringstream ss{x};
ss >> y;
std::cout << y;
}
}
#包括
#包括
#包括
int main(){
std::字符串x;
标准:cin>>x;
如果(x==“”){
std::cout>y;
std::cout在外部循环中,您试图读取一个双精度,但您一直按enter键。没有要计算的输入,因此它一直尝试读取双精度。您可以通过使用^Z结束输入流来摆脱它,或者您可以给它任何要读取的实际内容,它将尝试将其转换为双精度(这是您的代码明确告诉它等待的内容)
基本上,当您按enter键时,它会忽略它,因为
从流中提取尽可能多的字符,并将其插入由sb指向的流缓冲区对象(如果有)控制的输出序列,直到输入序列用尽或函数无法插入sb指向的对象为止
试着尝试一下,看看发生了什么
#include <iostream>
#include <vector>
#include <string>
int main() {
double x;
std::cin >> x;
std::cout << "read this value: " << x << std::endl;
// this is what while or if will look at
bool success = !std::cin.fail();
if (success)
std::cout << "success" << std::endl;
else
std::cout << "failure, loop will exit" << std::endl;
return 0;
}
#包括
#包括
#包括
int main(){
双x;
标准:cin>>x;
std::cout在外部循环中,您试图读取一个双精度,但您一直按enter键。没有要计算的输入,因此它一直尝试读取双精度。您可以通过使用^Z结束输入流来摆脱它,或者您可以给它任何要读取的实际内容,它将尝试将其转换为双精度(这是您的代码明确告诉它等待的内容)
基本上,当您按enter键时,它会忽略它,因为
从流中提取尽可能多的字符,并将其插入由sb指向的流缓冲区对象(如果有)控制的输出序列,直到输入序列用尽或函数无法插入sb指向的对象为止
试着尝试一下,看看发生了什么
#include <iostream>
#include <vector>
#include <string>
int main() {
double x;
std::cin >> x;
std::cout << "read this value: " << x << std::endl;
// this is what while or if will look at
bool success = !std::cin.fail();
if (success)
std::cout << "success" << std::endl;
else
std::cout << "failure, loop will exit" << std::endl;
return 0;
}
#包括
#包括
#包括
int main(){
双x;
标准:cin>>x;
std::cout请包含一个太低级的透视图。使用std::getline
一次读取一行,然后使用std::istringstream
解析该行。您没有包含一个,其他人可以复制和粘贴的代码,然后编译并运行以重现您的问题。您发布的代码都不完整(它并不复杂)并且有太多与实际问题无关的内容。请阅读您的问题,但大部分内容我都不理解。要“检测输入”,您只需要std::string x;std::cin>>x;if(x==“”)std::cout在这里,您可以看到编辑历史记录。如果线程中没有对AIK用户的评论,则不会通知用户。请输入