围绕变量“方程”进行堆栈;已损坏

发布于 2024-11-29 15:59:28 字数 1179 浏览 0 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小草泠泠 2024-12-06 15:59:28

您可以通过不编写 C 和 C++ 的神秘弗兰肯斯坦语言组合来阻止它,而是使用真正的 C++ 字符串类型:

#include <string>
#include <istream>
#include <iostream>


int main()
{
  std::string equation;
  std::cin >> equation;

  // now equation[0] is the first character
}

请注意,几乎可以保证 int(equation[0]) 不是您想象的那样。您想要的是类似 int x = std::atoi(equation[0]);std::strtol() 的东西,但这仅适用于单个数字。可能更简单的是仅流式传输到整数,它执行实际的文本到整数的转换:

int x, y;
std::string operand;

std::cin >> x >> operand >> y;

You make it stop by not writing an arcane Frankenstein language mix of C and C++, but instead using real C++ string types:

#include <string>
#include <istream>
#include <iostream>


int main()
{
  std::string equation;
  std::cin >> equation;

  // now equation[0] is the first character
}

Note that int(equation[0]) is almost guaranteed not to be what you think. What you want is something like int x = std::atoi(equation[0]); or std::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:

int x, y;
std::string operand;

std::cin >> x >> operand >> y;
琴流音 2024-12-06 15:59:28

equation 是一个由 4 个 char 组成的数组。

std::cin >> equation;

将任意长的字符串读入该数组。输入太多,就会溢出,占用相邻的内存。

正如 @Kerrek SB 所说,你最好使用 std::string ,它不存在这个问题。

equation is an array of 4 chars.

std::cin >> equation;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文