command line

Pages: 12
No.

Syntactically you don't have any function call in that. What is the correct syntax to call a function that does not take parameters? Besides, dhayden did already explain why you should not hardcode "comp201_head" into the usage string; the user can rename the program file.

I don't think that the argc can ever be 0. What value there should be in argv[0]?

There are several situations where you should print the usage instruction. That is why you have created a separate, easy to call function.
still having problems....

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
int main(int argc, char* argv[])
{
	ifstream fin;
	int numlines;
	string line;
	
	if (argc == 1)	
	{
		usage();
	}
	else if (argc > 2 && string_to_integer(argv[2], numlines))			//switch
	{
		//cout << numlines << endl << argc << endl;
		for (int i = 3; i <= argc; i++)
		{
			fin.open(argv[i]);
			if (fin.fail())
			{
				cout << "Error opening file." << endl;
				i++;
				continue;
			}
			for (int j = 0; !fin.eof() && j < numlines; j++)		// not end-of-file
			{
				std::getline(fin, line);
				cout << line << endl;
			}
			fin.close();
		}
	} 
	else if (true)
	{
		for (int i = 1; i < argc; i++)
		{
			fin.open(argv[i]);
			if (fin.fail())
			{
				cout << "Error opening file." << endl;
				i++;
				continue;
			}
			for (int j = 0; !fin.eof(); j++)		// not end-of-file
			{
				getline(fin, line);
				cout << line << endl;
			}
		}
	}
	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;
}
You say "problems", but don't explain what they are. The better you are at explaining a problem, the more likely you are to spot the reason for it yourself.

Lines 35-46 almost repeat lines 17-27. Make a separate function for actually printing one file and call that function. That way you don't have to write same code multiple times. (Just like with the usage function.)

Line ensures that argc is 3 or greater, but line 14 creates indices that will be "out of bounds" for the argv array of words.

Line 31 "else if true" is true every single time.

You wrote:
If your program is called with invalid inputs, it should output the usage statement above and exit. Invalid inputs include
(1) no file name,
(2) an argument that starts with a dash (-) but is not the only supported switch
(-n), or
(3) not having an integer immediately following the supported switch.

You don't test all those cases in your program.
You don't have a default value for numLines. I didn't see one in the original spec so you may be able to make up something for yourself, but there should be some default.

You don't check for "-n" anywhere.

In pseudo-code, you want to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
set currentArg = 1;
if (currentArg <= argc) usage();
if (argv[currentArg][0] == '-') {
    if (argv[currentArg][1] == 'n') {
        if (++currentArg <= argc) usage();
        numLines = atoi(argv[currentArg++]);
    } else {
        usage();  // "-" option but it isn't -n
    }
}
// currentArg now points to the first file arg
for (; currentArg < argc; ++currentArg) {
    // print the first numLines of argv[currentArg]
}


i dont know what argument to fill in on line 31, anyone help?
I need to rap this up real soon. Thanks
Sorry, but the point of my previous post was that you need to restructure your code. For example, right now if you enter "theProg file1 90125.fanz file3" it will think that 91025 is the argument to -n (at line 11).

You need to initialize numLines to something.

I suggest that you follow the pseudo-code that I gave in my last post.
can numlines be initialized to anything?
Your original specs say:
This means that if no -n switch is provided, output all lines of the file; if it is provided, output up to that
number,

It is kind of hidden there, but you have a clear (although tricky) default that you should use. I would use 0.

Why? Lets suppose that you write and use a function:
1
2
3
4
5
void showlines( const char * name, size_t limit );
// param name is used as filename
// param limit:
//   if ==0 show all lines
//   if  >0, show at most limit lines 
Topic archived. No new replies allowed.
Pages: 12