void InputFunction()
{
	system("cls");
	printf("\n");
	printf("\t\t   ***************************************\n");
	printf("\t\t   ***************************************\n");
	printf("\t\t   ***                                 ***\n");
	printf("\t\t   ***           WELCOME TO            ***\n");
	printf("\t\t   ***    CLASS ATTENDANCE SYSTEM      ***\n");
	printf("\t\t   ***                                 ***\n");
	printf("\t\t   ***************************************\n");
	printf("\t\t   ***************************************\n");
	printf("\n\n");

	fflush(stdin);
	printf("\t\t   What Is The Course Code? ");
	scanf("%s", SubCode);
	fflush(stdin);

	fflush(stdin);
	printf("\t\t   How Many of The Total Weeks? ");
	scanf("%d", &week);
	while(isalpha(week))
	{
		fflush(stdin);
		printf("\t\t   You Have Enter Out of Range\n");
		printf("\t\t   Please Re-enter Again\n");
		printf("\t\t   How Many Weeks? ");
		scanf("%d", &week);
	}

	printf("\t\t   How Many Students In This Class? ");
	scanf("%d", &student);
	while(isalpha(student))
	{
		fflush(stdin);
		printf("\t\t   You Have Enter Out of Range\n");
		printf("\t\t   Please Re-enter Again\n");
		printf("\t\t   How Many Students In This Class? ");
		scanf("%d", &student);
	}

Let's assume the user input alphabet, it should goes into the while loop and scanf again right.?
but my code is doesn't work like that, if user input invalid input, the program will straight away jump back to the beginning..
why? any problem with my code?

no..
it is no working properly..
i copy in another way of source code..

#include <stdio.h>
#include <ctype.h>
void main()
{
	char SubCode[30];
	int week, student;

	printf("What Is The Course Code? ");
	scanf("%s", SubCode);
	fflush(stdin);

	fflush(stdin);
	printf("How Many of The Total Weeks? ");
	scanf("%d", &week);
	while(isalpha(week))
	{
		fflush(stdin);
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many Weeks? ");
		scanf("%d", &week);
	}

	printf("How Many Students In This Class? ");
	scanf("%d", &student);
	while(isalpha(student))
	{
		fflush(stdin);
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many Students In This Class? ");
		scanf("%d", &student);
	}
}

A suggestion .. why not make use of the fact that scanf() returns the number of items read?
Something along the lines of the following ..

#include <stdio.h>

int main()
{
  /* Generally, initialize your variables. */
  int week = 0, scanned = 0;
  
  printf("How Many of The Total Weeks? ");

  /* Loop until one int has been scanned .. */
  while((scanned = scanf("%d", &week)) != 1)
  {
    printf("Input failure, scanned --> %d, weeks?\n", scanned);

    /* fflush(stdin) IS a bad practice, eventually replace it with something better. */
    fflush(stdin);
  }

  printf("Weeks --> %d\n", week);
  return 0;
}

As far as I'm seeing, you aren't defining what is right or what is out of range before or after the user inputs whatever is supposed to be there.

printf("\t\t   How Many of The Total Weeks? ");
	scanf("%d", &week);
	while(isalpha(week))
	{
		fflush(stdin);
		printf("\t\t   You Have Enter Out of Range\n");
		printf("\t\t   Please Re-enter Again\n");
		printf("\t\t   How Many Weeks? ");
		scanf("%d", &week);
	}

Unless I'm not seeing right. Does (Isalpha) check the variable against something in some way?

Also in this piece of code, you're missing an & , perhaps that is causing you errors?

printf("\t\t What Is The Course Code? ");
scanf("%s", SubCode);

@gahhon

ya, your program is behaving so weird its because when we are pressing any alpthabet from keyboard its taking a garbage value..

int isalpha(int c);

The c argument is an int, the value of which the application shall ensure is representable as an unsigned char(0-255) or equal to the value of the macro EOF. If the argument has any other value, the behavior is undefined.

As far as I'm seeing, you aren't defining what is right or what is out of range before or after the user inputs whatever is supposed to be there.

printf("\t\t   How Many of The Total Weeks? ");
	scanf("%d", &week);
	while(isalpha(week))
	{
		fflush(stdin);
		printf("\t\t   You Have Enter Out of Range\n");
		printf("\t\t   Please Re-enter Again\n");
		printf("\t\t   How Many Weeks? ");
		scanf("%d", &week);
	}

Unless I'm not seeing right. Does (Isalpha) check the variable against something in some way?

Also in this piece of code, you're missing an & , perhaps that is causing you errors?

printf("\t\t What Is The Course Code? ");
scanf("%s", SubCode);

i'm trying to check whether the student & week is int type or not..
that's why i put isalpha(), i tried put isdigit()
but it is keep looping no matter what is user input..

A suggestion .. why not make use of the fact that scanf() returns the number of items read?
Something along the lines of the following ..

#include <stdio.h>

int main()
{
  /* Generally, initialize your variables. */
  int week = 0, scanned = 0;
  
  printf("How Many of The Total Weeks? ");

  /* Loop until one int has been scanned .. */
  while((scanned = scanf("%d", &week)) != 1)
  {
    printf("Input failure, scanned --> %d, weeks?\n", scanned);

    /* fflush(stdin) IS a bad practice, eventually replace it with something better. */
    fflush(stdin);
  }

  printf("Weeks --> %d\n", week);
  return 0;
}

i quite understand your suggestion..
but this statement,

while((scanned = scanf("%d", &week)) != 1)

i quite not understand.. which mean you scanf an int type and assign into the week, as the same time you used scanned variable to receive is that an int type value, it will return positive 1 if it is an int type, or else return -1. am i correct? :-O

The loop attempts to get an integer from stdin storing this integer in week . If this succeeds, scanned , which stores the return value of scanf() , will equal 1 (because a single value was being scanned), otherwise scanned equals either zero or EOF (you might test how your scanf() works with various faulty input, including e.g. Ctrl-Z).

i'm trying to check whether the student & week is int type or not..
that's why i put isalpha(), i tried put isdigit()
but it is keep looping no matter what is user input..

Oh I feel stupid now. My eyes went over (isaplha) thinking that was apart of a library I never used before, doh, my mistake.

The loop attempts to get an integer from stdin storing this integer in week . If this succeeds, scanned , which stores the return value of scanf() , will equal 1 (because a single value was being scanned), otherwise scanned equals either zero or EOF (you might test how your scanf() works with various faulty input, including e.g. Ctrl-Z).

awh.. i'll try it..

Oh I feel stupid now. My eyes went over (isaplha) thinking that was apart of a library I never used before, doh, my mistake.

oh.. never mind dude...

i have try to use
while(scanned = scanf("%d", &week) != 1)
it is giving me the same result as what i do in beginning...
unlimited looping or else is straight back to the beginning of the function, it can't get back to ask the week question..

>> i have try to use while(scanned = scanf("%d", &week) != 1) Note that you have omitted two parenthesis, it should rather read

while((scanned = scanf("%d", &week)) != 1)

Otherwise scanned will only equal 0 or 1, (i.e. never EOF ).
Anyhow, you might post your most recent attempt.

I'm slightly confused now, if you're asking for an integer, why use !=1? What if the user needs to put in (1 week) or however it may ask for the input? Wouldn't EOF suffice in this scenario?

that's why..
maybe confused already..
i tell again what am i going to do,
i'm going to check the user input is it int type..
if there is not an int type will going into the while loop and ask the question again..
again and again until it get an int type.

How about if you use an if then statement, with a while loop inside of it?
You can make an if statement that will check if the (week) is not an int, and then execute the while loop, so that if it is an INT is will ignore the while loop?
I know that's what you were trying to do with the while loop in the first place, maybe the if statement can fix this?

okk... try this code, i made little change to your code..

#include <stdio.h>
#include <ctype.h>
int main()
{
	char SubCode[30];
	int week, student;

	printf("What Is The Course Code? ");
	scanf("%s", SubCode);
	fflush(stdin);

	printf("How Many of The Total Weeks? ");

	while(scanf("%d", &week)!=1)
	{
		fflush(stdin);
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many of The Total Weeks? ");

	}

	printf("How Many Students In This Class? ");

	while(scanf("%d", &student)!=1)
	{
		fflush(stdin);
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many Students In This Class? ");
	}
  return 0;
 }

okk... try this code, i made little change to your code..

#include <stdio.h>
#include <ctype.h>
int main()
{
	char SubCode[30];
	int week, student;

	printf("What Is The Course Code? ");
	scanf("%s", SubCode);
	fflush(stdin);

	printf("How Many of The Total Weeks? ");

	while(scanf("%d", &week)!=1)
	{
		fflush(stdin);
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many of The Total Weeks? ");

	}

	printf("How Many Students In This Class? ");

	while(scanf("%d", &student)!=1)
	{
		fflush(stdin);
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many Students In This Class? ");
	}
  return 0;
 }

it is work dude..
but i have something still understand about the checking..
!= 1, in the beginning i thought you're checking is that student or week equal to 1 or not... if not go into the loop... but i tried to put exceed than 1, and it work..
why?

How about if you use an if then statement, with a while loop inside of it?
You can make an if statement that will check if the (week) is not an int, and then execute the while loop, so that if it is an INT is will ignore the while loop?
I know that's what you were trying to do with the while loop in the first place, maybe the if statement can fix this?

dude, im trying to use vinitmittal2008 code,
it is work successfuly.
by the way, i wanna ask how come my while loop with isalpha() or isdigit() statement to check the condition is not work?

scanf("%d", &week);

The vast majority of functions will give us some indication of success or failure.
Especially with input it's best to make sure that the call succeeded before
proceeding as if it did. In the case of scanf,
a successful call will return the number of items converted:

if (scanf("%d",&week) == 1) {
  /* Success */
}
else {
  /* Failure */
}

Well from what I understand, using isalpha is checking the variable to see if it is a character or not, I don't know how well it works with a (while) loop. If you used an if statement like vinitmittal2008 used above, the program will know what to do in case the variable is not a number or letter, where using a while loop right after might cause some trouble, like if the compiler is expecting the while loop, not checking to see if it can be run or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.