我正在使用以下代码,但它只要求我输入并在不计算我的输入的情况下关闭
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(){
int balance=0;
int withdraw=0;
char* str;
cin.getline(str,10);
cout<<str;
withdraw=atoi(strtok(str," "));
balance=atoi(strtok(NULL," "));
cout<<withdraw<<" "<<balance;
return 0;
}
最佳答案
char* str;
这只是给你一个指针。该指针不指向任何地方,尤其是不指向您可以写入的某些 char
。当您调用 cin.getline(str,10)
时,它会尝试写入此指针指向的位置。这给了你未定义的行为。一个简单的解决方法是使 str
成为 10
char
的数组:
char str[10];
但是,我建议您开始使用 std::string
并找出如何使用 std::string< 执行类似
。提示:看strtok
的操作std::istringstream
.
关于c++ - 程序不计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694890/