I think I already said that... Any constructive advice?
hiddepolen 32 Posting Whiz in Training
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
I think I already said that... Any constructive advice?
OK... You asked for it :icon_twisted:
No! No labels! Please!
Why. Labels, though unnecessary, are benign. They don't do anything.
Labels are about the worst programming you can do.
I can think of many other things worse...
It makes code unreadable,
Actually, they can help document the code. Not as well as comments, but a well thought out label name can define code sections.
and is always solvable using loops.
Labels solve nothing.
Labels can often crash your program,
Labels cannot possibly crash a program. They just define a name to a place in the code.
and are impossible to debug,
Nothing to debug, they do nothing.
because of the jumps ni your code.
Only if you use goto
to jump to them.
Happy? :icon_wink:
hiddepolen 32 Posting Whiz in Training
Lol, I have never seen any code using labels, that does not contain a goto. I was asking for advice for the thread, not for me. I don't really care any more, don't make a point of it.
Please, just help me solve the thread instead of making people angry.
C++newbie chick 0 Light Poster
Great! Just keep trying: that is the only way to get 'C++ logic' :).
In some time, it will come to you, I am sure.If I'd have to change the loop, I would do it like this:
case 1: do { cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n"; cin >> stdIDInput; cout << "\nStudent ID Student Name Course Code Credit Points\n\n"; cout << setiosflags(ios::left); bool found = false; for(j = 0; j < NUMRECS; j++) { if(stdIDInput == stdRec[j].studentID) { found = true; cout << setw(12) << stdRec[j].studentID << setw(21) << stdRec[j].studentName << setw(13) << stdRec[j].courseCode << setw(5) << stdRec[j].creditPoint << endl; } } if(!found) { cout << "Not Found.\n"; } cout << "Would you like to search for a another student ? Y or N.\n"; cin >> choice; } while (choice != 'N'); pause (); break;
I made a do/while loop, so that when the user enters 1, it always searches one time. When the user enters a correct number, the user is displayed, if no users are found, the Not Found is displayed. Finally, the user is asked if he wants to search again.
Aaaaah! I get it now! My code logic was a little bit backwards in that it would ask to re-search before the search had begun- I was trying to find a way to go back to re-search, but this way is much more easy to understand! Thank-you so much for your help!
Erm, I just have one last question: as I understand, this constitutes as a linear search (as it searches all the records one by one until a match is found, right?), but what if you wanted to use something like a binary search (let's say, for option 6- add a student record)? I sort of get that it searches whether the input matches something higher or lower than the mid-point or something like that... and I kind of understand how you'd go about it when deleting a record where you'd shuffle all the records after the one you're "deleting" one down so then you'd overwrite the "deleting" one, but would you please be able to explain to me the logic in how I'd go about doing a binary search for finding the record the user would like to delete? You don't have to post the code (I feel like I've gotten enough assistance with the coding and it feels too much like cheating!), I would just like for the concept to be clarified! Pretty pretty please with a cherry on top! And thank-you so much for all the help you've already given!
raptr_dflo 48 Posting Pro
You've got the basic idea of binary search: it's the optimal strategy for the "I'm thinking of a number between 1 and 100, try to figure it out" game. You start with 50, and the other person tells you higher or lower. If he says lower, you guess 25 next, otherwise 75, always keeping track of the new range of values, approximately half the size of the previous range (thus the term "binary", dividing the search space into two equal segments).
Now, it only works if your data is sorted (like the numbers 1 to 100)!
But even strings (representing integers) can be correctly sorted, if they're guaranteed to be a constant length. Otherwise you end up with problems like "1000" comes before "123", the same way that "celebration" comes before "cent".
To do a binary search, you start with the start-of-range index at the beginning of your array, and the end-of-range index at the end of your array. Compute the index half way in between, and see if that element is the correct one. If so, you're done. Otherwise, if the record you're looking for should be before that, then the half-way index (minus one, no point looking at it again) becomes the new end-of-range. Or if the record you're looking for should be after that, then the half-way index (plus one, this time) becomes the new start-of-range. Don't forget to handle the case where the record you're looking for isn't in the set at all. What will happen to the start-of-range and end-of-range values in this case? Hint: what if your friend is annoying and chooses the value 56.3 and asks you to guess it?
MandrewP 60 Junior Poster in Training
Do NOT Follow this advice!!!!1) We do NOT - repeat NOT - recommend bad coding practices here!2) We do NOT - repeat NOT - write the code for people!3)
Ooooh boy! Here we go again, confusion run wild!
I thought I was perfectly clear in condemning the use of goto:label statements in regular program code. Here is what I said:
<< Now, I used a goto statement to make your program work. However, goto is bad bad programming practice. It's EVIL! LOL - not really evil, after all, it is a valid programming statement. However, for reasons to be discussed later, if you use goto in your program like I did, it will be condemned by academia forever. >>
<< it will be condemned by academia forever >>..... so she will submit goto's to her instructor??? I think not! I just assumed that by my condemnation of goto:label it would NOT be thought I was RECOMMENDING the practice! Yeah, I guess assumption killed the cat!
And I never said that I wrote the code, it was simply her OWN code that I added a goto statement to, and moved one line down. I let her copy and paste it to make it easier to avoid typos. This is the equivalent of a code snippet. And adding the goto statement was NOT solving the problem for her, it was only used to show and suggest program logic to solve her problem - she was to see the illustration of the goto and RE-CODE it without using any goto statements! I clearly stated that - here's the quote:
<< But I used it here just to show you the idea of how this could work. Once you see the logic of the program, then you HAVE to re-code it properly, without using any goto statements! >>
... and the last line in my reply is this:
<< See how I did it with goto and code it right! >>
See, I was suggesting a solution - not doing it for her. And also, I said the following, which is a perfectly valid use of the goto:label statement:
<< Also, consider using goto as a quick and dirty means of trying out program logic, just to see if it works, then re-code it properly. >>
There is a big difference between using goto:label statements as a permanent part of your code, which I condemned, and using goto:label temporally to test program logic and then remove it and code it properly. The latter is perfectly acceptable use of goto!
Maybe this statement of mine didn't help the confusion:
<< If you use goto at all, it should only be in the privacy of your own home - lol. >>
I just meant to cast a bad light on using goto permanently in code - NOT to do it anyway in secret!. In reality, the only valid criticism here is the bad formatting of the code. I knew I messed up the formatting of the do while{} loop, but failed to correct it before I posted.
My bad. (but then I never promised anyone a rose garden!)
C++newbie chick 0 Light Poster
You've got the basic idea of binary search: it's the optimal strategy for the "I'm thinking of a number between 1 and 100, try to figure it out" game. You start with 50, and the other person tells you higher or lower. If he says lower, you guess 25 next, otherwise 75, always keeping track of the new range of values, approximately half the size of the previous range (thus the term "binary", dividing the search space into two equal segments).
Now, it only works if your data is sorted (like the numbers 1 to 100)!
But even strings (representing integers) can be correctly sorted, if they're guaranteed to be a constant length. Otherwise you end up with problems like "1000" comes before "123", the same way that "celebration" comes before "cent".
To do a binary search, you start with the start-of-range index at the beginning of your array, and the end-of-range index at the end of your array. Compute the index half way in between, and see if that element is the correct one. If so, you're done. Otherwise, if the record you're looking for should be before that, then the half-way index (minus one, no point looking at it again) becomes the new end-of-range. Or if the record you're looking for should be after that, then the half-way index (plus one, this time) becomes the new start-of-range. Don't forget to handle the case where the record you're looking for isn't in the set at all. What will happen to the start-of-range and end-of-range values in this case? Hint: what if your friend is annoying and chooses the value 56.3 and asks you to guess it?
Ahhh, okay so then it's a more efficient search than the linear one! I found an algorithm for it which I quickly scribbled down from one of my lectures, but I just have one question: how could I make it apply to search an array of structs? Just to search for a student ID which is a string not of int type... I don't know whether I should declare the first, last & mid points as strings as well because this leads to another question: is the count dependent on type? If you could answer, it would be AWESOME! Please and thank-you!
int first = 0;
int last = length - 1;
int mid;
bool found = false;
while(first <= last && !found)
mid = (first + last)/2;
if(list[mid] == item)
found = true;
else if (list[mid] > item)
last = mid -1;
else
first = mid + 1;
if(!found)
cout << "NOT FOUND";
EDIT: I've started to try to do it, but now I am stuck! How am I supposed to display the student record? I only know how to do it the linear way... here's what I have so far:
int first = 0;
int last = NUMRECS;
int mid = (first + last)/2;
cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
cin >> stdIDIn;
bool found = false;
while (first <= last && !found)
{
if (stdRec[mid] == stdIDIn)
{
found = true;
cout << // now I'm stuck!
Edited by C++newbie chick because: n/a
raptr_dflo 48 Posting Pro
The count of items in your array is not dependent on the type, plus, you should be keeping track of how many items you've read in (so use "length - 1" rather than "NUMRECS" -- you might allocate more records than you actually fill, you don't want to search the empty ones).
As far as outputting your record, you already have code that does that at lines 75 and 96 of your original posting back on "Page 1" of this thread:
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
just replacing [i]
with [mid]
.
Don't forget, binary search through your stdRec array will only work if it's already sorted in increasing order of studentID string. Options include:
+ keep your list sorted at all times
+ make sure your list gets sorted before searching
+ go back to a straight linear search through your potentially-unsorted list
Unless your assignment specifically indicates otherwise -- or if you've covered sorting in your course, then presumably you should know to incorporate that concept in your work -- any of the above should be acceptable.
C++newbie chick 0 Light Poster
The count of items in your array is not dependent on the type, plus, you should be keeping track of how many items you've read in (so use "length - 1" rather than "NUMRECS" -- you might allocate more records than you actually fill, you don't want to search the empty ones).
As far as outputting your record, you already have code that does that at lines 75 and 96 of your original posting back on "Page 1" of this thread:
cout << setw(12) << stdRec[i].studentID << setw(21) << stdRec[i].studentName << setw(13) << stdRec[i].courseCode << setw(5) << stdRec[i].creditPoint << endl;
just replacing
[i]
with[mid]
.Don't forget, binary search through your stdRec array will only work if it's already sorted in increasing order of studentID string. Options include:
+ keep your list sorted at all times
+ make sure your list gets sorted before searching
+ go back to a straight linear search through your potentially-unsorted listUnless your assignment specifically indicates otherwise -- or if you've covered sorting in your course, then presumably you should know to incorporate that concept in your work -- any of the above should be acceptable.
Ahhhh! Thanks for the info! I didn't know you could do that, errr... but thanks so much!
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.