looking for numbers

hi,i'm new to c++ and i'm try to practise some stuff i've covered while learning.
the goal for the code is for the use to enter a few numbers, then enter a number to find in that digit, and report wether the number specified is in the original number. here is the code

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
#include <iostream>
#include <cstdio>
using namespace std;

void myfunc();


int main()
{
int i,j;
char end;
int str[10];
int find;

for(;;){

cout << "enter a number: ";
cin >> str[10];
 
 if(str[10]==0){break;};


cout << str[10] << "\n";

cout << "enter number to find: ";
cin >> find;


for(i=0;i<10;i++){
	if(str[i]==find){
		cout << "FOUND\n";
		break;}
	
	else{cout << "not "; break;}
}

}
return 0;
}

even if i enter a number that is in the original str, it is not reported, i still get told `not1. the break statments work though. how can i seach the number and look for a digit and report if it is there. thanks for any help.
You are using str improperly, are you sure you want it to be an int array?
i would like to use str as an array as i want to be able to access each of the numbers entered into it, eg str[0]=4 or str[4]=5. Or am i doing this wrong. i want to be able to enter a 10 digit number, then say i want to look for a number i specify, called find. In the next for loop, i want to check each number in the str array to see if it matches with find. I can't really see whats going wrong, but i know its somewhere in the
1
2
3
4
5
6
7
for(i=0;i<10;i++){
	if(str[i]==find){
		cout << "FOUND\n";
		break;}
	
	else{cout << "not "; break;}
}

bit. Can you see what i'm doing wrong?
Your for-loop will only loop once because wether a number is found or not, the break statement is executed, remove the else statement.
Last edited on
odd, that was my first thought. but this revised code(no break statment in that else bit doesnt work, it still won't report found even if the digit being looked for is clearly in the str array.

1
2
3
4
5
6
7
for(i=0;i<10;i++){
	if(str[i]==find){
		cout << "FOUND\n";
		break;}
	
	else{cout << "not "<< "\n";}
}


it still doesnt work. i'm using quincy as my ide and my out put if it helps is

not
not
not
//it goes on for 10 times
enter a number:


Last edited on
You can't cin>> into an array and have it be populated with the digits as you have coded it. You are trying to read into str[10], which is beyond the bounds of the array so you never check it in your for loop. The loop itself is just going over a bunch of uninitialized data.
Topic archived. No new replies allowed.