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.
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.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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;
}
}
}
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.
#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/constint 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:
#include <iostream>
int main()
{
constint 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';
}
}