How do you restrict a user to type in only positive numbers rather than alphabets if the condition is that the input has to be a positive integer otherwise the program must generate an error when the user inputs an alphabet or string.
I have an assignment that says that I have to ask the user for the number of working hours to calculate their basic pay or overtime depending on the hours.
I'm new to C and our professor has only taught us if-else and loop functions so I wrote my code like this...
Could someone please check my code and tell me how I'm supposed to stop the user from inputting a string or character value and how I can get my re entered input checked again with my if-else statements.
I will appreciate all the answers give to my query,
Thank you and have a nice day
Code:
#include<studio.h>
#include<conio.h>
main(){
int Basic_Pay = 325;
\\because this was the given pay per hour
into OverTime_Pay = 457; \\and some long calculation lol 😂
int Num_Hours; \\I'm using this variable to prompt the user for input as number of hours.
printf("Enter the number of hours worked : ");
scanf(" %d", &Num_Hours);
If(Num_Hours >= 0 && <=8) {
printf("your basic pay is : %d", Num_Hours*Basic_Pay);
}\\condition that if the number of hours is between or equal to 0 and eight display the users basic payment per hour.
Else if( Num_Hours > 8){
int cal1 = Basic_Pay*Num_Hours;
int cal2 = working hours greater than 8 - 8;
int cal3 = cal2*OverTime_Pay;
int cal4 = cal1 + cal3;
printf("your basic pay including overtime is : %d", cal4);
}
else{
printf("Error!");
}
printf(" please re-enter the input : ");
scanf(" %d", &Num_Hours);
\\ This code is the problem first it doesn't give me an error when I input an alphabet as an input fact it calculus the alphabet, secondly it does not recheck my input in the if-else statements after it has been re entered so yeah that's my problem
}