Hello, I am almost done with my assignment but I am having trouble figuring out how to fix a test that I am trying to put my program through. I am trying to test is with putting in the command line, what the user should enter for example is
./prog -n [#] [files]...
But the test I am trying to work out is what if the user enters this in the command line
./prog -k
Bellow is my code. Any assistance is appreciated.
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
// Preconditions: str is a c-style string
// Postconditions: val has integer represented by the c-style string
// Returns: true if the conversion was successful, false otherwise
bool string_to_integer(char str[], int& converted);
// Postconditions: outputs the usage statement
void usage();
int main(int argc, char* argv[])
{
int linenumber;
ifstream max;
string cheese;
//Below is an if statement that makes sure the user inputs the correct information in the command line
if(argv==0)
{
usage();
}
if (argc==1)
{
usage();
}
//Bellow is where the switch comes into play.
else if (argc > 2 && string_to_integer(argv[2], linenumber))
{
for (int e=3; e < argc; e++)
{
max.open(argv[e]);
//Bellow is an ifstatement that will output the text if the file does not open correctly
if (max.fail())
{
cout << "File did not open correctly." << endl;
continue;
}
//The for-statement below searches each file and reads the lines and displays the lines
for (int g=0; !max.eof() && g<linenumber; g++)
{
std::getline(max, cheese);
cout << cheese << endl;
}
max.close();
}
}
// The else-if statement below would be used if the user only enters in one command from the command line, the else-if statment is when the user enters in greater than 2 commands.