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: " ;
}
|