Hi, I'm just starting to get into these command line arguements, and I just have one small problem with my first one. It's a simple program, which I basically got to work, it's just the conditions for error checking in the program aren't all working.
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
if (argc == 2) {
string word = argv[1];
cout << argv[1] << " has " << word.length() << " letters in it.";
}
else if (argc == 3 && argv[1] == "file") {
ifstream input_file(argv[2]);
if(!input_file) {
cout << argv[2] << " does not exist";
return 0;
}
char c;
int count = 0;
while(input_file.get(c)) {
if (c != '\n' && c != ' ')
count++;
}
cout << argv[2] << " has " << count << " letters in it.";
}
else {
cout << "Proper format is 'count <string>' or " << endl
<< "'count file <file>'" << endl << argv[1];
return 0;
}
return 0;
}
The bolded and underlined " argv[1] == "file" " is where I'm having the problem. It doesn't seem to understand this in the program. What confuses me though is if I create a string variable and set that equal to "file", then compare the arguement to the variable, it works fine. Also, I output argv[1] in the else statement just to make sure it says "file", and it did.
So, I guess my question is... is it unacceptable to directly compare an arguement to a string constant?