0

I keep getting this error and have no idea how to fix it because I don't see anything wrong with my code.

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

#define GREEN "\033[32m"
#define RED   "\033[31m"
#define RESET "\033[0m"

void file_create(std::string name) {
    std::ifstream file(name);
    if (file.is_open()) {
    file.close();
    std::cout << "File already exists..." << std::endl;
    main();
    } 
    else {
    file.close(); std::ofstream newFile(name);
    if (newFile.is_open())
    std::cout << GREEN "New file successfully created..." << RESET << std::endl;
    else
    std::cout << RED "File could not be created" << RESET << std::endl;
    newFile.close();
    }
}

int main() {

}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402

2 Answers2

0

The main function is not intended to be invoked from your code. It is also a nonsense since it is called automatically when program starts. But if you need some alternative main to be invoked upon some condition, you can call it inside your main function

...
int alternative_main()
{
   place your program there
}
...
void file_create(std::string name) {
    ...
    if (file.is_open()) {
    ...
    alternative_main()
    } 
    else {
    ...
    }
}

Suppose your main looks like this

int main() {
   file_create("myfile");
}
armagedescu
  • 1,758
  • 2
  • 20
  • 31
0
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

#define GREEN "\033[32m"
#define RED   "\033[31m"
#define RESET "\033[0m"

int main(); // defined before file_create

void file_create(std::string name) {
    std::ifstream file(name);
    if (file.is_open()) {
    file.close();
    std::cout << "File already exists..." << std::endl;
    main();
    } 
    else {
    file.close(); std::ofstream newFile(name);
    if (newFile.is_open())
    std::cout << GREEN "New file successfully created..." << RESET << std::endl;
    else
    std::cout << RED "File could not be created" << RESET << std::endl;
    newFile.close();
    }
}

int main() {

}

Define int main() above file_create() so you can call it in file_create

Wils
  • 13
  • 3