CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2012
    Posts
    4

    Help required with searching

    Hi guys,

    I am having some trouble with a search script I am writing.

    The program I am writing takes two inputs (A and B) and returns a direct route from A to B if possible. If a direct route isn't possible then additional stops would need to be added. I have routes saved in a text file so to get from A to B wouldn't be a problem but if the user wants to go to A to C but no direct route is possible then the program should be able to offer A to B to C as A-B and B-C are valid direct routes so they can both be combined.

    This is what I have written so far for this part of the program.

    Code:
    vector<Route> routes; // contains all routes (A-B, B-C, A-D, etc)
    vector<Route> directRoutes; // if any direct routes are available then they would be added to this vector to allow the user to choose one
    vector<Route> nonDirectRoutes; // non direct routes would be added here
    string startPlace; // user enters start place
    string endPlace; // user enters end place
    
    for(int j=0; j < routes.size(); j++){
    			if(startPlace== routes[j].getRoute().substr(0,1) || endPlace == routes[j].getRoute().substr(2,1)){
    				directRoutes.push_back(routes[j]);
    				for(int k=0; k < directRoutes.size(); k++){
    					if(directRoutes[k].getRoute().substr(2,1) == directRoutes[k].getRoute().substr(0,1)){
    						nonDirectRoutes.push_back(directRoutes[k]);
    					}
    				}
    			}
    		}
    Now the code above doesn't work entirely as I thought it would. How I have got it set up at the moment is as follows;

    1. routes vector contains the following A-B, A-D, A-E, B-C, C-E, B-F
    2. Users enters A for startPlace and C for endPlace
    3. Program checks all routes and adds routes that start at A and end at C to directRoutes so A-B, A-D, A-E, B-C are added

    The steps above work as intended, it is now when something is wrong

    4. Program should check directRoutes to find any routes that have a matching startPlace and endPlace so A-B and B-C should match this criteria as the B in A-B matches up with the B in B-C which would mean to get from A-C the route would be A-B and then B-C or A-B-C and this would be added to nonDirectRoutes


    I think the issue is with the == comparison that I have but I can't understand why the code I have written wouldn't work.

    Any ideas and help would be appreciated!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help required with searching

    Quote Originally Posted by enabler View Post
    Hi guys,

    I am having some trouble with a search script I am writing.
    You wrote a program, not a "script". C++ is not VBA.
    I think the issue is with the == comparison that I have but I can't understand why the code I have written wouldn't work.
    The way you understand how your code is working is by debugging the code. This means using your debugger to see where your program breaks down. You wrote the program, you must be able to debug the code that you wrote. Even if you didn't use the debugger, the old-fashioned method of printing out what you're comparing could have been done to see exactly what is behind those substr() calls.

    Assuming you're using Visual C++, you have one of the best debuggers developed for C++ programming. It is now the time to learn to use it. If we had your entire code (not just a loop plucked out of a much larger program), that is exactly how we would solve the problem -- by using the debugger. We don't eyeball code and run the computer "in our heads" to figure these things out.

    In any event, no one would be able to help you anyway, since we have no idea what's in those variables, where, when, and how that code is called, what data you used, etc. Lastly, do you have a plan on paper to solve issue 4), and have you worked through it by hand? If not, and you're trying to write the code while figuring out what to do, then that is a wrong move. You must always have a plan worked out on paper before writing one line of code.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 26th, 2012 at 07:22 PM.

  3. #3
    Join Date
    May 2012
    Posts
    4

    Re: Help required with searching

    Here is some updated code which works as a standalone program and doesn't require knowledge of the rest of the program.

    Code:
    int main() {
    	vector<string> routes; // contains all routes (A-B, B-C, A-D, etc)
    	routes.push_back("A-B");
    	routes.push_back("B-C");
    	routes.push_back("A-D");
    	routes.push_back("B-D"); 
    	vector<string> directRoutes; // if any direct routes are available then they would be added to this vector to allow the user to choose one
    	vector<string> nonDirectRoutes; // non direct routes would be added here
    	string startPlace; // user enters start place
    	string endPlace; // user enters end place
    
    	cout << "enter start place" << endl;
    	cin >> startPlace;
    
    	cout << "enter end place" << endl;
    	cin >> endPlace;
    
    	for(int j=0; j < routes.size(); j++){
    		if(startPlace == routes[j].substr(0,1) || endPlace == routes[j].substr(2,1)){
    			directRoutes.push_back(routes[j]);
    			for(int k=0; k < directRoutes.size(); k++){
    				if(directRoutes[k].substr(2,1) == directRoutes[k].substr(0,1)){
    					nonDirectRoutes.push_back(directRoutes[k]);
    				}
    			}
    		}
    	}
    	keep_window_open("q");
    }
    The code compiles fine so the debugger doesn't help. I have used cout to print out the substr() lines and what is outputted is fine which is why I don't understand why the == comparison doesn't do anything.

    With the code I have posted above, if you enter A as startPlace and C as endPlace then the directRoutes vector contains the following values A-B, B-C, A-D which is correct. Now the substr(2,1) prints B, C, D and substr(0,1) prints A, B, A. There are two B's which match up so surely they should be added to the nonDirectRoutes vector or am I missing another for loop somewhere to compare the values correctly?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help required with searching

    Quote Originally Posted by enabler View Post
    The code compiles fine so the debugger doesn't help.
    You're joking, right?

    Compiling is not the same as debugging!

    When you compile successfully, all that means is that the syntax is correct, and all the functions you're calling exists. It doesn't mean that the program will produce the results you want. What if I want a program to add two numbers, and instead by mistake, I subtracted the numbers. The code would compile fine. So how would I fix it? I debug it to see that I didn't add the two numbers.

    Debugging is taking the program you have created, and then run it under the debugger, i.e. step through the program a line at a time to see what the values of variables are, what the flow of the program is, etc. What do you think is the purpose of the "Debug" menu item on the Visual C++ IDE? Take that small sample you posted, build it, and on successful build, hit the F10 key. Then hit F10 again, and again, and again, etc. Every "and again" is your program running one step at a time.

    Then do you see the Watch or Auto, or Local window? Those are your variables and the values at that point in the program. This is how you're supposed to use the debugger, and how we would solve your problem (so you're asking us to do the job you're supposed to be doing). Again, no programmer eyeballs code and runs the computer in their heads, unless the program is such a toy, it is easy to spot the mistake.

    So debug your code by using the debugger. Debugging code is a mandatory process of learning how to write programs.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 27th, 2012 at 06:30 PM.

  5. #5
    Join Date
    May 2012
    Posts
    4

    Re: Help required with searching

    Thanks for the help. I am a beginner with programming and a complete novice with C++ having only just learned a very small amount of it within a few weeks so I require a lot more reading until I am comfortable with it. The debugging feature has proven very useful, I have not used IDEs before so I wasn't really too sure how to debug the code.

    Now I have deduced what my problem is.

    The directRoutes vector contains A-B, B-C, A-D which is correct for the inputs of A and C. Now the substr(2,1) prints B, C, D and substr(0,1) prints A, B, A. So if both of the substr() are lined up in columns, I would have the following output;

    1 2
    B A
    C B
    D A

    Now what the for loop is doing is comparing the values of each row which clearly aren't equal hence me thinking something is wrong. So now I have figured out that the program should come B in column 1 with all values in column 2 and then compare C in column 1 with all values in column 2 and finally compare D in column 1 with all values in column 2. Now only the first value of B in column 1 would equal the second value of B in column 2.

    I assume this sort of comparing would require nested for loops but I am not entirely sure how many. Am I correct in thinking just one more for loop would be required and then a comparison will be made using an int from each for loop for the index of each vector?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help required with searching

    Quote Originally Posted by enabler View Post
    I assume this sort of comparing would require nested for loops but I am not entirely sure how many. Am I correct in thinking just one more for loop would be required and then a comparison will be made using an int from each for loop for the index of each vector?
    How would you do this on paper?
    Code:
    Assume col1 is the first column, and col2 is the second column
    
    For each item i in col1, and we have not found the item, do
       For each item j in col2 and we have not found the item, do
          if ( col1[i] == col2[j] ) then
            match found;
            record values of i and j;
            record that we have found the item
         end if      
       end For j
    end For i
    Is this what you are trying to accomplish?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    May 2012
    Posts
    4

    Re: Help required with searching

    Thank you very much for your help.

    I had previously written the nested for loop which you stated but I had it placed within another for loop so the output wasn't what I was expecting. Taking the nested for loops out of a larger loop produced the results I required.

    Thanks again for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured