| justinroi21 (7) | |
|
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 !!! | |
|
|
|
| vlad from moscow (3662) | |
| And where is your code? | |
|
|
|
| Framework (3242) | |||
Enjoy! Wazzak | |||
|
Last edited on
|
|||
| justinroi21 (7) | |
|
#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?! | |
|
|
|
| Peter87 (3917) | |||
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
|
|||
| justinroi21 (7) | |
| Then what i'm supposed to do ? | |
|
|
|
| ggarciabas (6) | |||
Could be something like this too.
GGarciaBas | |||
|
|
|||
| justinroi21 (7) | |
| Thank You! | |
|
|
|