Require to fix the else output repeating more than once of for statement

Tried to make a search record for any number and here is what I want to do:
1. given there are numbers between 999, 111 and 12, make a simple search function for the for statement to find these numbers using the input, if one of these numbers are found from the input, then one of these numbers will appear.
2. if none of these numbers appear, it should have an error output stated these numbers arent found and the error output should only appear once.

However it seems like the for statement likes to repeat the else statement of the error output if the numbers arent found. My code has compiled already and I cant figure out why.

Keep in mind the program should be a beginner level so dont make it very complicated and makes me easier to read as a beginner.
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
71
72
73
74
75
76
77
#include <iostream>
#include <string>   
#include <cctype>
using namespace std;

using std::cout;

using std::cin;

using std::endl;

 
class Numbers 
{

private:
int savenumber;

 
public:
void setRecordNumber (int newnumber) 
{
    
   savenumber = newnumber;
  
} 
int getRecordNumber () 
{
    
   return savenumber;
  
}

 
};


 
int main () 
{
  
int searchnumber;
bool foundnumber;
Numbers recordingNumber[5];
int Maxrecord = 3;
 
recordingNumber[0].setRecordNumber (999);
  
recordingNumber[1].setRecordNumber (111);
  
recordingNumber[2].setRecordNumber (12);
  
 
cout << "Please enter a number to search \n";
  
cin >> searchnumber;
  
for (int i = 0; i < Maxrecord; i++)
{
    if (recordingNumber[i].getRecordNumber() == searchnumber)
	{
	  
       cout << "A number is found!  \n";
       cout << "The number is " << recordingNumber[i].getRecordNumber() << endl;
    }
    else
   {
        cout << "Sorry, we cant find the number\n" << endl;
   } 
    
}
  
cout << endl;
return 0;
 
}

Last edited on
Your for loop has to print "found" or "not found" every time around the loop.

What you need is something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool found = false;
// search the array...
// .. until all records have been searched
// .. a record is found
for (int i = 0; i < Maxrecord && !found; i++)
{
    if (recordingNumber[i].getRecordNumber() == searchnumber)
	{
            found = true;
        }
}

// loop is complete, print the outcome just once.
if ( found ) {
  cout << "Found " << searchnumber;
} else {
  cout << "Not found";
}
Last edited on
@salemC

I also want to make something similar to this one, like if the first string word is hello and the first int number is 999, then the output should appear hello and 999 if I typed the input as 999 for searching.

The input is for numbers only, not for words so it should be much simple
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
71
72
73
74
75
76
#include <iostream>
#include <string>   
#include <cctype>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

 
class Numbers 
{
private:
int savenumber;
string saveword;

 
public:
void setRecordNumber (int newnumber) 
{
   savenumber = newnumber;
} 
int getRecordNumber () 
{
   return savenumber;
}
void setRecordWord (string newword) 
{
   saveword = newword;
} 
string getRecordWord () 
{
   return saveword;
}
};

int main () 
{
  
int searchnumber;
bool foundnumber;
Numbers recordingNumber[5];
int Maxrecord = 3;
 
recordingNumber[0].setRecordNumber (999);
recordingNumber[1].setRecordNumber (111);
recordingNumber[2].setRecordNumber (12);
recordingNumber[0].setRecordWord ("Hello");
recordingNumber[1].setRecordWord ("Nope");
recordingNumber[2].setRecordWord ("Good");
 
cout << "Please enter a number to search a match \n";
  
cin >> searchnumber;
  
for (int i = 0; i < Maxrecord; i++)
{
    if (recordingNumber[i].getRecordNumber() == searchnumber)
	{
	  
       cout << "A match is found!  \n";
       cout << "The number is " << recordingNumber[i].getRecordNumber() << endl;
       cout << "The word is " << recordingNumber[i].getRecordWord() << endl;
// An output of matching words and numbers should be found if the number of the match was found from the input number
    }
    else
   {
        cout << "Sorry, we cant find the number related to this match\n" << endl;
   } 
    
}
  
cout << endl;
return 0;
 
}
Last edited on
OK, so try it.
@salem c
Still cant figure out, so sorry about that, I still need to figure out how to make the output appear with a matching string word and the number if the number was found on the input number, so I need help.

The output should be this if 999 was typed into the input

1
2
3
A match is found!  
The number is 999
The word is Hello


This is what I tried so far
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
71
72
73
74
75
76
77
78
79

#include <iostream>
#include <string>   
#include <cctype>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

 
class Numbers 
{
private:
int savenumber;
string saveword;

 
public:
void setRecordNumber (int newnumber) 
{
   savenumber = newnumber;
} 
int getRecordNumber () 
{
   return savenumber;
}
void setRecordWord (string newword) 
{
   saveword = newword;
} 
string getRecordWord () 
{
   return saveword;
}
};

int main () 
{
  
int searchnumber;
bool foundnumber;
Numbers recordingNumber[5];
int Maxrecord = 3;
 
recordingNumber[0].setRecordNumber (999);
recordingNumber[1].setRecordNumber (111);
recordingNumber[2].setRecordNumber (12);
recordingNumber[0].setRecordWord ("Hello");
recordingNumber[1].setRecordWord ("Nope");
recordingNumber[2].setRecordWord ("Good");
 
cout << "Please enter a number to search a match \n";
  
cin >> searchnumber;
  
bool found = false;

for (int i = 0; i < Maxrecord && !found; i++)
{
    if (recordingNumber[i].getRecordNumber() == searchnumber)
	{
            found = true;
        }
}

if ( found ) {
  cout << "Found " << searchnumber;
  cout << "Found " << recordingNumber.getRecordWord[i];
// An output of matching words and numbers should be found if the number of the match was found from the input number

} else {
  cout << "Not found";
}
  
cout << endl;
return 0;
 
}
Last edited on
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
#include <iostream>
#include <string>

class Numbers {
private:
	int savenumber {};
	std::string saveword;

public:
	void setRecordNumber(int newnumber) { savenumber = newnumber; }
	int getRecordNumber() const { return savenumber; }
	void setRecordWord(const std::string& newword) { saveword = newword; }
	std::string getRecordWord() const { return saveword; }
};

int main() {
	const size_t Maxrecord {3};
	int searchnumber {};
	Numbers recordingNumber[Maxrecord] {};

	recordingNumber[0].setRecordNumber(999);
	recordingNumber[1].setRecordNumber(111);
	recordingNumber[2].setRecordNumber(12);
	recordingNumber[0].setRecordWord("Hello");
	recordingNumber[1].setRecordWord("Nope");
	recordingNumber[2].setRecordWord("Good");

	std::cout << "Please enter a number to search a match: ";
	std::cin >> searchnumber;

	int found {-1};

	for (size_t i = 0; i < Maxrecord && found == -1; ++i)
		if (recordingNumber[i].getRecordNumber() == searchnumber)
			found = i;

	// loop is complete, print the outcome just once.
	if (found >= 0) {
		std::cout << "A match is found!";
		std::cout << "\nThe number is " << recordingNumber[found].getRecordNumber();
		std::cout << "\nThe word is " << recordingNumber[found].getRecordWord() << '\n';
	} else
		std::cout << "Not found\n";
}

@seeplus

I have one question, what if the input was a string which I want to find matches by using words from the input? Did tried it but operators are unable to fit with string such as ==, therefore it wont work because of operators problem to string variables of input

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
#include <iostream>
#include <string>
using std::string;

class Numbers {
private:
	int savenumber {};
	string saveword;

public:
	void setRecordNumber(int newnumber) 
	{ 
	    savenumber = newnumber; 
	    
	}
	int getRecordNumber() const 
	{ 
	    return savenumber; 
	    
	}
	void setRecordWord(const std::string& newword) 
	{ 
	    saveword = newword; 
	    
	}
	std::string getRecordWord() const 
	{ 
	    return saveword; 
	}
};

int main() 
{
	const size_t Maxrecord =3;
	string searchwords ;
	Numbers recordingNumber[Maxrecord];

	recordingNumber[0].setRecordNumber(999);
	recordingNumber[1].setRecordNumber(111);
	recordingNumber[2].setRecordNumber(12);
	recordingNumber[0].setRecordWord("Hello");
	recordingNumber[1].setRecordWord("Nope");
	recordingNumber[2].setRecordWord("Good");

	std::cout << "Please enter a number to search a match: ";
	std::cin >> searchwords; 
// input the word to find match

	int found = -1;

	for (size_t i = 0; i < Maxrecord && found == -1; ++i)
		if (recordingNumber[i].getRecordNumber() == searchwords)
			found = i;

	if (found >= 0) 
	{
		std::cout << "A match is found!";
		std::cout << "\nThe number is " << recordingNumber[found].getRecordNumber();
		std::cout << "\nThe word is " << recordingNumber[found].getRecordWord() << '\n';
	} 
	else
		std::cout << "Not found\n";
}
Last edited on
Slightly different version, based @seeplus' 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
#include <iostream>
#include <string>

struct Numbers 
{
	int savenumber = 0;
	std::string saveword;
};

int main() 
{
	const size_t Maxrecord = 3;
	int searchnumber = 0;
	Numbers recordingNumber[Maxrecord] = 
	{
		{999, "Hello"},
		{111, "Nope" },
		{12,  "Good" }
	};

	std::cout << "Please enter a number to search a match: ";
	std::cin >> searchnumber;

	int found = -1;

	for (size_t i = 0; i < Maxrecord && found == -1; ++i)
		if (recordingNumber[i].savenumber == searchnumber)
			found = i;

	// loop is complete, print the outcome just once.
	if (found >= 0) {
		std::cout << "A match is found!";
		std::cout << "\nThe number is " << recordingNumber[found].savenumber;
		std::cout << "\nThe word is " << recordingNumber[found].saveword << '\n';
	} 
	else
		std::cout << "Not found\n";
}
@thmm

I did tried a similar thing to this code but uses words input to search a match, but it was not working because string will not work with operators with ==, you can see my code above the comment so can you help?
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
#include <iostream>
#include <string>
using std::string;

class Numbers {
private:
	int savenumber {};
	string saveword;

public:
	void setRecordNumber(int newnumber) 
	{ 
	    savenumber = newnumber; 
	    
	}
	int getRecordNumber() const 
	{ 
	    return savenumber; 
	    
	}
	void setRecordWord(const std::string& newword) 
	{ 
	    saveword = newword; 
	    
	}
	std::string getRecordWord() const 
	{ 
	    return saveword; 
	}
};

int main() 
{
	const size_t Maxrecord =3;
	string searchwords ;
	Numbers recordingNumber[Maxrecord];

	recordingNumber[0].setRecordNumber(999);
	recordingNumber[1].setRecordNumber(111);
	recordingNumber[2].setRecordNumber(12);
	recordingNumber[0].setRecordWord("Hello");
	recordingNumber[1].setRecordWord("Nope");
	recordingNumber[2].setRecordWord("Good");

	std::cout << "Please enter a number to search a match: ";
	std::cin >> searchwords; 
// input the word to find match

	int found = -1;

	for (size_t i = 0; i < Maxrecord && found == -1; ++i)
		if (recordingNumber[i].getRecordNumber() == searchwords)
			found = i;

	if (found >= 0) 
	{
		std::cout << "A match is found!";
		std::cout << "\nThe number is " << recordingNumber[found].getRecordNumber();
		std::cout << "\nThe word is " << recordingNumber[found].getRecordWord() << '\n';
	} 
	else
		std::cout << "Not found\n";
}
Last edited on
> if (recordingNumber[i].getRecordNumber() == searchwords)
Try calling getRecordWord instead.
and change L45 :)
Ok I fixed it and it worked
Thanks guys
Last edited on
Topic archived. No new replies allowed.