i'm doing a payroll system and i have finished it but i need some eror checking reports for name and integer but i'm going to do the integer first which wll input hours of a day from monday to friday what i need is a loop along with error checking to make sure that the hours inputted doesnt go past 24hours
i started wirting a if statement but i dont know what the hell i am doing and i am runniong out of help and i dont even have internet at home any more so pls help me show me a error checking tutorial or something pls

int add_hours(float *hours)
{
    int mo, tu, we, th, fr;
    
if(*hours<=24)    
printf("Enter daily hours for Monday:\n");
scanf("%d",&mo);
else
printf("the code you have entered is invalid please neter a number less than 24 hours");

printf("Enter daily hours for Tuesday:\n");
scanf("%d",&tu);

printf("Enter daily hours for Wednesday:\n");
scanf("%d",&we);

printf("Enter daily hours for Thursday:\n");
scanf("%d",&th);

printf("Enter daily hours for Friday:\n");
scanf("%d",&fr);
*hours=mo+tu+we+th+fr;
printf("\nThe weekly hours are:%f\n",*hours);

printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");

}

First of all, in English there are things called sentences. They use periods at the end and the next sentence starts with an upper case letter. It helps us understand your question.

Second, if you'd format your code, some of your problem would be immediately apparent, and again, we could understand your code.

Most error checking is done by
1) writing the code on paper and double/triple checking it before typing it into the compiler. You play the part of the compiler.
2) compiling the code, looking at the error and the designated line, and figuring out what was wrong with the specified line based on the error text.
3) asking an explicit question here which includes the error, the line(s) in question, and what you think the problem might be.

>zangetsuu
I can't decide if you like Bleach but only watched the English dub, or zangetsu was taken and you just added an extra 'u' to the end. If it's the former, the name Zangetsu is pronounced something like "Zahngets". The "oo" sound at the end is virtually silent when properly spoken, which means that forcing it like in the English dub ("Zahngetsoo") could easily change the entire meaning of the word.

So yea, that's your Japanese lesson for the day. ;) On to the real question.

>what i need is a loop along with error checking to make
>sure that the hours inputted doesnt go past 24hours

I'll mention that floating-point comparisons can be tricky and leave it to you to research further when you feel more comfortable with programming.

You can test whether scanf succeeded by counting the number of successful conversions returned. If that number matches the number you requested, all is well. Then you can add further tests and loop until you get what you want. Here's a simple example (that doesn't consider fixing a dirty stream):

while (scanf("%d" &mo) != 1 || !(mo >= 0 && mo <= 24)) {
  printf("Invalid input. Please try again: ");
  fflush(stdout);
}

Since you do a lot of that, it makes sense to throw it all in a separate function:

int get_hours(void)
{
  int input;

  while (scanf("%d" &mo) != 1 || !(mo >= 0 && mo <= 24)) {
    printf("Invalid input. Please try again: ");
    fflush(stdout);
  }

  return input;
}

This code isn't very robust, but I don't want to scare you with too much complexity right off the bat. It can wait until you encounter the problems caused by not being strictly correct, which you might not even see on this project.

lol omoshiroi bozu, demo... your a couple years too short to be giving me a lesson in nihongo(japanese) but at least your deductions are not off by far the proud name of "The moon visible in the morning" had already been taken so just added on the extra u like i always do lol but its nice to know that there are people out there who use deduction skills rather than just trying to disrespect a person

gomenosai wasureta domo, demo someone else already beat u to the code but again thx much and u seem like a very interesting person my email is snipped if u ever wanna chat nihongo lol

Edited 6 Years Ago by WaltP: Do NOT post email addresses on the forums.

This article has been dead for over six months. Start a new discussion instead.