Help me ! C++ !

so this the output ..
How many numbers? 4
Enter a positive number: 4
Enter a positive number:0
Invalid Input ! Try again!
Enter a positive number: 6
Enter a positive number: 4
Enter a positive number:-1
Invalid Input ! Try again!
Enter a positive number: 7
The Value is 4 6 4 7
Enter a number to be searched: 4
Found at indices: 0 2


Help me for the Codes ! ASAP! Thanks !!!
And where is your code?
closed account (zb0S216C)
1
2
3
4
int main( )
{
    // Your code here.
}

Enjoy!

Wazzak
Last edited on
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int *num,size,x,a;
cout<<"How many number? ";
cin>>size;

num=new int[size];

for(x=0; x<size; x++){
cout<<"Enter a positive number: " ;
cin>>num[x];

if(num[x]<0){
cout<<"Invalid Input!";
cout<<endl;
cout<<"Enter a positive number: ";
cin>>num[x];
}
}
cout<<endl;
cout<<"The value is " ;
for(x=0; x<size; x++){
cout<<num[x]<<" ";
}

cout<<endl;
cout<<"Enter a number to be searched: ";
cin>>a;

if(num[x]!=a)
cout<<"Not Found!";

else
cout<<"Found Indies ";
for(x=0; x<size; x++){
if(num[x]==a)
cout<<x<<" ";
}
getch();
}


This is my codes. But theres an error when it comes to the finding the index of the searched number.. There's always a "Not Found". Whats my error ? Help?!
1
2
3
4
if(num[x]!=a)
	cout<<"Not Found!";
else
	cout<<"Found Indies ";

When the program comes here x will have the same value as size, so num[x] will try to access the element that is after the last element in the array. You probably want to use a loop to compare a with the elements in the array.
Last edited on
Then what i'm supposed to do ?
Could be something like this too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
cout<<endl;
cout<<"Enter a number to be searched: ";
cin>>a;

for (x=0; x<size; x++) {
 if (num[x] == a) 
       cout << x << " ";
 else if (x == size-1)
       cout << "Not Found!";
}

getch();
...


GGarciaBas
Thank You!
Topic archived. No new replies allowed.