views:

112

answers:

3

I am going to be making a program that reads in a line and gets up to 6 numbers. 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?

A: 

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.

Michael Dorgan
Yes I understand this part.
pisfire
+1  A: 

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.

John at CashCommons
ok. I will do that.
pisfire
Post it assuming you're allowed to by your instructor, of course. ;)
John at CashCommons
+1  A: 

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.

Péter Török
Unit testing for a 2 week old C coder? This person probably just wrote hello world. Are you sure you want to go here already?
Michael Dorgan
@Michael, I think the earlier you start, the better :-) And since the OP seems to be actively thinking about use cases to cover, it occurred to me unit testing may be a good fit for him/her. Should there be a problem, there are lots of people here on SO to help, isn't it? :-)
Péter Török
@Micheal I did, I wrote the hello world code, but I have no choice we were given this assignment, and I have a week to do it. That's why I was brainstorming.
pisfire