I'm studying a C++ book and I came across a program in the book that I don't quite understand and was wondering if anyone can explain in detail of what's going on; basically line by line if that's possible and would greatly be appreciated it.
The program basically outputs the index position of the word "three" in the string: "one two three four"
Where I am mostly confused is in the find_substr function, within the for loop and especially the while loop.
// Functions 12 (From functions chapter, 7)
// Find substring in a string.
#include <iostream>
usingnamespace std;
int find_substr(char *sub, char *str);
int main()
{
int index;
index = find_substr("three", "one two three four");
cout << "Index of three is " << index; // index is 8
return 0;
}
// Return index of substring or -1 if not found.
int find_substr(char *sub, char *str)
{
int t;
char *p, *p2;
for(t=0; str[t]; t++)
{
p = &str[t]; // reset pointers
p2 = sub;
while(*p2 && *p2==*p) // check for substring
{
p++;
p2++;
}
/* If at end of p2 (i.e., substring), then
a match has been found. */
if(!*p2) return t; // return index of match
}
return -1; // no match found
}
You will actually learn more by using F10 and F11 in your compiler. F10 will run one line of code with each press meanwhile F11 will do the same except if you press it on a function, it will go line by line through that function also where as F10 will skip it.
These are two very important tools even for advanced C++ users because we use them to debug and find errors. It will help you learn much faster then us explaining it because you can see the variables and how they are changed line by line. Try it out!
Just to add to William's advice. Depending on the compiler your using, you'll probably want to set a 'watch' (if available) on things that you expect to change so that you can see more clearly when they actually do change. If you try this though and you're still stuck with a couple of problems let us know.
Right now I'm using the Borland compiler; where you compile through the windows cmd. I'm not sure if what William described is possible to do with this compiler?
If not, can anyone recommend a good compiler that can do what William described above? That would be great because I'll be able to see execution of the program line by line. Good way to learn and understand what's going on with your programs.
With Visual Studio, according to wiki, it says it can be used to create console based programs and GUI programs.. At the moment I'm interested in going about making console programs, is Visual Studio good support for console base type programs or is it more inclined toward GUI programs?
Is there a difference between MS Visual C++ and MS Visual Studio? According to what I read, MS Visual C++ is apart of the MS Visual Studio Express family, would it then just be better to go with Visual Studio or MS Visual C++?
Visual studio is perfectly fine for console based programs, I haven't used it for GUI stuff so I can't really say much about that. Within microsoft visual studio there is the ability to program in several different languages, assuming it's only ever C++ that you will be using just download whatever it says on their website that for programming in C++ and don't worry too much about it. Besides if your only starting it doesn't matter a huge amount, just get your teeth into it and start learning C++ you can always just change to a different IDE at a later date if you think it will be better, or if you just want to try something new.