admin管理员组文章数量:1122832
This C++ program i have written keeps crashing when I run it.
#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();
}
}
}
After I give it an option, it just waits... ... then crashes.
Any help?
This C++ program i have written keeps crashing when I run it.
#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();
}
}
}
After I give it an option, it just waits... ... then crashes.
Any help?
Share Improve this question edited Nov 22, 2024 at 18:42 genpfault 52.1k12 gold badges91 silver badges147 bronze badges asked Nov 22, 2024 at 17:08 Hector RobertsonHector Robertson 31 silver badge1 bronze badge 16 | Show 11 more comments1 Answer
Reset to default 2- You are trying to dereference the uninitialized pointers, such as:
cin >> *cppfile;
cin >> *outputfile;
To fix this, change the type of these variables to "std::string" objects and use "std::cin" to directly assign values to them.
- Change opt from "char*" to a "char" type.
- Replace the double quotes in the comparison with "opt" to single quotes:
if (opt == "1" || opt == "a" || opt == "A")
to
if (opt == '1' || opt == 'a' || opt == 'A')
- Include the string library to ensure the program works properly:
#include "string"
本文标签: cHow to fix cpp random crashesStack Overflow
版权声明:本文标题:c++ - How to fix cpp random crashes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302089a1931410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
string *cppfile;
is a pointer to astd::string
, which you never allocate. Start by changing all pointer declaration likestring *cppfile;
tostring cppfile;
and report back, egstring outputfile;
andchar opt;
and any others. – Richard Critten Commented Nov 22, 2024 at 17:11(opt == "1" || opt == "a" || opt == "A")
will never be true. You are comparing pointers. Pointers are not what they point to. – Avi Berger Commented Nov 22, 2024 at 17:14char *opt; cin >> opt;
Boom. Don't use pointers until you understand them and have a use for them.opt
is not initialized. Attempting to use it like this is death to your program. Also, as of C++20, this precise overload of operator>> has been changed and this form is no longer valid. Possibly you wantchar opt; std::cin >> opt;
– Avi Berger Commented Nov 22, 2024 at 17:25