Parsing and comparing argv

Hello, I am trying to iterate through argv[] using for loop, but when I try to compare argv[i] to a string, there's no result, that means false, but when I just print argv[i], it prints exactly the same result as the string I was comparing it to.

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
 #include<iostream>
#include <fstream>
#include <main.h>
#include <typeinfo>
using namespace std;

int main (int argc, char* argv[]) {
    
    int flag = 0;
    for (int i = 0; i < argc; i++) {
        if (argv[i] == "--de") {
            flag = 1;
            cout << "OK" << endl;
            break; 
        }
    }
 
    if (flag == 1) {
        cout << "o";
    }
    // WITH TEXT FILES
    ifstream infile; // intializing a file variable
    ofstream outfile;
    infile.open("text.txt"); // opening file
    string x, y;
    while(!infile.eof()) {
        getline(infile, x); 
        getline(infile, y); // assigning values to x and y from text file
    };
    infile.close(); // closing text file

    // INITILIAZING VARIABLES
    string msg = x;
    string key = y;
    msg = makeUpper(msg);
    key = makeUpper(key);
    msg = spaceEraser(msg);
    key = spaceEraser(key);
    

    int msgLen = msg.length(), keyLen = key.length();

    // CALLING FUNCTIONS
    string newKey = newKeyGen(msgLen, keyLen, key);
    string encryptedMsg = encryptor(msgLen, msg, newKey);
    string decryptedMsg = decryptor(msgLen, encryptedMsg, newKey);

    newKey += '\0';
    // decryptedMsg += '\0';
 
    // PRINTING RESULTS
    cout << "Original Message: " << msg;
    cout << "\nKey: " << key;
    cout << "\nNew Generated Key: " <<newKey;
    cout << "\nEncrypted Message: " << encryptedMsg;
    cout << "\nDecrypted Message: " << decryptedMsg;

    outfile.open("text.txt");
    outfile << encryptedMsg << endl;



 
	return 0;
}
You can't use the comparison operator with two C-strings, you must use strcmp() instead.

or you can cast to string and use ==
read your compiler messages
warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead)
 
for (int i = 0; i < argc; i++) {


Small point, but as argv[0] is the program name, this can never be the required. start i at 1.

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


Also, rather than flag being type int, it could be of type bool using false/true as it only has 2 states (found/not-found).

1
2
3
4
5
6
infile.open("text.txt"); // opening file
    string x, y;
    while(!infile.eof()) {
        getline(infile, x); 
        getline(infile, y); // assigning values to x and y from text file
    };


Whatever you thought this code would do, it probably doesn't. If odd number of lines in the file, x will be the last line of the file and y is empty. If even number of lines then both x and y will be empty.

Last edited on
Topic archived. No new replies allowed.