Hello.,
I'm working on my school assignment and the condition is to use post-test loop trying to get following result:
---------------------------------------------------------------
Enter the Section Code: 0

Invalid value entered. Must be 1 to 4, please re-enter: 1
Enter the Student's ID:
---------------------------------------------------------------

this is the assignment condition:

When the program starts it will prompt the user for the Section Code (a number between 1 and 4 that identifies each unique class that are running concurrently in the semester). You must use a post-test loop for this loop.

------------------

I must use post-test loop for validating the input as well as moving on to the next questions, but post-test loop executes the statement at least once so when Im trying to use do/while loop, when the condition is false(numbers selected out of 1-4 range) it goes straight back to the beginning of the loop, when i need it to jump to the second statement printf("Invalid value entered. Must be 1 to 4, please re-enter:"), is it possible to redirect within the loop?

Here is my code:

#include<stdio.h>
main()
{

int sec_code; int i;



     printf("\n");
     printf("Enter the Secion Code:");
     scanf("%d", &sec_code);

     do
        {


         printf("\n");
         printf("Ivalid value entered. Must be 1 to 4, please re-enter:");
         scanf("%d", &sec_code);
     }while(sec_code < 1 || sec_code >4);

}

Any help would be greatly appreciated

Recommended Answers

All 22 Replies

You need to test the value after reading it. If invalid, start the loop.

#include <stdio.h>

int main(void)
{
	int sec_code = 0;
	while(sec_code < 1 || sec_code > 4) {
		scanf("%d", &sec_code);
		if(sec_code < 1 || sec_code > 4) {
			printf("Ivalid value entered. Must be 1 to 4, please re-enter: ");
		}
	}
}
commented: A WHILE loop is not a POSTtest loop +15
#include <stdio.h>

int main(void)
{
	int sec_code = 0;
	while(sec_code < 1 || sec_code > 4) {
		scanf("%d", &sec_code);
		if(sec_code < 1 || sec_code > 4) {
			printf("Ivalid value entered. Must be 1 to 4, please re-enter: ");
		}
	}
}

Thanx a lot for answers, but another condition is i haveto use only loops, no if statement, break; etc., post-test loops only... its really tricky i couldnt crack this one...

You must use a post-test loop for this loop.

OK use a post-test loop. Where in your question is mentioned that you may not use for, if, break or whatever at the same time?

#include <stdio.h>
 
int main(void)
{
	int sec_code;
	scanf("%d", &sec_code);
	
	while(sec_code < 1 || sec_code > 4) {
		printf("Ivalid value entered. Must be 1 to 4, please re-enter: ");
		scanf("%d", &sec_code);
	}
}

No if statement, no break. Is it what do you want?

Here is the instrusction for the first part of the program that im trying to resolve:
This program to be written using LOOPS only. Do not use any "if" statements in your code. The use of any of the following C statement or functions is not permitted under any circumstances.
break;
continue;
exit();
abort();
All user input must be validated:
Check for non-numeric input when reading numeric input
Global variables are not permitted must be declared within the function

When the program starts it will prompt the user for the Section Code (a number between 1 and 4 that identifies each unique class that are running concurrently in the semester). You must use a post-test loop for this loop.

#include <stdio.h>
 
int main(void)
{
	int sec_code;
	scanf("%d", &sec_code);
	
	while(sec_code < 1 || sec_code > 4) {
		printf("Ivalid value entered. Must be 1 to 4, please re-enter: ");
		scanf("%d", &sec_code);
	}
}

No if statement, no break. Is it what do you want?

Thats cool, but where is initial "Enter the Section Code", its complex sped all day eysterday

Thats cool, but where is initial "Enter the Section Code", its complex sped all day eysterday

sorry has to be post-test loop

A post-test loop is a generic name for a loop of the form do { ... } while (condition), as opposed to a while(condition) {} loop.

The solution, if you have to do that, is to have the printf() statement reporting the error within the loop condition. To do that use;

1) shortcutting occurs with operations like && and ||. For example, if x is true, a test x && y does not evaluate y.

2) printf() returns the number of bytes output i.e. if it succeeds in printing your error message, it will return a non-zero (i.e. true) value.

All you need to do is set up a do-while loop and read the input inside the loop.

tried that., if i do that the do/while loop stil executes at least once with a second Invalid Input request. :(((

A post-test loop is a generic name for a loop of the form do { ... } while (condition), as opposed to a while(condition) {} loop.

The solution, if you have to do that, is to have the printf() statement reporting the error within the loop condition. To do that use;

1) shortcutting occurs with operations like && and ||. For example, if x is true, a test x && y does not evaluate y.

2) printf() returns the number of bytes output i.e. if it succeeds in printing your error message, it will return a non-zero (i.e. true) value.

im trying for over 3 hours to understand and inbed what you suggesting, but get errors during compilation, i'm very new to c, and dont fully understand the terminology yet, do you mean to put && printf("Invalid Entry"); and nest it during condition of while statement?

i feel embaressed, sorry for such goofy questions

#include <stdio.h>

void main(int argc,char * argv[])
{
	int sec_code = 0;
	/*
		The definition of 'mark' is to make the do-while loop TRUE if the first input is invalid,
		and if your first input is valid, the while test 'sec_code < 1 || sec_code > 4 || mark' will
		return FALSE.

		You can google 'shortcutting' for more details,hope this helps...
	*/
	int mark = 1;		

	printf("\n");
	printf("Enter the Secion Code: ");
	scanf("%d",&sec_code);
	do{
		printf("\n");
		printf("Invalid value entered. Must be 1 to 4, please re-enter:");
		scanf("%d",&sec_code);
		/*
			Could the follwing statement be treated as 'if statement' even though there is an description 
				"The conditional expression,written with the ternary operator "? :",provides an alternate 
				way to write this and similar constructions." 
			in the book <The C Programming Language>
		*/
		mark	= (sec_code < 1 || sec_code > 4) ? 1 : 0;
	}while(sec_code < 1 || sec_code > 4 || mark);

	printf("\n");
	printf("PASS!\n");
	printf("Your section code is: %d\n",sec_code);
	getchar();
	
}

by Kevin in Anhui University of P.R.China
Best wishes ...

im trying for over 3 hours to understand and inbed what you suggesting, but get errors during compilation, i'm very new to c, and dont fully understand the terminology yet, do you mean to put && printf("Invalid Entry"); and nest it during condition of while statement?

i feel embaressed, sorry for such goofy questions

I'll answer your question with a question.

Under what conditions will

if ((x < 1 || x > 4) && printf("Goofy"))
{
    printf(" is also known as Dippy Dawg");
}

print out "Goofy is also known as Dippy Dawg"?

void main()
{
	int sec_code=0;
	do
	{
		scanf("%d",&sec_code);
	}while((sec_code<1||sec_code>4)&&printf("invalid section code"));
}

Try this code works perfectly for me....

void main()

Wash your mouth out with soap. main() returns int.

Wash your mouth out with soap. main() returns int.

Ok i made a mistake like all other newbies [should have been int main(void)]...
but grumpier take an advice stop being grumpy
and btw wash your tongue with soap and rinse it properly with
shampoo and maybe just maybe it could wash off that
sour spot off your tongue...cheers

commented: Just take the damn advice and let it go at that. Don't pollute the thread. +0
commented: Some people can't handle what they dish out, can they? :-) +15
void main()
{
	int sec_code=0;
	do
	{
		scanf("%d",&sec_code);
	}while((sec_code<1||sec_code>4)&&printf("invalid section code"));
}

Try this code works perfectly for me....

wow


its killer!!!
works great., thanx alot! thank you ))

i didnt know you can put statements inside conditions, its great!

but grumpier take an advice stop being grumpy
and btw wash your tongue with soap and rinse it properly with
shampoo and maybe just maybe it could wash off that
sour spot off your tongue...cheers

Pfft! People earn what they get from me: good or otherwise.

tried that., if i do that the do/while loop stil executes at least once with a second Invalid Input request. :(((

Prove it. Just because you did the loop wrong does not mean the suggestion is bad. :icon_wink:

wow
its killer!!!
works great., thanx alot! thank you ))

i didnt know you can put statements inside conditions, its great!

No, it's terrible. A while statement like that is confusing. Just because you can do it doesn't mean you should.

int sec_code;
printf("enter the sec_code");
scanf("%d",&sec_code);
while(sec_ocde < 0 && sec_code > 4 )
{
printf("invalid Sec_code \n please enter between 1 to 4 ");
}

int sec_code;
printf("enter the sec_code");
scanf("%d",&sec_code);
while(sec_ocde < 0 && sec_code > 4 )
{
printf("invalid Sec_code \n please enter between 1 to 4 ");
}

That it is a nonsensical condition. When would that condition meet? Nonetheless, it is a good thing that it doesn't, because it would enter in a forever loop.

Learn how to post code with the correct tags.

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.