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
  • 5 string *cppfile; is a pointer to a std::string, which you never allocate. Start by changing all pointer declaration like string *cppfile; to string cppfile; and report back, eg string outputfile; and char opt; and any others. – Richard Critten Commented Nov 22, 2024 at 17:11
  • 6 (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:14
  • 3 Please go back to the start of your beginners material (books, tutorials, class-notes, etc.) and refresh the basics. – Some programmer dude Commented Nov 22, 2024 at 17:18
  • 4 char *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 want char opt; std::cin >> opt; – Avi Berger Commented Nov 22, 2024 at 17:25
  • 5 fwiw, there is nothing random here. Using "random" for "i dont understand" can lead to confusion and misunderstandings because its actual meaning is distinct from that. You will find it easier to find and fix your problems if you acknowledge what actually happens, your program crashes very reliably. – 463035818_is_not_an_ai Commented Nov 22, 2024 at 17:49
 |  Show 11 more comments

1 Answer 1

Reset to default 2
  1. 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.

  1. Change opt from "char*" to a "char" type.
  2. 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')

  1. Include the string library to ensure the program works properly:

#include "string"

本文标签: cHow to fix cpp random crashesStack Overflow