![]() |
|
#1
|
|||
|
|||
Linear SearchThe search function I have isn't working, possibly because of the scope of the while & if functions or that I am using the wrong functions.
Please see if a different search or maybe if the code needs changing, Thank you button. Code:
|
|
#2
|
|||
|
|||
|
theres only one thing wrong with ur linear search mechanism, otherwise it's ok. Linear search is the only way to search thru unsorted databases. there were a few things wrong with ur code.
1. you do not need file.open("database") after you just create a file stream so i've commented that statement out. otherwise it wasn't writing anything to your file. 2. In your output to file & display items loops, your condition is x<8 instead of x<9. so it misses outputting to file & displaying the last item in your array. either make it x<=8 or x<9. 3. Your linear search while loop did not have an exit condition. in case if the search is not in the database, you want to search thru the database and then exit the loop when you reach the end. you only had it exit if search string matched one of the items in database without controlling your count variable. so it went on spilling out of the array and into memory garbage until it found the entered string in memory's garbage and then displayed those garbage values. So i've added (x<=8 ) condition in your while loop using &&. This way it will stop when it has finished thru the entire array if it doesnt find the search string. I've changed x =1 from x = 8 and done x++ instead of x-- but it's just a matter of choice and it'd work either way. 4. i have commented out opening the file for reading input part since you were not using it anyway to make it simpler. you are using the array to search not the file. You can uncomment it when you implement reading from the file. other than that, i think it's fine. suggestions: I've added a cin.ignore which will ignore 10 spaces after your string input including '\n'. I think it's safer that way for your input streams when your entering a string. If you have strings that have spaces in them, then you must use getline function. also you may want to use a do while(num!=0) loop instead of if(num!=0) for your user input so they can keep on searching or displaying the data as much as they want to instead of just doing it only once, although you might have to play around with it because when i tried it for some reason it's getting garbage leftover from the last input and i've forgotten the way to clear the buffer, besides i din't have time... so i'll let you figure it out. below is the code: CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
|
i not sure bottom there i modify is correct or not
cause i notice that u try to store all the data in to database.txt but when u display and search all the data u just read from the array not database.txt i need to comfirm with u are u display and search from array or file if from the file, above ur and my code need to modify again __________________
challenges are make life interesting, overcome them is make life meaningful. |
|
#4
|
||||
|
||||
|
i had help u modify u code
i think should be work without an error the red code word is i modify / change 1st is ur CPP / C++ / C Code:
2nd instead of if(num!=0) i change it to CPP / C++ / C Code:
3rd CPP / C++ / C Code:
it is new value change to CPP / C++ / C Code:
4th i add and exit() for u CPP / C++ / C Code:
#include <iostream.h> //Pre Processor Directives #include <fstream.h> //Libary for Input funcitons #include <iomanip.h> //Input/Output manipulation libary #include <string.h> using namespace std; void main() { ofstream file("Database.txt"); //Create a new file //file.open("Database"); //take this line out char* name[9]; name[0] = NULL; name[1]="HP4p"; name[2]="HP5L"; name[3]="HP6p"; name[4]="Cannon2A"; name[5]="Cannon3B"; name[6]="Epson2"; name[7]="Epson3"; name[8]="Epson4A"; double cost[9]; cost[1]=32.99; cost[2]=24.99; cost[3]=30.00; cost[4]=23.45; cost[5]=31.99; cost[6]=25.00; cost[7]=30.00; cost[8]=27.99; int quantity[9]; quantity[1]=100; quantity[2]=50; quantity[3]=75; quantity[4]=30; quantity[5]=80; quantity[6]=10; quantity[7]=25; quantity[8]=30; char name8[13]="Cartridge"; char cost8[11]="£00.00p"; char quantity8[12]="Quantity"; // { whats this brace for? for (int i = 1; i <= 8 ; i++) //fixed this from x<8 to x<=8 file<<name[i]<<' '<<cost[i]<<' '<<quantity[i]<<endl; // put endl make u data look nice // } file.close(); //close the file int num=0; char sought[15]; //Declare Variable cout<<"Display all records press '1' "<<"Do a Search press '2' "<<" '0' to Quit "<<endl; cin>>num; //the looping should use while loop while(num!=1 || num!=2 || num!=0) { if (num==1) { cout<<"Total Items in stock"<<endl<<endl; // ifstream file("Database.txt",ios::in); //ios::in for input from the file cout<<setw(20)<<setfill( ' ' )<<setiosflags(ios::left)<<name8; cout<<setw(10)<<setfill( ' ' )<<setiosflags(ios::right)<<cost8; cout<<setw(12)<<setfill( ' ' )<<setiosflags(ios::right)<<quantity8<<endl; for (int x = 1; x <= 8 ; x++) //fixed this { cout<<endl<<setw(20)<<setfill( ' ' )<<setiosflags(ios::left)<<name[x]; cout<<setw( 8 )<<setfill( ' ' )<<setiosflags(ios::right)<<cost[x]; cout<<setw( 8 )<<setfill( ' ' )<<setiosflags(ios::right)<<quantity[x]; } } //cout<<endl<<endl<<"Display all records press '1' "; //cout<<"Do a Search press '2' "<<" '0' to Quit "<<endl; //cin>>num; if (num==2) { cout<<endl<<"To search by the printers make type in the name e.g HP4p: Type 'N' for no more"<<endl; cin>>sought; cin.ignore( 10,'\n' ); //added //ifstream file("Database"); //not doing anything with the file //open file for input FROM file //while(!file.eof() ) //{ //file.seekg( 0 ); //set file pointer to first character if (strcmp(sought,"N")!=0) { delete [] name[0]; //it is safe to call delete [] on NULL pointer. name[0] = new char[strlen(sought)]; strcpy(name[0],sought); //must declare X again, cause it is not relevent with above x int x=1; while((strcmp(name[x],sought)!=0)&&(x<=8 ) ) //added loop exit condition { x++;} if (strcmp(name[x],sought)==0) { cout<<endl<<setw(20)<<setfill( ' ' )<<setiosflags(ios::left)<<name[x]; cout<<setw( 8 )<<setfill( ' ' )<<setiosflags(ios::right)<<cost[x]; cout<<setw( 8 )<<setfill( ' ' )<<setiosflags(ios::right)<<quantity[x]<<endl; } else cout<<"No such item in stock"<<endl<<endl; //file.close(); //cout<<"Display all records press '1' "; //cout<<"Do a Search press '2' "<<" '0' to Quit"<<endl; //cin>>num; } } if (num==0) // when key in 0 auto exit exit(0); cout<<"\nDisplay all records press '1' "; cout<<"Do a Search press '2' "<<" '0' to Quit"<<endl; cin>>num; } } __________________
challenges are make life interesting, overcome them is make life meaningful. |
|
#5
|
|||
|
|||
|
Quote:
first, you wouldnt need to have multiple comparisons in your loop. second, your user choice input would only need be included once, inside your do while loop third, you wouldnt have to call exit(0); try not to use exit() in your programs unless need be. Quote:
in such a case you can reuse variables and i think one should to save memory. why waste memory? every little byte counts. |
|
#6
|
|||
|
|||
|
I've reposted the code with a do while loop this time, i've tested it, and it works perfectly... i think...
CPP / C++ / C Code:
|
|
#7
|
||||
|
||||
|
Quote:
Let me also add that reusing variable is a good idea. It keeps the code cleaner, but memory is not an issue generally. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#8
|
||||
|
||||
|
correct do.. while really better than while.. in this case
but for CPP / C++ / C Code:
it will generate compile error for my compiler CPP / C++ / C Code:
i duno what compiler u all using i think put int x=1 is correct 1 cause ur int x is declare in the nested but the x=1 is outside the nested for standard C++ x will find the own declare at same nested, once it cannot found it will look more outside nested and it will not look into inside nested __________________
challenges are make life interesting, overcome them is make life meaningful. |
|
#9
|
|||
|
|||
|
yes walt i agree that in small programs a little byte means nothing, but it's good practice to try and make your programs as efficient as possible since many of us might go on to program very ambitious programs at some point in our lifetime (i hope i do). Either way, i just hate wasting space knowingly, maybe i am too obsessive. And no, i wasn't even born in 70s, altho i wish i had programmed with PDP machines to know the kind of machine level stuff that goes on in our little metal boxes...
Tay most of us using windows use VC++ or Borland c++ compilers and for linux gcc |
|
#10
|
||||
|
||||
|
Quote:
__________________
-Aaron |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linear search on a multidimensional array. | anignna | C++ Forum | 4 | 07-Mar-2004 21:07 |
| weird search problem!! | JUNK KED | Open Discussion Forum | 3 | 11-Oct-2003 01:48 |
| How a search engine really works (In english) | jrobbio | Open Discussion Forum | 0 | 06-Jul-2003 18:13 |
| Search Engine Positioning 101 and 201 "How To" Tips... | 000 | Search Engine Optimization Forum | 0 | 29-May-2003 11:34 |
| [class] 404 search function code | jrobbio | MySQL / PHP Forum | 6 | 22-Apr-2003 10:32 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The