just some simple logic.
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
terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
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);
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
oh that looks much better :)
what is the exact purpose of the fflush(stdout) ?
terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
'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?
terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
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.
terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
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
terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
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)
}
}
YAMNA MIDHAT
Junior Poster in Training
57 posts since Aug 2011
Reputation Points: 5
Solved Threads: 4