C homework, ignore for now (I think I am going repost later with some code)
I am going to be making a program that reads in a line and gets up to 6 numb开发者_JS百科ers. The program will eventually solve a a square matrix between 2x2 and 6x6. My question is what errors do I need to look for on the get_numb() function?
I am thinking that the function will have to check character by character to make sure that the individual characters are actual numbers and not a EOF or \n. I will have to also check that there is not more than 6 numbers on a line. I am about a week into programing, so is there anything I need to know to tackle this?
I absolutely recommend you start by taking into use a good unit testing framework, and write unit tests as you go. This way you can cover all the cases you mention above, and make sure that your program really works the way you think it should work.
There are loads of questions on SO about C unit testing frameworks; pick your favourite.
Apart from the cases you mention, I can think of the following:
- less than 6 numbers on a line
- empty line
- (if the numbers are floating point, various number formats)
If your teacher gave you sample input / output, you may of course incorporate that into your unit tests as well.
The potential errors you described are reasonable ones to check for.
I recommend you give it a shot. If they're not sufficient and you get stuck, then post your code and explain what you're seeing.
Most ascii to integer converters will help you out with the error checking. Here's hoping your teacher gave you some example input code and perhaps, depending on the input methods, some example conversion code. As this is homework, I don't want to get too specific.
精彩评论