at start of program, should path input file , path output file console. if user give not required number of parameters or wrong parameters (with spaces example, or without ".txt") should give user second chance enter parameters without exiting program. possible?
int main(int argc, char* argv[]) { //and here should check if user entered parameters correctly //(number , if path) , give user try if wrong //(so user enter them console again) string path_open(argv[1]); strin path_out(argv[2]);
yes it's possible, but... weird. if going have program ask input, why not put that in loop until proper input? ultimately, i'd 1 or other:
- get command line input (and, @iinspectable suggests in comments, if it's not valid, exit program); or
- have program ask input recursively until user gives valid input.
input command line:
int main(int argc, char* argv[]) { // sanity check see if right amount of arguments provided: if (argc < 3) return 1; // process arguments: if (!path_open(argv[1])) return 1; if (!path_out(argv[2])) return 1; } bool path_open(const std::string& path) { // verify path correct... }
program asks input:
int main() { std::string inputpath, outputpath; { std::cout << "insert input path: "; std::getline(std::cin, inputpath); std::cout << std::endl; std::cout << "insert output path "; std::getline(std::cin, outputpath); } while (!(path_open(inputpath) && path_out(outputpath))); }
of course you'd validate input separately in case entering valid input path invalid output path, gist.
Comments
Post a Comment