Reputation:
I want to make a program that will perform some math after reading from user input files.
During the reading process (a function) I want to check if the user syntax in the file is correct, otherwise I would like to shutdown the program so that the user can modify the file(s= accordingly and run it again.
The structure will be something like this:
int main(int argCount, char *args[])
{
std::string fileName = "PathOfFile";
int a = GetUserInput(fileName, variableName);
int b = GetUserInput(fileName, variableName);
// Other functions will be placed here
return 0;
}
int GetUserInput(std::string filename, std::string variableName)
{
// Some routine to read the file and find the variableName
// Some routine to check the syntax of the user input.
// Let us assume that the integers are to be fined as: variableName 1;
// and I want to check that the ; is there. Otherwise, shutdown the program.
}
How can I shutdown the program safely from the function GetUserInput
? Is there any C++ to signal that the program must wrap up and exit?
Upvotes: 1
Views: 235
Reputation: 118425
There are many different ways of doing this, the differences are mostly style, personal preferences, and which parts of the C++ library you are familiar with.
exit()
.int
value setting, the function takes a pointer or a reference to an int
value as an additional parameter and sets it, if valid. The function returns a bool
, instead, to indicate whether it parsed a valid setting. main()
checks the returned bool
value, and itself return
s from main()
, ending the program.std::optional<int>
, instead, returning a std::nullopt
to indicate a parsing failure. main()
checks the returned value, and itself return
s from main()
, ending the program.main
, with the exception handler return
ing from main
.Each alternative has its own advantages and disadvantages. You can decide, by yourself, which approach works best for your program.
Upvotes: 4
Reputation: 123431
I would suggest to structure your code such that "normal shutdown" is return from main
. For example like this
bool GetUserInput(const std::string file,const std::string& variablename,int& a) {
a = ... read value from file...
if (something went wrong) return false;
return true;
}
int main() {
int a;
if (! GetUserInput("file","foo",a)) return 1;
int b;
if (! GetUserInput("file","foo",b)) return 1;
}
You can consider to throw an exception when something goes wrong:
void GetUserInput(const std::string file,const std::string& variablename,int& a) {
a = ... read value from file...
if (something went wrong) throw std::runtime_error("something went wrong");
}
int main() {
int a;
GetUserInput("file","foo",a);
int b;
GetUserInput("file","foo",b);
}
This allows you to catch the exception in main
and act accordingly in case you can recover from it. Also you can have different exceptions rater than only a single bool
.
If you merely want to exit the program cleanly, you can use std::exit
as suggested by πάντα ῥεῖ:
void GetUserInput(const std::string file,const std::string& variablename,int& a) {
a = ... read value from file...
if (something went wrong) std::exit();
}
This will safely shutdown your program (stack is unwound, ie destructors are called, files are closed properly, etc). However, this does not allow you to react on in the caller.
Upvotes: 1