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
  • 161,918
  • 27
  • 262
  • 366

3 Answers3

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,505
  • 2
  • 19
  • 25
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
-2
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
//use this instead of repetitively using std all the time
   

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

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


int main() {
string name;
cin>>name;
file_create(name);


}


try using this way...
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 25 '21 at 18:17
  • [Don’t](https://stackoverflow.com/questions/1452721) write `using namespace std;`. – JDługosz Oct 25 '21 at 18:23