-1

So Im new to coding, and I was wondering why my for loop is not going through argv correctly. When I run it, it doesnt run at all. Here is the bit of code that matters:

int main(int argc, char* argv[]){
bool isCapital[500];
bool capital;
bool isSpace[500];
bool space;
bool palindrome;
cout<<"Test";
for(int j=1; argv[j][0]!='-'; j++){
    cout<<argv[j][0];
    if('-'==argv[j][0]){
        cout<<j;
        for(int i=0; argv[j][i]!='\0'; i++){
            if((argv[j][i])==('c'|'C')){
                isCapital[j]=true;
                break;
                cout<<isCapital[j];
            }
        }
        for(int i=0; argv[j][i]!='\0'; i++){
            if((argv[j][i]==('s'|'S'))){
                isSpace[j]=true;
                break;
            }
        }
    }
}
  • I'd suggest stepping through in a debugger to see exactly what the program does and find where it diverges from what you expect. I'd also suggest compiling with warnings. – chris Nov 02 '18 at 23:28
  • 1
    How do you run it? What dp you pass as programs arguments? What is the input? What is the expected output? – KamilCuk Nov 02 '18 at 23:31
  • 2
    `for(int j=1; argv[j][0]!='-'; j++){` you'll access `argv` out of range since you don't limit your loop to `argc`. – Swordfish Nov 02 '18 at 23:31
  • 1
    `(argv[j][i]==('s'|'S'))` doesn't do what you think it does. You want `(argv[j][i] == 's' || argv[j][i] == 'S')`. – Swordfish Nov 02 '18 at 23:32
  • `break; cout< – Swordfish Nov 02 '18 at 23:35
  • Your arrays are uninitialized so setting `isCapital[j]=true;` or `isSpace[j]=true;` means nothing since those values are indistinguishable from the random garbage in the arrays. – Swordfish Nov 02 '18 at 23:36
  • 1
    Whats that program supposed to do? Hint: there are functions in the standard library that check if a character is a space (`std::isspace()`) or a uppercase character (`std::isupper()`) in ``. – Swordfish Nov 02 '18 at 23:37
  • If you describe what your input parameters are supposed to be and their meaning we might be better able to understand what your code is trying to achieve. – Galik Nov 02 '18 at 23:42
  • Looks like your loop condition is inverted. – Eugene Nov 02 '18 at 23:46
  • Also, this approach to parsing args may be helpful: https://stackoverflow.com/questions/44233367/how-to-declare-a-pointer-to-pointer-to-constant-in-c/44233463#44233463 also https://stackoverflow.com/questions/5272550/c-command-line-parameters/51534733#51534733 – Galik Nov 02 '18 at 23:48

1 Answers1

0

There are a couple of bugs in your code but I think the code below is major one:

for(int j=1; argv[j][0]!='-'; j++){
    cout<<argv[j][0];
    if('-'==argv[j][0]){

so let us look at it in more details.

for(int j=1; argv[j][0]!='-'; j++){
             ^^^^^^^^^^^^^^^

This part says: only execute the body of the for-loop if argv[j][0] is not a -

This line:

    if('-'==argv[j][0]){

says: only execute the body of the if-statement if argv[j][0] is a -

These two are contradictions so execution never enters the body of the if-statement

Maybe you want this line

for(int j=1; argv[j][0]!='-'; j++){

to be

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

BTW:

As written in a comment by @swordfish

1) Notice that

if((argv[j][i])==('c'|'C'))

is probably not what you want. You probably want:

if(argv[j][i] == 'c' || argv[j][i] == 'C')

2) Notice that your loop condition should consider argc

3) That isCapital and isSpace should be initialized to "all false"

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Alright, all I had to do is change the != in the for loop to <= and that fixed it thank you! But just for clarification, even though it works, why is the if statement a bad idea? – Shawn Keene Nov 03 '18 at 01:43
  • 1
    @ShawnKeene - Other programming languages interpret your construct `if (argv[j][i] == ('c' | 'C'))` as "test if `argv[j][i]` is equal to one of `'c'` or `'C'`", but C++ does not. The `if` statement is a bad idea because its meaning is completely different in C++ from what you apparently expect. Look up "bitwise operations" for the specifics of what that test actually does. – Peter Nov 03 '18 at 07:21
  • In ascii `x == ('C'|'c')` turns out to be equivalent to `x == 'c'`), due to the way the encoding works (lower case letters are identical to upper case with one extra bit set), so it may superficially appear to work... – Chris Dodd Nov 03 '18 at 20:15