#include <iostream>
usingnamespace std;
int main()
{
int counter = 0;
int anarray[9] = {3, 1, 10, 2, 7, 14, 5, 8, 2};
int number =0;
do{
while(counter <= 8){
cout << anarray[counter] << endl;
counter++;
}
cout << "Please enter a number(enter -1 to quit): ";
cin >> number;
if (number >= 0 || number <= 8 && number != -1)
{
cout << "\nYour number: " << anarray[number] << endl;
}
}while(number != -1);
return(0);
}
I have made an array already but not sure where to go from there. Not totally asking for the answer.Do I need to make a class to do this?
Thank you for your help.
//What you think it's doing
if ((number >= 0 || number <= 8) && number != -1)
//what it's actually doing (&& has a higher precedence than ||)
if (number >= 0 || (number <= 8 && number != -1))
Even if it did what you thought is was doing, it still wouldn't be doing what you think it is doing. Say I enter 10:
(number >= 0 || number <= 8) is true (10 is >= 0)
number != -1 = true
if we and these results now, we get true and the program spits out some garbage value.
The solution you ask? Change your if statement to this:
if (number >= 0 && number <= 8)
What all have you covered/ learned so far? An array wrapper wouldn't be too hard to implement, and it is certainly a good beginner OOP exercise. You can even build on it as you learn more about OOP and C++ in general.
I have done arrays and covered up to classes and pointers. However, I don't have a good grasp of these things, and I would be willing to try an array wrapper as you suggested.