// Lab1.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include "Lab1.h" #include #include #include #include #include using namespace std; /* a helpful "usage message" function that uses that parameter to print out a helpful usage message telling the user how to run the program correctly , and returns the enumeration label corresponding to the failure */ int usage(char* programName) { cout << "usage: " << programName << " : "; cout << "Please enter exactly one command line argument as the file name." << endl; return invalidinput; } /* A readfile function that reads the input and prints out each line of the file as the output */ int parse_file(vector& lines, char* fileName) { //Create variables that takes int the file ifstream input; input.open(fileName); string line; //Check if the input is good if (!(input.good())) { return cannotopenfile;//return corresponding err } while (input >> line) {//Extract line from input and add to vector lines.push_back(line); } return success;//return corresponding msg } /* The main method */ int main(int argc, char * argv[]) { //std::cout << "hello world" << endl; //Check if only one command line argument entered if (argc != NUM_ARGS) { return usage(argv[programname]); } else { if (parse_file(string_vec, argv[inputfilename]) != success) {//Check if the file is parsed with success return parse_file(string_vec, argv[inputfilename]); } else { for (unsigned int i = 0; i < string_vec.size(); i++) {//An outer loop through the string vector //Declare a boolean for checking if the string is number bool isNum = true; //An inner loop that loops through each string for (unsigned int j = 0; j < string_vec[i].length(); j++) { if (!isdigit(string_vec[i][j])) { isNum = false;//Set the bool to be false for nondigit char } } //If all digits print and push to vector if (isNum) { //Convert string to int stringstream sstm(string_vec[i]); int temp = 0; sstm >> temp; //Push the int to the int vector int_vec.push_back(temp); } else { cout << string_vec[i] << endl; } } //Print all the integers for (unsigned int k = 0; k < int_vec.size(); k++) { cout << int_vec[k] << endl; } return success; } } }