Jump to content

My First From Scratch C Program


WhenKittensATK

Recommended Posts

Not sure if you've covered this yet in class but take a peek at the return values of scanf. You can view this by typing 'man scanf' into your terminal window. Again, I'm not sure how much your teacher wants you to do but you could impress him by going a bit further. If you need more help just let us know.

Unfortunately he's on Windows. :unsure:

 

No, what I was saying is:

 

It'll accept non-number input and blow up rather nicely.

It'll also accept a grade of less than 0.0 (unless it's 0.1). Try -0.2 for instance.

It'll accept a grade above 100.0

If the average grade is bigger than 100.0 - which is currently possible, as you accept grades > 100.0 - nothing at all will be printed out at the end.

:)

 

The first thing you should learn when coding is that you need to figure out how people are going to break your program and what you can do to stop it from breaking. Sadly most computer science programs don't cover this till pretty far along into the classes.

Share this post


Link to post
Share on other sites

:)

 

1- There's also not much need for the 'count' variable to be a double, it can be an int.

 

2- You don't have to have the printf() for the average score duplicated 5 times in the end if statements.

 

3- You could do a check to see if (count > 0) on the averageScore calculation to avoid dividing by zero.

 

4- It's easy to check the input is within a valid range of, say, 0.0 to 100.0. You should do this at a minimum really.

 

5- If you look at the return value of scanf() it should be easy enough to figure out how to stop it exploding when you enter non number input.

 

6- After looking at the return value of scanf() you could go one step further, and if there were no conversions done you could try and reread the buffer in to a string (aka char array), and do a check for 'done', 'quit', 'finish' or 'exit' to stop the program, instead of -0.1. hint: Do another scanf(), the input is still laying around to be read if 0 conversions are done, unless you do a fflush() or fpurge() (something you may also want to do).

Edited by markiemrboo

Share this post


Link to post
Share on other sites

If you ignore my last point about being able to read a word to quit, rather than -0.1, the rest is really rather straight forward. Reread the first four points in my previous post. You should be able to think about and do all of those yourself if you understand the code you originally posted us.

 

I appreciate you may not be aware that functions can return things, so I am more than happy to help you out with point #5 *after* you've sorted points 1 to 4 out using some good old brain power ;)

 

You can probably quite safely ignore point #6 completely, unless you're *really* keen (in which case I can attempt to help you again).

 

 

 

A do-while loop is only very slightly different from a while loop:

 

int something = 0;

do
{
  printf("I'm here\n");
  something++;   // something = something + 1;
} while (something < 0);

 

int something = 0;

while (something < 0)
{
  printf("I'm here\n");
  something++;
}

 

A do-while does the code inside the loop, then does the exit condition test. A while loop does the exit condition test, and then does the code inside the loop.

 

Run those two examples. You'll see the do-while loop prints "I'm here" once, even though something isn't smaller than 0. The plain while loop will print nothing.

Edited by markiemrboo

Share this post


Link to post
Share on other sites

Unfortunately he's on Windows. :unsure:

then google "man scanf" the first result is what you want.

 

The first thing you should learn when coding is that you need to figure out how people are going to break your program and what you can do to stop it from breaking. Sadly most computer science programs don't cover this till pretty far along into the classes.

agreed

Share this post


Link to post
Share on other sites

I wrote your program with 3 variables (you used 5). If you get rid of the redundant scanf (like markiemrboo said, but I used a while loop instead of do-while) it really helps. It all has to do with knowing what scanf returns;

These functions (scanf, fscanf, sscanf) return the number of input items assigned, which can be fewer than provided for, or even zero, in the event of a matching failure. Zero indicates that, while there was input available, no conversions were assigned; typically this is due to an invalid input character, such as an alphabetic character for a `%d' conversion.

The last sentence is the key. so if you have:

int ret;
ret = scanf("%lf", &grade);

if the user inputs a 79, ret will be equal to 1. if the user inputs the character 'a' ret will be zero. Now the trick is using that knowledge to do your loop.

 

I just thought of something else too. (I hope you realize we're trying to help you code better). What if you wanted to change the text for the output? if you want to change the "Your average score is ##" you have to change it in 5 places. You could move the print statement outside of the if statements since they are all identical.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...