Nested if else within a for loop

I'm having trouble making sense of my nested if else within a for loop. I'm betting it also has something to do with the conditions of the if statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Array a;
int b;

for (int index = 0; index < a.capacity( ); index++)
    {
        cout << "Input an index and a value [q to quit]: ";
        cin >> index >> b;
        a[index] = b;
        if (index != 'q'||'Q')
        {
            continue;
        }
        else
        {
            for (int index = 0; index < a.capacity( ); index++)
                cout << index + 1 << "=>" << a[index] << endl;
        }

    }

When I enter the condition for the if else just loops back to the beginning of the for loop without doing the else statement.

The output is supposed to look like this

Input an index and its value: [Q to quit] 4  6
Input an index and its value: [Q to quit] q

4 => 6
...
...


And I haven't gotten that far yet, as I'm stuck on this part, but I'm also supposed to only cout the indexes with an assigned value.

Any help is appreciated
Last edited on
Please post your code in code tags, see http://www.cplusplus.com/articles/jEywvCM9/

So it looks like this.
1
2
3
4
5
6
7
8
9
10
11
for (int index = 0; index < a.capacity(); index++) {
  cout << "Input an index and a value [q to quit]: ";
  cin >> index >> b;
  a[index] = b;
  if (index != 'q' || 'Q') {
    continue;
  } else {
    for (int index = 0; index < a.capacity(); index++)
      cout << index + 1 << "=>" << a[index] << endl;
  }
}


> if (index != 'q' || 'Q')
This is not how you compare something with a set of values.

Do something like
if (index != 'q' && index != 'Q')

Or
if ( tolower(index) != 'q' )

What type is index?
1
2
3
  cin >> index >> b;
  a[index] = b;
  if (index != 'q'

Because on the one hand, it seems to be an integer to serve as an index into your array.
And on the other, it seems to be a char you use to signal the end of input.
You can't have it both ways!

If you need to have several attempts at parsing the same input, then read it into a string stream to begin with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ( ) {
  string line;
  getline(cin,line);
  istringstream is(line);
  int index, value;
  char c;
  if ( is >> index >> value ) {
    cout << "Index = " << index
         << ", Value = " << value
         << endl;
  } else {
    is.clear(); // reset error state
    if ( is >> c ) {
      cout << "C = " << c << endl;
    }
  }
}


$ g++ foo.cpp
$ ./a.out 
1 2
Index = 1, Value = 2
$ ./a.out 
d
C = d
$ 



Thanks. It's literally my first time posting on this forum so forgive me for the mistakes.

index is an integer but I thought you can still use char to compare with integers in C++?
The example you showed me is beyond what we've been taught in class, so I think it's best that I don't use it. But even then, it doesn't seem like it will populate an array. Correct me if I'm wrong.
Another problem is you're using index for your loop variable and your input.
The input will mess up your loop.

You can still do the same without a string stream.
1
2
3
4
5
6
7
8
9
10
11
12
  int index, value;
  char c;
  if ( cin >> index >> value ) {
    cout << "Index = " << index
         << ", Value = " << value
         << endl;
  } else {
    cin.clear(); // reset error state
    if ( cin >> c ) {
      cout << "C = " << c << endl;
    }
  }


> cout << "Input an index and a value [q to quit]: ";
If you want to actually exit the loop, then you need to use break; and not continue;
Thanks! I appreciate the help!
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
#include <iostream>

int main()
{
   // Array is not a pre-defined C++ data type
   // is this an alias for a STL container?
   // Array a;

   // max number of elements your array can hold
   // using uniform initialization instead of
   // const int arr_size = 10;
   // https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
   const int arr_size { 10 };

   // use more descriptive variable names,
   // single character variables may be easier to type
   // but are easier to confuse
   // create the array, zero initialized
   int arr[arr_size] { };

   for (int index { }; index < arr_size; index++)
   {
      std::cout << "Input an index and a value [-1 to quit]: ";

      // intitialize variables close to where they are used
      int input { };
      std::cin >> index >> input;

      if (index != -1 || input != -1)
      {
         // you want to add the input into your array only if it is valid
         arr[index] = input;
         continue;
      }
      else
      {
         std::cout << '\n';

         for (int index { }; index < arr_size; index++)
         {
            std::cout << index << "=>" << arr[index] << '\n';
         }
         break;
      }
   }
}

Input an index and a value [-1 to quit]: 4 106
Input an index and a value [-1 to quit]: 8 999
Input an index and a value [-1 to quit]: -1 -1

0=>0
1=>0
2=>0
3=>0
4=>106
5=>0
6=>0
7=>0
8=>999
9=>0

A for loop isn't the best way to get a variable number of inputs. I might use a while or do/while loop instead:
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
#include <iostream>

int main()
{
   const int arr_size { 10 };

   int arr[arr_size] { };

   int index { };

   while (true)
   {
      std::cout << "Input an index and a value [-1 to quit]: ";
      int input { };
      std::cin >> index >> input;

      if (index != -1 || input != -1)
      {
         arr[index] = input;
         continue;
      }
      else
      {
         std::cout << '\n';

         break;
      }
   }

   for (int index { }; index < arr_size; index++)
   {
      std::cout << index << "=>" << arr[index] << '\n';
   }
}

Topic archived. No new replies allowed.