arrays within an if statement
Nov 2, 2009 at 11:45am UTC
Getting a bit stuck trying to put an if statement to test weather the array value entered is bigger or equal to 1 and smaller than or equal to ten.
I've tried
numbers[counter]>=1 && numbers[counter]<=10
but it doesn't work, i've also tried just numbers but it's coming up with can't compare a pointer and integer, any help would be greatly appreciated! Thanks alot.
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
#include <iostream.h>
#include <windows.h>
int main()
{
int numbers[10];
int counter=0;
for (counter = 1; counter <= 9; counter++)
{
cout << "Enter element number " << counter << " : " ;
cin >> numbers[counter];
}
system("PAUSE" );
return 0;
}
Nov 2, 2009 at 12:03pm UTC
Your expression is correct. It's possible that it's used incorrectly. Can you post your code with it in.
Nov 2, 2009 at 12:08pm UTC
Can't remeber where I placed it before but I can't really see where to put it now because if I put the if statement after cin
then yeah it's testing weather or not it's >=1 or <=10 but I don't know how to get it to repeat the for loop if it is true and exit if it is false.
Nov 2, 2009 at 12:49pm UTC
my whole code is:
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 67 68 69 70
#include <iostream.h>
#include <windows.h>
int main()
{
int numbers[10];
int counter=0;
int guess[10];
for (counter = 1; counter <= 9; counter++)
{
cout << "Enter a number in box " << counter << " : " ;
cin >> numbers[counter];
}
system("CLS" );
for (counter = 1; counter <= 9; counter++)
{
cout << "Please guess the number in box " << counter << " : " ;
cin >> guess[counter];
}
system("CLS" );
for (counter = 1; counter <= 9; counter++)
{
cout << "box " << counter << " : " ;
if (guess[counter] == numbers[counter])
{cout<<"Right!" <<endl;}
else {cout<<"Wrong!" <<endl;}
}
system("PAUSE" );
return 0;
}
Just trying to get it so it'll only accept numbers between 1 and 10 (including 1 and ten).
Nov 2, 2009 at 1:20pm UTC
What's wrong with that?
Nov 2, 2009 at 1:26pm UTC
It's fine but I need it to only accept numbers between 1 and 10, just don't know how to include the if statement in the loop.
Nov 2, 2009 at 2:20pm UTC
1 2 3 4 5 6 7 8
for (counter = 1; counter <= 9; counter++)
{
std::cout << "Enter a number in box " << counter << " : " ;
do
std::cin >> numbers[counter];
while (!(1 <= numbers[counter] && numbers[counter] <= 10));
}
Is there a reason your loops start at 1? C++ arrays start at zero. Conventionally, your loop would be:
for (counter = 0; counter < 10; ++counter)
Topic archived. No new replies allowed.