我编写的这个 C++ 程序在运行时不断崩溃。
#include <iostream>
using namespace std;
void compile_program() {
cout << "worked";
string *cppfile;
string *outputfile;
cout << "FILENAME: ";
cin >> *cppfile;
cout << endl << "OUTPUT FILE: ";
cin >> *outputfile;
cout << endl;
string command = "g++ " + *cppfile + " -o " + *outputfile;
system(command.c_str());
}
int main() {
while (true) {
cout << "Welcome to the program loader." << endl;
cout << "OPTION 1: Compile program" << endl;
cout << "OPTION 2: Run program" << endl;
cout << "> ";
char *opt;
cin >> opt;
cout << endl;
if (opt == "1" || opt == "a" || opt == "A") {
compile_program();
} else if (opt == "2" || opt == "b" || opt == "B") {
// run_program();
}
}
}
当我给它一个选项后,它就只是等待...... ...然后崩溃。
有什么帮助吗?
cin >> *cppfile;
cin >> *输出文件;
要解决此问题,请将这些变量的类型更改为“std::string”对象,并使用“std::cin”直接为其赋值。
if (opt == "1" || opt == "a" || opt == "A")
到
if (opt == '1' || opt == 'a' || opt == 'A')
#include“字符串”