This is what the question is asking:
Write a program to get the user to enter a positive number. Validate the input so any negative or zero number entered is rejected and the user is to re-enter the number.


and this is what i have done

#include<stdio.h>

int main (void)

{
int x;
do {
printf("incorrect value\n");
printf("enter a positive number\n");
scanf("%d",&x);
}
while (x != 0 && x>0);
getchar ();
getchar();
return 0;
}

This works pretty fine. Is there any way that I can include the incorrect value printline without having it to appear the first time?
I know that the problem can easily be solved by ommiting that line

You only need to show the invalid prompt if the entered value was actually invalid:

printf("Enter a positive number: ");
fflush(stdout);

/* I'm ignoring non-integer input for now to keep things simple */
scanf("%d", &x);

while (x <= 0) {
    printf("Invalid input. Please try again: ");
    fflush(stdout);
    scanf("%d", &x);
}

'fflush(stdout)' forces the data (if any)in the I/O buffer to be written to the screen .Including a '\n' character(if suitable) in the 'printf' itself also does the same thing.

printf("incorrect value\n");

Remember that fflush is only used with output streams.Calling fflush with NULL argument flushes all the opened streams.

'fflush(stdout)' forces the data (if any)in the I/O buffer to be written to the screen .Including a '\n' character(if suitable) in the 'printf' itself also does the same thing.

I got the point that it outputs everything in the I/O buffer..but what is its use here? Is it perhaps the case that there isn't anything in the I/O buffer ? .. because when I removed the fflush(stdout) the same was outputted.

And another simple question.. Am i correct if I understand I/O buffer
as the temporary memory stored/inputted in the variables?

Am i correct if I understand I/O buffer
as the temporary memory stored/inputted in the variables?

see here.

.but what is its use here?

Just to make sure that everything in the buffer is written immediately after you call printf and not after sometimes when you're taking inputs from the user. Believe me it happens(or may not,it depends on the compiler you're using or the system you're running on), usually when you're trying to take inputs and give outputs subsequently.This happens because buffers are flushed out only when they're full or a newline is encountered or fflush() is called.

oh I get the point then. So, in other words it fflush can be used instead of always creating a double getchar() in the end of each prog.

Edited 5 Years Ago by terence193: n/a

oh I get the point then. So, in other words it fflush can be used instead of always creating a double getchar() in the end of each prog.

No, you don't. I've already said that fflush() should not be used with input streams, it is made to use with output streams only.People usually use getchar() at the end to consume the data that might be left in the input buffer. That could be replaced with this

while (((ch = getchar()) != '\n') && (ch != EOF))//has no body ;

But still at least a character is left in the buffer.

Edited 5 Years Ago by D33wakar: n/a

qmm , hopefully I will grasp this concept better by practice.

I have another problem with the following program I have tried which tests if sides form a right angled triangle or not.

#include<stdio.h>

int main (void)

{
int x,y,z,a,b;
printf("Enter 3 numbers to check if they form a right angled triangle\n");
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);

if (x>y && x>z) {
   a=(x*x);
   b=((y*y)+(z*z));
	if (a==b) {printf("The 3 numbers are sides of a right angled triangle\n");
	fflush(stdin);
	getchar ();
	return 0;
	}
	else {printf("The 3 numbers aren't sides of a right angled triangle\n");
	fflush(stdin);
	getchar ();
	return 0;}
	}
if (y>x && y>z) {
	a=(y*y);
   b=((x*x)+(z*z));
   if (a==b) {printf("The 3 numbers are sides of a right angled triangle\n");
	fflush(stdin);
	getchar ();
	return 0;}
	else {printf("The 3 numbers aren't sides of a right angled triangle\n");
	fflush(stdin);
	getchar ();
	return 0;}
	}
if (z>y && z>x) {
	a=(z*z);
   b=((x*x)+(y*y));
   if (a==b) {printf("The 3 numbers are sides of a right angled triangle\n");
	fflush(stdin);
	getchar ();
	return 0;}
	else {printf("The 3 numbers aren't sides of a right angled triangle\n");
	fflush(stdin);
	getchar ();
	return 0;}
	}

fflush(stdout);
getchar ();
return 0;
}

First of all, is there any unnecessary repetition?
Is there any simple way by which I can add a reply feature to the program?

Thanks

Edited 5 Years Ago by terence193: n/a

fflush(stdin);

Didn't you read my previous post ? How many times do I have to tell you that you should not use fflush() with stdin.In the c standards, it is only defined to use with the output streams.

I have another problem with the following program I have tried which tests if sides form a right angled triangle or not.

First of all, if you have another problem and the current problem is resolved then you should mark this thread URL="http://www.daniweb.com/community-center/daniweb-community-feedback/threads/226710"]"solved"[/URL] and open another thread with the new problem.

First of all, is there any unnecessary repetition?

Actually, that's your job to figure it out since you know your level of expertise and how compact you need your program to be.You can use functions to make it more short and compact.

Is there any simple way by which I can add a reply feature to the program?

That is as simple as you can get.

if(Condition)
    printf("Something");
else
    printf("something else");

This is what the question is asking:
Write a program to get the user to enter a positive number. Validate the input so any negative or zero number entered is rejected and the user is to re-enter the number.


and this is what i have done

#include<stdio.h>

int main (void)

{
int x;
do {
printf("incorrect value\n");
printf("enter a positive number\n");
scanf("%d",&x);
}
while (x != 0 && x>0);
getchar ();
getchar();
return 0;
}

This works pretty fine. Is there any way that I can include the incorrect value printline without having it to appear the first time?
I know that the problem can easily be solved by ommiting that line

int  no;
do
{
if(no>0)
{
pirntf("");
exit();
}
else
{
printf("inhvalid input")
while(1)
}
}

Edited 5 Years Ago by YAMNA MIDHAT: n/a

This question has already been answered. Start a new discussion instead.