This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you Programming Errors (C++). Following along are instructions in the video below:
okay so in this video I want to look at the four different types of errors that you can get in C++ so thats the compile time only time run time and logic errors so lets go over to eclipse and take a look at a small program and which will exhibit all four of these errors okay so lets take a look at this program where were going to ask the user to input some number of toys and also some number of kids and then were going to figure out how many toys we could allocate evenly to each kid so we can see here in our main function where we actually ask the user to enter in the number of toys then we will store that value into integer variable called toys and we do the same basic operation with kids and then if we have our toys being greater than the number of kids and what well do is well say each kid can have and then well call this function kids per toy which will calculate the number of toys per kid and well output that to the user otherwise else well say that theres not enough toys for each kid okay so lets go ahead and compile this program and see if we get any sort of errors so again were looking for compile time link time run time and logic errors so we go here and build this and so whenever we build this particular program it says we have an error and it says expected comma or semicolon before C out so let me go ahead and double click on this so if you go down to the console here reports the warnings and errors and you double click on a particular error or warning itll actually take you to the line where its expecting something different than what youve provided so if we look at this particular line of code here well find out
that theres really not much wrong with this theres nothing wrong with this line of code it actually turns out that the line of code above it it has the problem and we missed a semicolon here so let me go ahead and put that semicolon in and a lot of times when you get compile time error messages they may not make much sense in the actual error you know may report a arrow on line 105 and the error is actually on line 35 for whatever reason so this is just going to be something that requires a little bit of Investigation and looking at so if you having a lot of problems with ticker error that youre getting from the compiler I would recommend going online and searching for a solution a lot of times you will find forums or other sites that may list out several different errors for a particular language and it may provide a better explanation okay so lets go ahead and save this and well compile our build it once again and this time we didnt get in compile time errors but we got an undefined reference to this function toys per kid so this is a link time error was expecting us to have some particular function definition for this function that we declared so we declared the function here we call the function but we never had an actual function definition so this was a link time error so we can just define this particular function so well say nth toys per kid we pass in an int and Ill just abbreviate toys as T another int okay I guess you can put kids so well go ahead and put toys and kids in here so these are actually separate variables from the toys and kids up here so these in memory would be two different values they just happen to have the same name and here well just simply return the expression
of toys divided by kids so were just simply dividing the number of toys by the number of kids so lets go ahead and save that and build it again so thats compiling and linking and this time we did not get any type of compile time errors or link time errors just as you did with a compile time errors you can also go online and search for link time errors okay so now that weve got everything compiling and linking we can actually run this program so lets go ahead and run it and it says enter the number of toys and well say that we have 10 toys and well say that in this particular case maybe no kids showed up to receive any toys so were going to try to a lot each kid with the same number or toys but no kids showed up so were gonna put in zero and whenever we try to do this operation here we actually crash our program we end up with a run time error in this case Im gonna go ahead and close this program we should be able to stay in to stay in Eclipse so basically just terminated our program but we ended up with a runtime error because we had to divide by zero since our number of toys was ten and the number of kids was zero we divided by zero which was undefined and thats what crashed our program it did not know how to handle that so that would be an example of a runtime error so lets go ahead and maybe change up our code here and just put in a condition say if kids is greater than zero then well do this ah do these operations here someone just select all this code here and tab it over and come down and do that and then Im gonna put an else statement in here and maybe say see out no kids showed
up showed up for the toys okay so thats what well output to the user if it turns out that theres zero kids so were performing that check to see if the kids is greater than zero if thats not the case well just say no kids showed up now were still not checking to see if a negative value was inputted you know if a negative value was inputted we could say that wed still be saying no kids showed up in reality thats really not what we should be saying we should ensure that non-negative values were inputted for toys and kids so you would have to set up maybe a do-while loop around toys and kids to ensure that you dont get any negative values to validate that input okay so lets do one more run and well save this and build it okay so the changes we made didnt result in new sort of compile time or link time errors so well run this program again and this time were going to put in ten toys and well put in ten kids and it says not enough toys for each kid so if you go back and look at the way we constructed this we had this test this condition set up to see if toys is greater than kids and then we would calculate how much each kid we how many toys each kid would get and thats not necessarily what we should be doing because if we had the same number of toys and kids as we did in this case each one of the kids could still get one toy but were having it output not enough toys for each kid so lets go ahead and maybe change this this would be a case of a logic error everything was okay in terms of us compiling linking we didnt get a runtime error but we didnt get the result of what we expected this is the
case with logic errors whenever we run our program somewhere the way we were thinking about this program and the way we constructed it everything was okay but the logic was just bad so let me go ahead and change this to greater than or equal to kids so go ahead and build this again so compiling and linking we didnt get any compiler link time problems and well run it again and well test it out so 10 and 10 so in this case it says each kid can have one toy and thats what we would expect if we had the same number of toys and kids this is some just simple examples of you know run time compile time link time and logic errors theres some other logic errors in terms of us validating the input here but this gives you just an example of the different types of errors that you can get in C++ and how you can go about correcting them when you first start programming in C++ most of your errors are probably going to be related to compile time or link time errors it may be a problem with your syntax you may be having issues with the types being mismatched what you were asking for for particularly and what was being inputted you know could be something very different this could happen at runtime as well you may have some sort of issue with asking for a particular type and the user inputs a string instead of a floating point value or something like that its not able to handle that input runtime so usually when you first start programming its compile time and link time problems then youll get into more runtime issues and then youll spend most of your time once you become comfortable with a particular language to looking at logic errors and spending your time figuring out where did your logic go wrong all right so thats it for this video
C++, syntax error, compile time error, runtime error, link-time error, logic error
Thank you for watching all the articles on the topic Programming Errors (C++). All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.