Searching for a particular value within an array.

Hello there, I'm new to the forums, but am loving all the help that I've been seeing here. I'm having a little trouble with this code I made here. The idea is that I have a "Student" class that holds an ID number and a Value, which is all user-input. So, I think I have the inputting of the Students into the Array down properly (please, feel free to correct me if I'm wrong :P), but I'm confused as to how I could search through the array for a particular ID of a Student. Here's the code I have thus 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
#include <iostream>
#include "Student.h"
using namespace std;

int main()
{
	const int MAX = 10;
	int tempid = 0;
	double tempval = 0.0;
	int check = 1;
	int i = 0;
	Student list[10];
	cout << "Welcome! This program allows the user to input students (id and value)." << endl << "You cannot input more than 10 students." << endl;
	cout << "Please input one line with the student id (positive integer value) followed" << endl << "by a line with the student value (real value). Indicate the last student by giving a 0" << endl;
	cout << "Enter values now: " << endl;
	for (i; i<11; i++) {

		while (tempid != 0) {
			cout << "ID?: ";
			cin >> tempid;

			cout << "value?: ";
			cin >> tempval;
			
			Student temporary(tempid, tempval);
			list[i] = temporary;
		}
		
	}
	cout << "You are now done entering students." << endl << "You can now find the value of a student by entering the ID." << endl;
	cout << "Please input on line with the student id (integer value)" << endl << "Indicate the desire to stop by giving 0. Enter IDs now:" << endl;
	while (check != 0) {
		cout << "ID? ";
		cin >> tempid;
		cout << "The value for student with ID of " << tempid << " is ";
		i = 0;
		
	
	}

If this is confusing at all, please indicate it so that I can clarify. I appreciate the assistance!
~Ems
You need to go through the Student list[] looking for the user ID, and if you find it display the value.

for (i; i<11; i++) { 11 will let you go our of bounds, i must be less than 10 (an array with 10 elements has addresses 0-9).

Doh, thanks for that.

I understand that I'll need to go through the Student list[], but I am not sure how to do that...
You should use a loop and compare all of the ids from the list with the id your searching and if they are the same then print the element from the list.
1
2
for(i;i<11;i++)
if(list[i].ID==tempid)cout<<what ever you need here : )

Is this what you were looking for? If not then please excuse me : )
Actually, I believe that IS what I'm looking for, thank you, I'll try that out straight away!
Okay, so here's my new code, AND I've got a "Student" class and header file all set up.
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
#include "student.h"

#include <iostream>
using namespace std;
int main()
{
	const int MAX = 10;
	int tempid = 1;
	double tempval = 0.0;
	int check = 1;
	int i = 0;
	Student list[10];
	cout << "Welcome! This program allows the user to input students (id and value)." << endl << "You cannot input more than 10 students." << endl;
	cout << "Please input one line with the student id (positive integer value) followed" << endl << "by a line with the student value (real value). Indicate the last student by giving a 0" << endl;
	cout << "Enter values now: " << endl;
	for (i; i<10; i++) {

		while (tempid != 0) {
			cout << "ID?: ";
			cin >> tempid;

			cout << "value?: ";
			cin >> tempval;
			
			Student temporary(tempid, tempval);
			list[i] = temporary;
		}
		
	}
	cout << "You are now done entering students." << endl << "You can now find the value of a student by entering the ID." << endl;
	cout << "Please input on line with the student id (integer value)" << endl << "Indicate the desire to stop by giving 0. Enter IDs now:" << endl;
	i = 0;
	while (check != 0) {
		cout << "ID? ";
		cin >> tempid;
		cout << "The value for student with ID of " << tempid << " is ";
		for(i;i<10;i++){
			if(list[i-1].get_id() == tempid){
				cout << list[i-1].get_val();
			}
		}
	}
}


The first half of the code (getting the user to input properly) seems to work well, but I'm still having problems getting the program to feed back the value of the Student in the array. Can anyone see what I'm doing wrong?
The first half of the code (getting the user to input properly) seems to work well,


Does it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	for (i; i<10; i++) {

		while (tempid != 0) {
			cout << "ID?: ";
			cin >> tempid;

			cout << "value?: ";
			cin >> tempval;
			
			Student temporary(tempid, tempval);
			list[i] = temporary;
		}
		
	}


This code asks the user to input a list of ids and and values 10 times. It only stores one of the ids/values entered each time, and that id is always 0.

Btw, why are you declaring every variable at the top of your function? Prefer to declare variables as close as possible to where they're used.

1
2
3
4
5
6
7
8
9
10
11
12
13
for ( unsigned i=0; i<10; ++i )
{
     int idNo ;
     cout << "ID?: " ;
     cin >> idNo ;

     int value ;
     cout << "value?: " ;
     cin >> value ;

     list[i] = Student(idNo, value) ;
}

Yeah, I noticed that it isn't storing properly. :(
I declare at the top of my function purely out of habit.
Your code appears to be what I need, but I altered it a bit to make it jump out of the loop when the user enters a zero. I'm still having trouble with the second half of the program though. Argh!
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
#include "student.h"

#include <iostream>
using namespace std;
int main()
{
	const int MAX = 10;
	int tempid = 1;
	double tempval = 0.0;
	int check = 1;
	int i = 0;
	Student list[10];
	cout << "Welcome! This program allows the user to input students (id and value)." << endl << "You cannot input more than 10 students." << endl;
	cout << "Please input one line with the student id (positive integer value) followed" << endl << "by a line with the student value (real value). Indicate the last student by giving a 0" << endl;
	cout << "IDs must be different for each student for this program to function. " << "Enter values now: " << endl;
	//for (i; i<10; i++) {

		
			for (i; i<10; i++) {
			cout << "ID?: ";
			cin >> tempid;
			if (tempid == 0){
				break;
			}
			cout << "value?: ";
			cin >> tempval;
			
			list[i] = Student(tempid, tempval);
			}
		
		
	//}
	cout << "You are now done entering students." << endl << "You can now find the value of a student by entering the ID." << endl;
	cout << "Please input on line with the student id (integer value)" << endl << "Indicate the desire to stop by giving 0. Enter IDs now:" << endl;
	i = 0;
	tempid = 1;
	while (tempid != 0) {
		cout << "ID? ";
		cin >> tempid;
		cout << "The value for student with ID of " << tempid << " is ";
		for(i;i<10;i++){
			if(list[i].get_id() == tempid){
				cout << list[i].get_val();
				break;
			}
		}
	}
}
Taking into account that you break the loop after entering 0 for tempid

1
2
3
4
5
6
7
8
9
10
11
			for (i; i<10; i++) {
			cout << "ID?: ";
			cin >> tempid;
			if (tempid == 0){
				break;
			}
			cout << "value?: ";
			cin >> tempval;
			
			list[i] = Student(tempid, tempval);
			}



You shall use further the number of entered elemnts in the loop where you are doing the search.
I declare at the top of my function purely out of habit.


It's a habit you might consider breaking. You might dismiss the suggestion because it's purely stylistic. There's no technical reason you can't declare variables at the top of a function.

There are really two reasons for adopting a style of writing code. One is readability. The second is the possibility to reduce opportunities for errors to creep in.

When you declare a variable and it is some lines of code before you use it, the possibility that you inadvertently modify the value to something inappropriate between the time it's declared and when you use it exists. That possibility does not exist, or at least the possibility can be greatly mitigated by putting the declaration/definition as close as possible to the first use.

It also increases readability. You don't have to check all the intervening lines of code to make sure nothing is changing/accessing a variable it shouldn't. You don't have to look far from it's first use to find the type of the variable or it's initial value.


Your code appears to be what I need, but I altered it a bit to make it jump out of the loop when the user enters a zero.


I assumed what you were trying to do was make sure a user didn't attempt to enter an invalid id (0), since you weren't keeping track of the number of id-value pairs you generated. For what you wanted to do, I would've used a while loop since the loop condition is really compound. I'd probably introduce a utility function to make the loop logic a little clearer as well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// convenience function to make writing the input loop simpler.
// returns false if we didn't get valid input, true if we did.
bool get( int& id, double& val )
{
	cout << "ID: " ;
	if ( cin >> id  &&  id != 0 )
	{
		cout << "value: " ;
		if ( cin >> val )
			return true ;
	}

	if ( !cin )   // We've had invalid input
	{
		cin.clear() ;  // clear error state
		cin.ignore(std::numeric_limits<streamsize>::max(), '\n' );  // clear rest of the current line in input.
	}

	return false ;
}


And then the loop would become:
(The {} is just to limit the scope of id and value.)
1
2
3
4
5
6
7
	unsigned nList = 0 ;   // the number of valid Students in list
	{
		int id ;
		double value ;
		while ( nList < MAX  &&  get(id,value) )
			list[nList++] = Student(id, value) ;
	}




As far as the searching goes, you should prefer to declare and define variables as close to first use as possible. Had you followed the usual convention of declaring the counter in the for loop, you wouldn't have the bug you did.

You assign 0 to i before you enter the while loop, but never do so again, so after the first 'search' you never again begin at the first element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	cout << "ID: " ;
	int id ;
	while ( cin >> id  &&  id != 0 )
	{
		unsigned i = 0 ;
		while ( i< nList  &&  list[i].get_id() != id )
			++i ;

		if ( i == nList )
			cout << "ID " << id << " was not found.\n" ;
		else
			cout << "ID " << id << " is " << list[i].get_val() << ".\n" ;

		cout << "\nID: " ;
	}



Last edited on
Topic archived. No new replies allowed.