Head assignment test issues

Just posted this question in Unix/Linux Program forum, I was not sure which one I should post in as I am new to the forum. Thanks for your Patience.

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  #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

cout << endl << "==> " << argv[e] << " <==" << endl;

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.

else if (true)
{

for (int g=1; g < argc; g++)
{

max.open(argv[g]);

cout << endl << "==> " << argv[g] << " <==" << endl;

//Same as above the if-statment below ouputs a message if the file failed to open.

if (max.fail())
{
cout << "File did not open correctly." << endl;

continue;

}

//Like above the for statment reads and outputs the lines being read.

for (int g=0; !max.eof(); g++)
{
getline(max, cheese);

cout << cheese << endl;

}

}
}

//if anything goes weird and the above else-if statments don't work for a command the bellow statment just output the usage statment.

else
{
usage();
}

return 0;
}

bool string_to_integer(char str[], int& val)
{
string std_str( str );
istringstream i( std_str );
i >> val;
return !i.fail();
}

void usage()
{
cout << "usage: comp201_head [-n #] file1 [file2] ..." << endl;
Can you explain how the -k argument is meant to affect your program?
-k is just an invalid value.
im trying to prepare the program incase the user enters an unaccepted command.
-n should be what is entered but I want the usage statement to be output if they enter anything other than that.
like -v or -a

Hope that gives a better idea of what I am trying to do.

If the first argument always has to be -n, why can't you just check that?
1
2
3
4
if(argc < 3 || std::string(argv[1]) != "-n")
{
    //...
}
Topic archived. No new replies allowed.