[英]“Principles and Practice Using C++” chapter 6.3.1 code error?
I am working my way through Principles and Practice Using C++
. 我正在通过
Principles and Practice Using C++
工作。 I've been understanding it kind of well but I've hit a wall recently with chapter 6
. 我一直很理解它,但是最近我在
chapter 6
碰壁了。 You are suppose to start writing a calculator program and it gradually becomes more feature rich as you continue on to it. 您假定开始编写计算器程序,并且随着您的继续使用它逐渐变得功能丰富。 It eventually leads into tokens which is confusing the heck out of me.
最终导致产生令牌,这使我感到困惑。
ANYWHO! ANYWHO! my question is that I'm following this code and it isn't working as it is explaining.
我的问题是,我正在遵循此代码,但它无法正常工作。 I've gone over the code against the book several times and it looks similar.
我已经针对这本书多次检查了代码,看起来很像。 The code just keeps taking in lval and not doing anything with it.
该代码只是保持使用lval而不对其进行任何操作。 After 3 cin entries it will just show me whatever lval was first set to.
输入3个cin后,它只会告诉我最初设置的lval。 I'm also not 100% sure on the use of cin >> op in the while loop.
我也不确定在while循环中使用cin >> op。 What makes it stop?
是什么使它停止? When does it know to stop?
什么时候知道停止? The error function doesn't seem to work either.
错误功能似乎也不起作用。 I keep trying to break the program but it doesn't pop up any of the error messages.
我一直试图破坏程序,但没有弹出任何错误消息。
This is just frustrating because I'm learning and It's hard for me to figure my own problems out without a mentor :/ Thanks everyone for your time though! 这只是令人沮丧,因为我正在学习,没有导师,我很难弄清楚自己的问题:/谢谢大家的宝贵时间! incoming code...it's what I have so far
传入代码...这就是我到目前为止
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter an expression (we can handle ""+"",""-"",""*"",""/"")" << endl;
int lval = 0;
int rval;
char op;
/*int res;*/
cin >> lval; //read left most number
if (!cin) error("No first operand");
while (cin >> op) //Repeatedly read operand and right value
{
cin >> rval;
if (!cin) error("No second operand");
switch(op)
{
case '+':
lval += rval; //add: lval = lval + rval
break;
case '-':
lval -=rval;//subtract: lval = lval - rval
break;
case '*':
lval *= rval; //Multiply: lval = lval * rval
break;
case '/':
lval /= rval; //Divide: lval = lval / rval
break;
default:
cout << "Result: " << lval << endl;
keep_window_open();
return 0;
}
}
error("Bad expression");
}
PS I tried using break points to see how to code works line by line, but it starts throwing me into the iostream files and I have no clue how to read those at this point! PS我尝试使用断点来查看如何逐行编码,但是这开始使我陷入iostream文件中,此时我不知道如何阅读这些信息!
It does sort of work. 它确实做了一些工作。 For instance if you introduce the following sequence:
例如,如果您引入以下序列:
3 <enter>
+ <enter>
3 <enter>
d <enter>
3 <enter>
It produces: 它产生:
Result: 6
The reason is cin always expects an enter to finish. 原因是cin总是期望输入完成。 There is also an error in the logic and even when you want to stop the execution you have to introduce an extra dummy value.
逻辑上也有错误,即使您想停止执行,也必须引入一个额外的哑数值。 To solve that you must check the operator before asking for the rval.
为了解决这个问题,您必须在要求rval 之前检查操作员。
EDIT: 编辑:
Probably this would be close to what you want: 可能这将接近您想要的:
#include "iostream"
#include <cstdio>
using namespace std;
int main()
{
cout << "Please enter an expression (we can handle ""+"",""-"",""*"",""/ "")" << endl;
int lval = 0;
int rval;
char op;
/*int res;*/
cin >> lval; //read left most number
if (!cin) printf("No first operand");
while (cin >> op) //Repeatedly read operand and right value
{
if(op != '+' && op != '-' && op != '*' && op != '/')
{
cout << "Result: " << lval << endl;
//keep_window_open();
getchar();
return 0;
}
cin >> rval;
if (!cin) printf("No second operand");
switch(op)
{
case '+':
lval += rval; //add: lval = lval + rval
break;
case '-':
lval -=rval;//subtract: lval = lval - rval
break;
case '*':
lval *= rval; //Multiply: lval = lval * rval
break;
case '/':
lval /= rval; //Divide: lval = lval / rval
break;
}
}
printf("Bad expression");
}
The problem comes with your first outputted line. 问题出在您的第一条输出线上。 Instead of two sets of quotes, you need to escape one set with a backslash, telling C++ to treat the quotes as a character rather than the start or end of a string.
您需要使用反斜杠对一组引号进行转义 ,而不是使用两组引号,告诉C ++将引号视为字符,而不是字符串的开头或结尾。 Also, your while loop seems to test a very odd condition.
另外,您的while循环似乎测试了一个非常奇怪的条件。 I don't know what has gone wrong, but it just continually takes in the operator and (presumably) the code in the while loop never gets executed.
我不知道出了什么问题,但是它只是不断地吸收运算符,(大概)while循环中的代码永远不会执行。 A better code solution is below.
下面是一个更好的代码解决方案。
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter an expression (we can handle \"+\",\"-\",\"*\",\"/\")" << endl;
int lval = 0;
int rval;
int loop = 1;
char op;
/*int res;*/
while (loop == 1) //Repeatedly calculate
{
cout<<"Please enter the first number"<<endl;
cin >> lval; //read left most number
if (!cin) error("No first operand");
cout<<"Please enter operator"<<endl;
cin >> op;
cout<<"Please enter second number"<<endl;
cin >> rval;
if (!cin) error("No second operand");
switch(op)
{
case '+':
lval += rval; //add: lval = lval + rval
break;
case '-':
lval -=rval;//subtract: lval = lval - rval
break;
case '*':
lval *= rval; //Multiply: lval = lval * rval
break;
case '/':
lval /= rval; //Divide: lval = lval / rval
break;
default:
cout << "Result: " << lval << endl;
keep_window_open();
return 0;
}
cout<<"Enter 1 to calculate a new expression, or 0 to exit."<<endl;
cin>>loop;
}
error("Bad expression");
}
Dismiss the default statement, maybe you should not use default
for displaying the result. 关闭默认语句,也许您不应该使用
default
显示结果。 Try this: 尝试这个:
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter an expression (we can handle ""+"",""-"",""*"",""/"")" << endl;
int lval = 0;
int rval;
char op;
/*int res;*/
cin >> lval; //read left most number
if (!cin) error("No first operand");
while (cin >> op) //Repeatedly read operand and right value
{
cin >> rval;
if (!cin) error("No second operand");
switch(op)
{
case '+':
lval += rval; //add: lval = lval + rval
break;
case '-':
lval -=rval;//subtract: lval = lval - rval
break;
case '*':
lval *= rval; //Multiply: lval = lval * rval
break;
case '/':
lval /= rval; //Divide: lval = lval / rval
break;
}
cout << "Result: " << lval << endl;
keep_window_open();
return 0;
}
error("Bad expression");
}
this should work. 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.