围绕变量“方程”进行堆栈;已损坏
我正在尝试用 VC++ 制作一个计算器,即使它运行,它也会不断读取我没有告诉它的内存,而且我不知道如何让它停止。
#include <iostream>
#include <ctype.h>
int main(){
char equation[4];
equation[3] = '\0'; //string terminator
int result;
bool wantsToContinue = true;
char yesOrNo;
equationPrompt:
std::cout << "Enter Equation: ";
std::cin >> equation;
while(wantsToContinue){
switch(equation[1]){
case '+':
result = int(equation[0]) + int(equation[2]);
break;
case '-':
result = int(equation[0]) - int(equation[2]);
break;
case '*':
result = int(equation[0]) * int(equation[2]);
break;
case '/':
result = int(equation[0]) / int(equation[2]);
break;
}
std::cout << std::endl << "Your answer is " << result << std::endl;
exitPrompt:
std::cout << "Exit? Y/N: ";
std::cin >> yesOrNo;
if(tolower(yesOrNo) == 'n'){
wantsToContinue = true;
goto equationPrompt;
}
else if (tolower(yesOrNo) == 'y')
wantsToContinue = false;
else{
std::cout << std::endl << "Unknown response." << std::endl;
goto exitPrompt;
}
}
return 0;
}
I'm trying to make a calculator in VC++ and even though it runs, it keeps reading memory that I haven't told it to, and I don't know how to make it stop.
#include <iostream>
#include <ctype.h>
int main(){
char equation[4];
equation[3] = '\0'; //string terminator
int result;
bool wantsToContinue = true;
char yesOrNo;
equationPrompt:
std::cout << "Enter Equation: ";
std::cin >> equation;
while(wantsToContinue){
switch(equation[1]){
case '+':
result = int(equation[0]) + int(equation[2]);
break;
case '-':
result = int(equation[0]) - int(equation[2]);
break;
case '*':
result = int(equation[0]) * int(equation[2]);
break;
case '/':
result = int(equation[0]) / int(equation[2]);
break;
}
std::cout << std::endl << "Your answer is " << result << std::endl;
exitPrompt:
std::cout << "Exit? Y/N: ";
std::cin >> yesOrNo;
if(tolower(yesOrNo) == 'n'){
wantsToContinue = true;
goto equationPrompt;
}
else if (tolower(yesOrNo) == 'y')
wantsToContinue = false;
else{
std::cout << std::endl << "Unknown response." << std::endl;
goto exitPrompt;
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过不编写 C 和 C++ 的神秘弗兰肯斯坦语言组合来阻止它,而是使用真正的 C++ 字符串类型:
请注意,几乎可以保证
int(equation[0])
不是您想象的那样。您想要的是类似int x = std::atoi(equation[0]);
或std::strtol()
的东西,但这仅适用于单个数字。可能更简单的是仅流式传输到整数,它执行实际的文本到整数的转换:You make it stop by not writing an arcane Frankenstein language mix of C and C++, but instead using real C++ string types:
Note that
int(equation[0])
is almost guaranteed not to be what you think. What you want is something likeint x = std::atoi(equation[0]);
orstd::strtol()
, but that only works for single digits. Probably much simpler to just stream into an integer, which performs an actual text-to-integer conversion:equation
是一个由 4 个char
组成的数组。将任意长的字符串读入该数组。输入太多,就会溢出,占用相邻的内存。
正如 @Kerrek SB 所说,你最好使用 std::string ,它不存在这个问题。
equation
is an array of 4char
s.reads an arbitrarily long string into that array. Type too much, and it will overflow, stepping on adjacent memory.
As @Kerrek SB says, you're better off using
std::string
, which doesn't have that problem.